2222
2323import Foundation
2424
25- // MARK: - Cache
25+ // MARK: - Stash
2626
2727/// A simple cache that can be used to store objects.
2828///
29- /// Use a cache to store objects of a given type in memory using an associated key.
30- /// You can then fetch attempt to retrieve from the cache at a later time using the key.
29+ /// Use a ``Stash`` to store objects of a given type in memory using an associated key.
30+ /// You can then fetch attempt to retrieve from the ``Stash`` at a later time using the key.
3131///
3232/// ```swift
33- /// // Create a cache of dogs.
34- /// let cache = Cache <String, Dog>
33+ /// // Create a stash of dogs.
34+ /// let stash = Stash <String, Dog>
3535///
36- /// // Store a dog inside the cache .
36+ /// // Store a dog inside the stash .
3737/// let fido = Dog(name: "Fido")
38- /// cache .insert(fido, forKey: "1")
38+ /// stash .insert(fido, forKey: "1")
3939///
40- /// // Retrieve a dog from the cache .
41- /// let cachedDog = cache .value(forKey: "1")
40+ /// // Retrieve a dog from the stash .
41+ /// let stashdDog = stash .value(forKey: "1")
4242///
43- /// // Remove a dog from the cache .
44- /// cache .removeValue(forKey: "1")
43+ /// // Remove a dog from the stash .
44+ /// stash .removeValue(forKey: "1")
4545///
46- /// // Interact with the cache using subscripts.
47- /// cache ["2"] = Dog(name: "Spark")
48- /// let otherCachedDog = cache ["2"]
46+ /// // Interact with the stash using subscripts.
47+ /// stash ["2"] = Dog(name: "Spark")
48+ /// let otherStasheedDog = stash ["2"]
4949/// ```
5050///
51- /// The cache implements a default limetime for which an item is considered valid. If you fetch an object
52- /// from the cache and it has existed beyond the cache 's specified lifetime, the cached object will be removed
51+ /// The stash implements a default limetime for which an item is considered valid. If you fetch an object
52+ /// from the stash and it has existed beyond the stash 's specified lifetime, the stashd object will be removed
5353/// and the retrievel will fail, returning a null value.
5454///
5555/// If both your key and the object you are storing are `Codable`, you can persist
56- /// your cache to disk and load it at a later point in time.
57- public final class Cache < Key: Hashable , Value> {
56+ /// your stash to disk and load it at a later point in time.
57+ public final class Stash < Key: Hashable , Value> {
5858
5959 // MARK: Properties
6060
61- /// The provider used to create dates for cache entries.
61+ /// The provider used to create dates for stash entries.
6262 let dateProvider : ( ) -> Date
6363
64- /// The max lifetime that a value should exist in the cache .
64+ /// The max lifetime that a value should exist in the stash .
6565 let lifetime : TimeInterval
6666
6767 /// The actual cache store.
@@ -70,19 +70,19 @@ public final class Cache<Key: Hashable, Value> {
7070 /// An observer of the store.
7171 let keyTracker = KeyTracker ( )
7272
73- /// The keys that currently exist within the cache .
73+ /// The keys that currently exist within the stash .
7474 public var keys : Set < Key > {
7575 return keyTracker. keys
7676 }
7777
7878 // MARK: Initializers
7979
80- /// Creates a cache with the provided configuration.
80+ /// Creates a stash with the provided configuration.
8181 ///
8282 /// - Parameters:
83- /// - dateProvider: The provider used to create dates for cache entries.
84- /// - lifetime: The max lifetime that a value should exist in the cache .
85- /// - limit: The max number of items that should exist in the cache .
83+ /// - dateProvider: The provider used to create dates for stash entries.
84+ /// - lifetime: The max lifetime that a value should exist in the stash .
85+ /// - limit: The max number of items that should exist in the stash .
8686 public init (
8787 dateProvider: @escaping ( ) -> Date = Date . init,
8888 lifetime: TimeInterval = 12 * 60 * 60 ,
@@ -96,7 +96,7 @@ public final class Cache<Key: Hashable, Value> {
9696
9797 // MARK: Actions
9898
99- /// Inserts the provided value into the cache .
99+ /// Inserts the provided value into the stash .
100100 ///
101101 /// - Parameters:
102102 /// - value: The value to be inserted.
@@ -107,17 +107,17 @@ public final class Cache<Key: Hashable, Value> {
107107 insert ( entry)
108108 }
109109
110- /// Retrieves the value for the provided key from the cache .
110+ /// Retrieves the value for the provided key from the stash .
111111 ///
112- /// - Parameter key: The key used to lookup the value in the cache .
112+ /// - Parameter key: The key used to lookup the value in the stash .
113113 /// - Returns: Returns the value if it exists, otherwise nil.
114114 public func value( forKey key: Key ) -> Value ? {
115115 entry ( forKey: key) ? . value
116116 }
117117
118- /// Deletes the value in the cache for the provided key.
118+ /// Deletes the value in the stash for the provided key.
119119 ///
120- /// - Parameter key: The key used to lookup the value in the cache .
120+ /// - Parameter key: The key used to lookup the value in the stash .
121121 public func removeValue( forKey key: Key ) {
122122 store. removeObject ( forKey: WrappedKey ( key) )
123123 }
@@ -137,7 +137,7 @@ public final class Cache<Key: Hashable, Value> {
137137
138138 // MARK: Private Actions
139139
140- /// Inserts the provided entry into the cache .
140+ /// Inserts the provided entry into the stash .
141141 ///
142142 /// - Parameters:
143143 /// - entry: The entry to be inserted.
@@ -146,7 +146,7 @@ public final class Cache<Key: Hashable, Value> {
146146 keyTracker. keys. insert ( entry. key)
147147 }
148148
149- /// Gets the cache entry for the provided key.
149+ /// Gets the stash entry for the provided key.
150150 ///
151151 /// - Parameter key: The key used to lookup the entry.
152152 /// - Returns: The entry, if it exists, otherwise nil.
@@ -164,9 +164,9 @@ public final class Cache<Key: Hashable, Value> {
164164 }
165165}
166166
167- // MARK: - Cache + Codable
167+ // MARK: - Stash + Codable
168168
169- extension Cache : Codable where Key: Codable , Value: Codable {
169+ extension Stash : Codable where Key: Codable , Value: Codable {
170170 public convenience init ( from decoder: Decoder ) throws {
171171 self . init ( )
172172
@@ -180,35 +180,35 @@ extension Cache: Codable where Key: Codable, Value: Codable {
180180 try container. encode ( keyTracker. keys. compactMap ( entry) )
181181 }
182182
183- /// Saves the cache to disk for long-term storage.
183+ /// Saves the stash to disk for long-term storage.
184184 ///
185185 /// - Parameters:
186- /// - name: The name of the cache file.
187- /// - fileManager: The file manager that should store the cache .
186+ /// - name: The name of the stash file.
187+ /// - fileManager: The file manager that should store the stash .
188188 public func saveToDisk( withName name: String , using fileManager: FileManager = . default) throws {
189189 let folderURLs = fileManager. urls ( for: . cachesDirectory, in: . userDomainMask)
190- let fileURL = folderURLs [ 0 ] . appendingPathComponent ( name + " .cache " )
190+ let fileURL = folderURLs [ 0 ] . appendingPathComponent ( name + " .stash " )
191191 let data = try JSONEncoder ( ) . encode ( self )
192192 try data. write ( to: fileURL)
193193 }
194194
195- /// Loads a cache from disk, initializing it for usage.
195+ /// Loads a stash from disk, initializing it for usage.
196196 ///
197197 /// - Parameters:
198- /// - name: The name of the cache file.
198+ /// - name: The name of the stash file.
199199 /// - keyType: The type
200- /// - valueType: The type of item being stored in the cache .
201- /// - fileManager: The file manager that should load the cache from disk.
202- /// - Returns: A cache initialized with the stored data.
200+ /// - valueType: The type of item being stored in the stash .
201+ /// - fileManager: The file manager that should load the stash from disk.
202+ /// - Returns: A stash initialized with the stored data.
203203 public static func loadFromDisk(
204204 withName name: String ,
205205 keyType: Key . Type ,
206206 valueType: Value . Type ,
207207 using fileManager: FileManager = . default
208- ) throws -> Cache < Key , Value > {
208+ ) throws -> Stash < Key , Value > {
209209 let folderURLs = fileManager. urls ( for: . cachesDirectory, in: . userDomainMask)
210- let fileURL = folderURLs [ 0 ] . appendingPathComponent ( name + " .cache " )
210+ let fileURL = folderURLs [ 0 ] . appendingPathComponent ( name + " .stash " )
211211 let data = try Data ( contentsOf: fileURL)
212- return try JSONDecoder ( ) . decode ( Cache < Key , Value > . self, from: data)
212+ return try JSONDecoder ( ) . decode ( Stash < Key , Value > . self, from: data)
213213 }
214214}
0 commit comments