Ver código fonte

Public memory storage

onevcat 5 anos atrás
pai
commit
5b16a4e5c7

+ 2 - 2
Sources/Cache/ImageCache.swift

@@ -465,7 +465,7 @@ open class ImageCache {
         let computedKey = key.computedKey(with: identifier)
 
         if fromMemory {
-            try? memoryStorage.remove(forKey: computedKey)
+            memoryStorage.remove(forKey: computedKey)
         }
         
         if fromDisk {
@@ -633,7 +633,7 @@ open class ImageCache {
     
     /// Clears the memory storage of this cache.
     @objc public func clearMemoryCache() {
-        try? memoryStorage.removeAll()
+        memoryStorage.removeAll()
     }
     
     /// Clears the disk storage of this cache. This is an async operation.

+ 24 - 17
Sources/Cache/MemoryStorage.swift

@@ -79,7 +79,8 @@ public enum MemoryStorage {
             }
         }
 
-        func removeExpired() {
+        /// Removes the expired values from the storage.
+        public func removeExpired() {
             lock.lock()
             defer { lock.unlock() }
             for key in keys {
@@ -98,12 +99,16 @@ public enum MemoryStorage {
             }
         }
 
-        // Storing in memory will not throw. It is just for meeting protocol requirement and
-        // forwarding to no throwing method.
-        func store(
+        /// Stores a value to the storage under the specified key and expiration policy.
+        /// - Parameters:
+        ///   - value: The value to be stored.
+        ///   - key: The key to which the `value` will be stored.
+        ///   - expiration: The expiration policy used by this store action.
+        /// - Throws: No error will
+        public func store(
             value: T,
             forKey key: String,
-            expiration: StorageExpiration? = nil) throws
+            expiration: StorageExpiration? = nil)
         {
             storeNoThrow(value: value, forKey: key, expiration: expiration)
         }
@@ -126,17 +131,13 @@ public enum MemoryStorage {
             keys.insert(key)
         }
         
-        /// Use this when you actually access the memory cached item.
-        /// By default, this will extend the expired data for the accessed item.
+        /// Gets a value from the storage.
         ///
         /// - Parameters:
-        ///   - key: Cache Key
-        ///   - extendingExpiration: expiration value to extend item expiration time:
-        ///     * .none: The item expires after the original time, without extending after access.
-        ///     * .cacheTime: The item expiration extends by the original cache time after each access.
-        ///     * .expirationTime: The item expiration extends by the provided time after each access.
-        /// - Returns: cached object or nil
-        func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) -> T? {
+        ///   - key: The cache key of value.
+        ///   - extendingExpiration: The expiration policy used by this getting action.
+        /// - Returns: The value under `key` if it is valid and found in the storage. Otherwise, `nil`.
+        public func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) -> T? {
             guard let object = storage.object(forKey: key as NSString) else {
                 return nil
             }
@@ -147,21 +148,27 @@ public enum MemoryStorage {
             return object.value
         }
 
-        func isCached(forKey key: String) -> Bool {
+        /// Whether there is valid cached data under a given key.
+        /// - Parameter key: The cache key of value.
+        /// - Returns: If there is valid data under the key, `true`. Otherwise, `false`.
+        public func isCached(forKey key: String) -> Bool {
             guard let _ = value(forKey: key, extendingExpiration: .none) else {
                 return false
             }
             return true
         }
 
-        func remove(forKey key: String) throws {
+        /// Removes a value from a specified key.
+        /// - Parameter key: The cache key of value.
+        public func remove(forKey key: String) {
             lock.lock()
             defer { lock.unlock() }
             storage.removeObject(forKey: key as NSString)
             keys.remove(key)
         }
 
-        func removeAll() throws {
+        /// Removes all values in this storage.
+        public func removeAll() {
             lock.lock()
             defer { lock.unlock() }
             storage.removeAllObjects()

+ 2 - 2
Tests/KingfisherTests/ImageCacheTests.swift

@@ -345,7 +345,7 @@ class ImageCacheTests: XCTestCase {
             var cacheType = self.cache.imageCachedType(forKey: key)
             XCTAssertEqual(cacheType, .memory)
             
-            try! self.cache.memoryStorage.remove(forKey: key)
+            self.cache.memoryStorage.remove(forKey: key)
             cacheType = self.cache.imageCachedType(forKey: key)
             XCTAssertEqual(cacheType, .disk)
             
@@ -368,7 +368,7 @@ class ImageCacheTests: XCTestCase {
             var cacheType = self.cache.imageCachedType(forKey: key)
             XCTAssertEqual(cacheType, .memory)
             
-            try! self.cache.memoryStorage.remove(forKey: key)
+            self.cache.memoryStorage.remove(forKey: key)
             cacheType = self.cache.imageCachedType(forKey: key)
             XCTAssertEqual(cacheType, .disk)
             

+ 2 - 2
Tests/KingfisherTests/ImagePrefetcherTests.swift

@@ -221,7 +221,7 @@ class ImagePrefetcherTests: XCTestCase {
         let key = testKeys[0]
         cache.store(KFCrossPlatformImage(), forKey: key)
         cache.store(testImage, forKey: key) { result in
-            try! cache.memoryStorage.remove(forKey: key)
+            cache.memoryStorage.remove(forKey: key)
             
             XCTAssertEqual(cache.imageCachedType(forKey: key), .disk)
             
@@ -253,7 +253,7 @@ class ImagePrefetcherTests: XCTestCase {
         let key = testKeys[0]
 
         cache.store(testImage, forKey: key) { result in
-            try! cache.memoryStorage.remove(forKey: key)
+            cache.memoryStorage.remove(forKey: key)
             
             XCTAssertEqual(cache.imageCachedType(forKey: key), .disk)
             

+ 16 - 16
Tests/KingfisherTests/MemoryStorageTests.swift

@@ -63,36 +63,36 @@ class MemoryStorageTests: XCTestCase {
     func testStoreAndGetValue() {
         XCTAssertFalse(storage.isCached(forKey: "1"))
 
-        try! storage.store(value: 1, forKey: "1")
+        storage.store(value: 1, forKey: "1")
 
         XCTAssertTrue(storage.isCached(forKey: "1"))
         XCTAssertEqual(storage.value(forKey: "1"), 1)
     }
 
     func testStoreValueOverwritting() {
-        try! storage.store(value: 1, forKey: "1")
+        storage.store(value: 1, forKey: "1")
         XCTAssertEqual(storage.value(forKey: "1"), 1)
 
-        try! storage.store(value: 100, forKey: "1")
+        storage.store(value: 100, forKey: "1")
         XCTAssertEqual(storage.value(forKey: "1"), 100)
     }
 
     func testRemoveValue() {
         XCTAssertFalse(storage.isCached(forKey: "1"))
-        try! storage.store(value: 1, forKey: "1")
+        storage.store(value: 1, forKey: "1")
         XCTAssertTrue(storage.isCached(forKey: "1"))
 
-        try! storage.remove(forKey: "1")
+        storage.remove(forKey: "1")
         XCTAssertFalse(storage.isCached(forKey: "1"))
     }
 
     func testRemoveAllValues() {
-        try! storage.store(value: 1, forKey: "1")
-        try! storage.store(value: 2, forKey: "2")
+        storage.store(value: 1, forKey: "1")
+        storage.store(value: 2, forKey: "2")
         XCTAssertTrue(storage.isCached(forKey: "1"))
         XCTAssertTrue(storage.isCached(forKey: "2"))
 
-        try! storage.removeAll()
+        storage.removeAll()
         XCTAssertFalse(storage.isCached(forKey: "1"))
         XCTAssertFalse(storage.isCached(forKey: "2"))
     }
@@ -101,11 +101,11 @@ class MemoryStorageTests: XCTestCase {
         let exp = expectation(description: #function)
 
         XCTAssertFalse(storage.isCached(forKey: "1"))
-        try! storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
+        storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
         XCTAssertTrue(storage.isCached(forKey: "1"))
 
         XCTAssertFalse(storage.isCached(forKey: "2"))
-        try! storage.store(value: 2, forKey: "2")
+        storage.store(value: 2, forKey: "2")
         XCTAssertTrue(storage.isCached(forKey: "2"))
 
         delay(0.2) {
@@ -126,7 +126,7 @@ class MemoryStorageTests: XCTestCase {
         storage.config.expiration = .seconds(0.1)
 
         XCTAssertFalse(storage.isCached(forKey: "1"))
-        try! storage.store(value: 1, forKey: "1")
+        storage.store(value: 1, forKey: "1")
         XCTAssertTrue(storage.isCached(forKey: "1"))
 
         delay(0.2) {
@@ -143,7 +143,7 @@ class MemoryStorageTests: XCTestCase {
         let exp = expectation(description: #function)
         
         XCTAssertFalse(storage.isCached(forKey: "1"))
-        try! storage.store(value: 1, forKey: "1", expiration: .seconds(1))
+        storage.store(value: 1, forKey: "1", expiration: .seconds(1))
         XCTAssertTrue(storage.isCached(forKey: "1"))
         
         delay(0.1) {
@@ -169,7 +169,7 @@ class MemoryStorageTests: XCTestCase {
         let exp = expectation(description: #function)
         
         XCTAssertFalse(storage.isCached(forKey: "1"))
-        try! storage.store(value: 1, forKey: "1", expiration: .seconds(1))
+        storage.store(value: 1, forKey: "1", expiration: .seconds(1))
         XCTAssertTrue(storage.isCached(forKey: "1"))
         
         delay(0.1) {
@@ -194,7 +194,7 @@ class MemoryStorageTests: XCTestCase {
         let exp = expectation(description: #function)
 
         XCTAssertFalse(storage.isCached(forKey: "1"))
-        try! storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
+        storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
         XCTAssertTrue(storage.isCached(forKey: "1"))
 
         delay(0.2) {
@@ -215,7 +215,7 @@ class MemoryStorageTests: XCTestCase {
         let exp = expectation(description: #function)
 
         let expiration = StorageExpiration.seconds(0.5)
-        try! storage.store(value: 1, forKey: "1", expiration: expiration)
+        storage.store(value: 1, forKey: "1", expiration: expiration)
 
         delay(0.3) {
             // This should extend the expiration to (0.3 + 0.5) from initially created.
@@ -240,7 +240,7 @@ class MemoryStorageTests: XCTestCase {
         let config = MemoryStorage.Config(totalCostLimit: 3, cleanInterval: 0.1)
         storage = MemoryStorage.Backend(config: config)
 
-        try! storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
+        storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
         XCTAssertTrue(storage.isCached(forKey: "1"))
         XCTAssertEqual(self.storage.keys.count, 1)