You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,9 +117,13 @@ To use `PersistableCache`, make sure that the specified key type conforms to bot
117
117
Here's an example of creating a cache, setting a value, and saving it to disk:
118
118
119
119
```swift
120
-
let cache = PersistableCache<String, Double>()
120
+
enumKey: String{
121
+
casepi
122
+
}
123
+
124
+
let cache = PersistableCache<Key, Double, Double>()
121
125
122
-
cache["pi"] =Double.pi
126
+
cache[.pi] =Double.pi
123
127
124
128
do {
125
129
try cache.save()
@@ -131,9 +135,9 @@ To use `PersistableCache`, make sure that the specified key type conforms to bot
131
135
You can also load a previously saved cache from disk:
132
136
133
137
```swift
134
-
let cache = PersistableCache<String, Double>()
138
+
let cache = PersistableCache<Key, Double, Double>()
135
139
136
-
let pi = cache["pi"] // pi == Double.pi
140
+
let pi = cache[.pi] // pi == Double.pi
137
141
```
138
142
139
143
Remember that the `save()` function may throw errors if the encoder fails to serialize the cache to JSON or the disk write operation fails. Make sure to handle the errors appropriately.
Copy file name to clipboardExpand all lines: Sources/Cache/Cache/PersistableCache.swift
+57-12Lines changed: 57 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -9,9 +9,13 @@ import Foundation
9
9
Here's an example of creating a cache, setting a value, and saving it to disk:
10
10
11
11
```swift
12
-
let cache = PersistableCache<String, Double>()
12
+
enum Key: String {
13
+
case pi
14
+
}
15
+
16
+
let cache = PersistableCache<Key, Double, Double>()
13
17
14
-
cache["pi"] = Double.pi
18
+
cache[.pi] = Double.pi
15
19
16
20
do {
17
21
try cache.save()
@@ -23,9 +27,9 @@ import Foundation
23
27
You can also load a previously saved cache from disk:
24
28
25
29
```swift
26
-
let cache = PersistableCache<String, Double>()
30
+
let cache = PersistableCache<Key, Double, Double>()
27
31
28
-
let pi = cache["pi"] // pi == Double.pi
32
+
let pi = cache[.pi] // pi == Double.pi
29
33
```
30
34
31
35
Note: You must make sure that the specified key type conforms to both `RawRepresentable` and `Hashable` protocols. The `RawValue` of `Key` must be a `String` type.
0 commit comments