Skip to content

Commit 833d530

Browse files
committed
Added subscripts
1 parent 081182e commit 833d530

File tree

3 files changed

+91
-14
lines changed

3 files changed

+91
-14
lines changed
Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11
import Foundation
22

33
public struct DiskConfig {
4-
/// The name of disk storage, this will be used as folder name within directory
4+
5+
/// The name of disk storage, this will be used as folder name within directory.
6+
///
57
public let name: String
68

7-
/// Expiry date that will be applied by default for every added object
8-
/// if it's not overridden in the add(key: object: expiry: completion:) method
9+
/// Expiry date that will be applied by default for every added object if it's not
10+
/// overridden in the add(key: object: expiry: completion:) method.
11+
///
912
public let expiry: Expiry
1013

11-
/// Maximum size of the disk cache storage (in bytes)
14+
/// Maximum size of the disk cache storage (in bytes).
15+
///
1216
public let maxSize: UInt
1317

14-
/// A folder to store the disk cache contents. Defaults to a prefixed directory in Caches if nil
15-
18+
/// A folder to store the disk cache contents. Defaults to a prefixed directory in
19+
/// Caches if nil.
20+
///
1621
public let directory: URL?
22+
1723
#if os(iOS) || os(tvOS)
18-
/// Data protection is used to store files in an encrypted format on disk and to decrypt them on demand.
19-
/// Support only on iOS and tvOS.
24+
25+
/// Data protection is used to store files in an encrypted format on disk and to
26+
/// decrypt them on demand. Support only on iOS and tvOS.
27+
///
2028
public let protectionType: FileProtectionType?
2129

30+
/// Create a on disk cache configuration.
31+
///
32+
/// - parameter name: The name of disk storage, this will be used as folder name
33+
/// within directory.
34+
/// - parameter expiry: Expiry date that will be applied by default for every added
35+
/// object if it's not overridden in the add(key: object: expiry: completion:)
36+
/// method.
37+
/// - parameter maxSize: Maximum size of the disk cache storage (in bytes).
38+
/// - parameter directory: A folder to store the disk cache contents. Defaults to a
39+
/// prefixed directory in Caches if nil.
40+
/// - parameter protectionType: Data protection is used to store files in an
41+
/// encrypted format on disk and to decrypt them on demand. Support only on iOS and
42+
/// tvOS.
43+
///
2244
public init(name: String, expiry: Expiry = .never,
2345
maxSize: UInt = 0, directory: URL? = nil,
2446
protectionType: FileProtectionType? = nil) {
@@ -28,14 +50,17 @@ public struct DiskConfig {
2850
self.directory = directory
2951
self.protectionType = protectionType
3052
}
53+
3154
#else
55+
3256
public init(name: String, expiry: Expiry = .never,
3357
maxSize: UInt = 0, directory: URL? = nil) {
3458
self.name = name
3559
self.expiry = expiry
3660
self.maxSize = maxSize
3761
self.directory = directory
3862
}
63+
3964
#endif
4065

4166
}

Source/Shared/Configuration/MemoryConfig.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@ import Foundation
22

33
public struct MemoryConfig {
44

5-
/// Expiry date that will be applied by default for every added object
6-
/// if it's not overridden in the add(key: object: expiry: completion:) method
5+
/// Expiry date that will be applied by default for every added object if it's not
6+
/// overridden in the add(key: object: expiry: completion:) method.
7+
///
78
public let expiry: Expiry
89

9-
/// The maximum number of objects in memory the cache should hold.
10-
/// If 0, there is no count limit. The default value is 0.
10+
/// The maximum number of objects in memory the cache should hold. If 0, there is no
11+
/// count limit. The default value is 0.
12+
///
1113
public let countLimit: UInt
1214

13-
/// The maximum total cost that the cache can hold before it starts evicting objects.
14-
/// If 0, there is no total cost limit. The default value is 0
15+
/// The maximum total cost that the cache can hold before it starts evicting
16+
/// objects. If 0, there is no total cost limit. The default value is 0.
17+
///
1518
public let totalCostLimit: UInt
1619

20+
/// Create an in-memory cache configuration.
21+
///
22+
/// - parameter expiry: Expiry date that will be applied by default for every added
23+
/// object if it's not overridden in the add(key: object: expiry: completion:)
24+
/// method.
25+
/// - parameter countLimit: The maximum number of objects in memory the cache should
26+
/// hold. If 0, there is no count limit. The default value is 0.
27+
/// - parameter totalCostLimit: The maximum total cost that the cache can hold
28+
/// before it starts evicting objects. If 0, there is no total cost limit. The
29+
/// default value is 0.
30+
///
1731
public init(expiry: Expiry = .never, countLimit: UInt = 0, totalCostLimit: UInt = 0) {
1832
self.expiry = expiry
1933
self.countLimit = countLimit

Source/Shared/Storage/CacheStorage.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public final class CacheStorage<Key: Hashable, Value> {
1616
/// - parameters:
1717
/// - diskConfig: Configuration for disk storage
1818
/// - memoryConfig: Optional. Pass config if you want memory cache
19+
/// - transformer: A custom transformer for stored values
1920
/// - throws: Throw StorageError if any.
2021
///
2122
public convenience init(diskConfig: DiskConfig, memoryConfig: MemoryConfig, transformer: Transformer<Value>) throws {
@@ -50,30 +51,53 @@ public final class CacheStorage<Key: Hashable, Value> {
5051

5152
extension CacheStorage: StorageAware {
5253

54+
/// Returns all cached keys
55+
///
5356
public var allKeys: [Key] {
5457
self.syncStorage.allKeys
5558
}
5659

60+
/// Returns all cached objects
61+
///
5762
public var allObjects: [Value] {
5863
self.syncStorage.allObjects
5964
}
6065

66+
/// Returns the specified entry for the given key
67+
///
68+
/// - parameter key: The key of the cached object.
69+
/// - returns: An `Entry` object which has metadata, expiry, and an object.
70+
///
6171
public func entry(forKey key: Key) throws -> Entry<Value> {
6272
return try self.syncStorage.entry(forKey: key)
6373
}
6474

75+
/// Remove an object from the cache.
76+
///
77+
/// - parameter key: The key of the cached object.
78+
///
6579
public func removeObject(forKey key: Key) throws {
6680
try self.syncStorage.removeObject(forKey: key)
6781
}
6882

83+
/// Add an object to the cache.
84+
///
85+
/// - parameter value: The value of the cached object.
86+
/// - parameter key: The key of the cached object.
87+
/// - parameter expiry: A custom expiration for the object.
88+
///
6989
public func setObject(_ object: Value, forKey key: Key, expiry: Expiry? = nil) throws {
7090
try self.syncStorage.setObject(object, forKey: key, expiry: expiry)
7191
}
7292

93+
/// Removes all cached objects
94+
///
7395
public func removeAll() throws {
7496
try self.syncStorage.removeAll()
7597
}
7698

99+
/// Removes all expired cache objects
100+
///
77101
public func removeExpiredObjects() throws {
78102
try self.syncStorage.removeExpiredObjects()
79103
}
@@ -86,6 +110,20 @@ public extension CacheStorage {
86110
return CacheStorage<Key, U>(hybridStorage: hybridStorage.transform(transformer: transformer))
87111
}
88112

113+
subscript(key: Key) -> Value? {
114+
get { return try? entry(forKey: key).object }
115+
set {
116+
guard let value = newValue else {
117+
// If nil was assigned using our subscript,
118+
// then we remove any value for that key:
119+
try? removeObject(forKey: key)
120+
return
121+
}
122+
123+
try? setObject(value, forKey: key)
124+
}
125+
}
126+
89127
}
90128

91129
extension CacheStorage: StorageObservationRegistry {

0 commit comments

Comments
 (0)