Skip to content

Commit 8c86994

Browse files
committed
Add DiskStorage size property
1 parent c7f4d63 commit 8c86994

File tree

6 files changed

+76
-14
lines changed

6 files changed

+76
-14
lines changed

Cache.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
0E79164B250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E791649250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift */; };
1212
0E79164C250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E791649250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift */; };
1313
0E79164E250E2B0400A71666 /* Hasher+constantAccrossExecutions+Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E79164D250E2B0400A71666 /* Hasher+constantAccrossExecutions+Tests.swift */; };
14+
2292AB7B252A14520031F3B9 /* URL+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2292AB7A252A14520031F3B9 /* URL+Extensions.swift */; };
15+
2292AB7C252A14520031F3B9 /* URL+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2292AB7A252A14520031F3B9 /* URL+Extensions.swift */; };
16+
2292AB7D252A14520031F3B9 /* URL+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2292AB7A252A14520031F3B9 /* URL+Extensions.swift */; };
17+
2292AB85252A14F50031F3B9 /* FileManager+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2292AB84252A14F50031F3B9 /* FileManager+Extensions.swift */; };
18+
2292AB86252A14F50031F3B9 /* FileManager+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2292AB84252A14F50031F3B9 /* FileManager+Extensions.swift */; };
19+
2292AB87252A14F50031F3B9 /* FileManager+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2292AB84252A14F50031F3B9 /* FileManager+Extensions.swift */; };
1420
BDEDD37D1DBCEB8A007416A6 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BDEDD3561DBCE5B1007416A6 /* Cache.framework */; };
1521
D21B667C1F6A723C00125DE1 /* DataSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2CF98531F694FFA00CE8F68 /* DataSerializer.swift */; };
1622
D21B667E1F6A723C00125DE1 /* ExpirationMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2CF98551F694FFA00CE8F68 /* ExpirationMode.swift */; };
@@ -179,6 +185,8 @@
179185
/* Begin PBXFileReference section */
180186
0E791649250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Hasher+constantAccrossExecutions.swift"; sourceTree = "<group>"; };
181187
0E79164D250E2B0400A71666 /* Hasher+constantAccrossExecutions+Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Hasher+constantAccrossExecutions+Tests.swift"; sourceTree = "<group>"; };
188+
2292AB7A252A14520031F3B9 /* URL+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Extensions.swift"; sourceTree = "<group>"; };
189+
2292AB84252A14F50031F3B9 /* FileManager+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+Extensions.swift"; sourceTree = "<group>"; };
182190
BDEDD3561DBCE5B1007416A6 /* Cache.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Cache.framework; sourceTree = BUILT_PRODUCTS_DIR; };
183191
BDEDD3781DBCEB8A007416A6 /* Cache-tvOS-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Cache-tvOS-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
184192
D221E5BB20D00D9300BC940E /* MemoryStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryStorage.swift; sourceTree = "<group>"; };
@@ -332,6 +340,8 @@
332340
0E791649250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift */,
333341
D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */,
334342
D2CF98501F694FFA00CE8F68 /* Date+Extensions.swift */,
343+
2292AB7A252A14520031F3B9 /* URL+Extensions.swift */,
344+
2292AB84252A14F50031F3B9 /* FileManager+Extensions.swift */,
335345
);
336346
path = Extensions;
337347
sourceTree = "<group>";
@@ -792,10 +802,12 @@
792802
D2D4CC261FA3426B00E4A2D5 /* JSONArrayWrapper.swift in Sources */,
793803
D27014A720D129EB003B45C7 /* TransformerFactory.swift in Sources */,
794804
D270147E20D107DA003B45C7 /* SyncStorage.swift in Sources */,
805+
2292AB7D252A14520031F3B9 /* URL+Extensions.swift in Sources */,
795806
D292DAFF1F6A970B0060F614 /* Result.swift in Sources */,
796807
D21B669A1F6A724300125DE1 /* Date+Extensions.swift in Sources */,
797808
0E79164C250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift in Sources */,
798809
D21B66891F6A723C00125DE1 /* ImageWrapper.swift in Sources */,
810+
2292AB87252A14F50031F3B9 /* FileManager+Extensions.swift in Sources */,
799811
D21B668B1F6A723C00125DE1 /* StorageError.swift in Sources */,
800812
D5A9D1B921134547005DBD3F /* ObservationToken.swift in Sources */,
801813
D270148A20D11040003B45C7 /* Storage+Transform.swift in Sources */,
@@ -880,10 +892,12 @@
880892
D2D4CC251FA3426B00E4A2D5 /* JSONArrayWrapper.swift in Sources */,
881893
D27014A620D129EB003B45C7 /* TransformerFactory.swift in Sources */,
882894
D270147D20D107DA003B45C7 /* SyncStorage.swift in Sources */,
895+
2292AB7C252A14520031F3B9 /* URL+Extensions.swift in Sources */,
883896
D292DAFE1F6A970B0060F614 /* Result.swift in Sources */,
884897
D21B66991F6A724200125DE1 /* Date+Extensions.swift in Sources */,
885898
0E79164B250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift in Sources */,
886899
D21B66801F6A723C00125DE1 /* ImageWrapper.swift in Sources */,
900+
2292AB86252A14F50031F3B9 /* FileManager+Extensions.swift in Sources */,
887901
D21B66821F6A723C00125DE1 /* StorageError.swift in Sources */,
888902
D5A9D1B821134547005DBD3F /* ObservationToken.swift in Sources */,
889903
D270148920D11040003B45C7 /* Storage+Transform.swift in Sources */,
@@ -934,10 +948,12 @@
934948
D292DAFD1F6A970B0060F614 /* Result.swift in Sources */,
935949
D27014A520D129EB003B45C7 /* TransformerFactory.swift in Sources */,
936950
D2D4CC241FA3426B00E4A2D5 /* JSONArrayWrapper.swift in Sources */,
951+
2292AB7B252A14520031F3B9 /* URL+Extensions.swift in Sources */,
937952
D270147C20D107DA003B45C7 /* SyncStorage.swift in Sources */,
938953
D2CF98671F694FFA00CE8F68 /* Expiry.swift in Sources */,
939954
0E79164A250E2AA500A71666 /* Hasher+constantAccrossExecutions.swift in Sources */,
940955
D270148820D11040003B45C7 /* Storage+Transform.swift in Sources */,
956+
2292AB85252A14F50031F3B9 /* FileManager+Extensions.swift in Sources */,
941957
D2CF986A1F694FFA00CE8F68 /* StorageError.swift in Sources */,
942958
D5A9D1B721134547005DBD3F /* ObservationToken.swift in Sources */,
943959
D5A138C11EB29BFA00881A20 /* UIImage+Extensions.swift in Sources */,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import Foundation
3+
4+
extension FileManager {
5+
/**
6+
Calculates the size of the directory at the provided `URL`
7+
- parameter url: the directory url
8+
- returns: the size of the provided directory in bytes
9+
*/
10+
public func sizeOfDirectory(at url: URL) -> Int? {
11+
guard let enumerator = self.enumerator(at: url, includingPropertiesForKeys: [], options: [], errorHandler: { _, _ -> Bool in
12+
return false
13+
}) else {
14+
return nil
15+
}
16+
var size = 0
17+
for case let url as URL in enumerator {
18+
size += url.fileSize ?? 0
19+
}
20+
return size
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
import Foundation
3+
4+
extension URL {
5+
/// Returns the file size of the file at the given `URL` in bytes
6+
var fileSize: Int? {
7+
do {
8+
let file = try self.resourceValues(forKeys: [.totalFileAllocatedSizeKey, .fileAllocatedSizeKey])
9+
return file.totalFileAllocatedSize ?? file.fileAllocatedSize
10+
} catch {
11+
return nil
12+
}
13+
}
14+
}

Source/Shared/Storage/DiskStorage.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,6 @@ extension DiskStorage {
188188
return "\(path)/\(makeFileName(for: key))"
189189
}
190190

191-
/// Calculates total disk cache size.
192-
func totalSize() throws -> UInt64 {
193-
var size: UInt64 = 0
194-
let contents = try fileManager.contentsOfDirectory(atPath: path)
195-
for pathComponent in contents {
196-
let filePath = NSString(string: path).appendingPathComponent(pathComponent)
197-
let attributes = try fileManager.attributesOfItem(atPath: filePath)
198-
if let fileSize = attributes[.size] as? UInt64 {
199-
size += fileSize
200-
}
201-
}
202-
return size
203-
}
204-
205191
func createDirectory() throws {
206192
guard !fileManager.fileExists(atPath: path) else {
207193
return
@@ -273,3 +259,13 @@ public extension DiskStorage {
273259
return storage
274260
}
275261
}
262+
263+
public extension DiskStorage {
264+
/// Calculates the total size of the cache directory in bytes.
265+
var totalSize: Int? {
266+
if let directory = URL(string: self.path), let size = self.fileManager.sizeOfDirectory(at: directory) {
267+
return size
268+
}
269+
return nil
270+
}
271+
}

Source/Shared/Storage/HybridStorage.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,10 @@ extension HybridStorage: KeyObservationRegistry {
165165
}
166166
}
167167
}
168+
169+
public extension HybridStorage {
170+
/// Returns the total size of the underlying DiskStorage in bytes.
171+
var totalDiskStorageSize: Int? {
172+
return self.diskStorage.totalSize
173+
}
174+
}

Source/Shared/Storage/Storage.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,10 @@ extension Storage: KeyObservationRegistry {
108108
hybridStorage.removeAllKeyObservers()
109109
}
110110
}
111+
112+
public extension Storage {
113+
/// Returns the total size of the DiskStorage of the underlying HybridStorage in bytes.
114+
var totalDiskStorageSize: Int? {
115+
return self.hybridStorage.diskStorage.totalSize
116+
}
117+
}

0 commit comments

Comments
 (0)