Skip to content

Commit e468810

Browse files
committed
Merge pull request #5 from hyperoslo/feature/cachable
Add Cachable protocol
2 parents e42ccc2 + f501e7d commit e468810

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

Pod/Pod.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
D520D4B01BBC4770001A2A31 /* MemoryCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D520D4AF1BBC4770001A2A31 /* MemoryCache.swift */; settings = {ASSET_TAGS = (); }; };
1212
D520D4B21BBC47A2001A2A31 /* DiskCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D520D4B11BBC47A2001A2A31 /* DiskCache.swift */; settings = {ASSET_TAGS = (); }; };
1313
D520D4B61BBC4D0B001A2A31 /* CacheAware.swift in Sources */ = {isa = PBXBuildFile; fileRef = D520D4B51BBC4D0B001A2A31 /* CacheAware.swift */; settings = {ASSET_TAGS = (); }; };
14+
D520D4BB1BBEB59D001A2A31 /* Cachable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D520D4BA1BBEB59D001A2A31 /* Cachable.swift */; settings = {ASSET_TAGS = (); }; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXFileReference section */
@@ -26,6 +27,7 @@
2627
D520D4AF1BBC4770001A2A31 /* MemoryCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryCache.swift; sourceTree = "<group>"; };
2728
D520D4B11BBC47A2001A2A31 /* DiskCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskCache.swift; sourceTree = "<group>"; };
2829
D520D4B51BBC4D0B001A2A31 /* CacheAware.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheAware.swift; sourceTree = "<group>"; };
30+
D520D4BA1BBEB59D001A2A31 /* Cachable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cachable.swift; sourceTree = "<group>"; };
2931
/* End PBXFileReference section */
3032

3133
/* Begin PBXFrameworksBuildPhase section */
@@ -95,6 +97,7 @@
9597
D520D4AF1BBC4770001A2A31 /* MemoryCache.swift */,
9698
D520D4B11BBC47A2001A2A31 /* DiskCache.swift */,
9799
D520D4B51BBC4D0B001A2A31 /* CacheAware.swift */,
100+
D520D4BA1BBEB59D001A2A31 /* Cachable.swift */,
98101
);
99102
name = Source;
100103
path = ../Source;
@@ -170,6 +173,7 @@
170173
files = (
171174
D520D4B01BBC4770001A2A31 /* MemoryCache.swift in Sources */,
172175
D520D4B21BBC47A2001A2A31 /* DiskCache.swift in Sources */,
176+
D520D4BB1BBEB59D001A2A31 /* Cachable.swift in Sources */,
173177
D520D4B61BBC4D0B001A2A31 /* CacheAware.swift in Sources */,
174178
14A139B41AEFC72B00AD732F /* Tests.swift in Sources */,
175179
);

Source/Cachable.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Foundation
2+
3+
public protocol Cachable: AnyObject {}
4+
5+
public extension Cachable {
6+
func encode() -> NSData {
7+
var value = self
8+
return withUnsafePointer(&value) { p in
9+
NSData(bytes: p, length: sizeofValue(value))
10+
}
11+
}
12+
13+
static func decode<T: Cachable>(data: NSData) -> T {
14+
let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T.Type))
15+
data.getBytes(pointer, length: sizeof(T))
16+
return pointer.move()
17+
}
18+
}

Source/CacheAware.swift

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
import Foundation
2-
3-
public protocol Cachable: AnyObject {}
4-
5-
public extension Cachable {
6-
func encode() -> NSData {
7-
var value = self
8-
return withUnsafePointer(&value) { p in
9-
NSData(bytes: p, length: sizeofValue(value))
10-
}
11-
}
12-
13-
static func decode<T: Cachable>(data: NSData) -> T {
14-
let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T.Type))
15-
data.getBytes(pointer, length: sizeof(T))
16-
return pointer.move()
17-
}
18-
}
19-
201
public protocol CacheAware {
212
var prefix: String { get }
223
var path: String { get }

Source/DiskCache.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public class DiskCache: CacheAware {
1717
// MARK: - CacheAware
1818

1919
public func add<T: Cachable>(key: String, object: T) {
20+
if !fileManager.fileExistsAtPath(path) {
21+
do {
22+
try fileManager.createDirectoryAtPath(path,
23+
withIntermediateDirectories: true, attributes: nil)
24+
} catch _ {}
25+
}
26+
27+
fileManager.createFileAtPath(filePath(key),
28+
contents: object.encode(), attributes: nil)
2029
}
2130

2231
public func object<T: Cachable>(key: String) -> T? {
@@ -28,4 +37,10 @@ public class DiskCache: CacheAware {
2837

2938
public func clear() {
3039
}
40+
41+
// MARK: - Helpers
42+
43+
private func filePath(key: String) -> String {
44+
return "\(path)/key"
45+
}
3146
}

0 commit comments

Comments
 (0)