Просмотр исходного кода

Add BackgroundKeepingStorageObject

onevcat 4 лет назад
Родитель
Сommit
382c5d1955
1 измененных файлов с 27 добавлено и 2 удалено
  1. 27 2
      Sources/Cache/MemoryStorage.swift

+ 27 - 2
Sources/Cache/MemoryStorage.swift

@@ -126,7 +126,7 @@ public enum MemoryStorage {
             // The expiration indicates that already expired, no need to store.
             guard !expiration.isExpired else { return }
             
-            let object = StorageObject(value, key: key, expiration: expiration)
+            let object = BackgroundKeepingStorageObject(value, key: key, expiration: expiration)
             storage.setObject(object, forKey: key as NSString, cost: value.cacheCost)
             keys.insert(key)
         }
@@ -211,8 +211,33 @@ extension MemoryStorage {
 }
 
 extension MemoryStorage {
+    
+    class BackgroundKeepingStorageObject<T>: StorageObject<T>, NSDiscardableContent {
+        var accessing = true
+        func beginContentAccess() -> Bool {
+            if value != nil {
+                accessing = true
+            } else {
+                accessing = false
+            }
+            return accessing
+        }
+        
+        func endContentAccess() {
+            accessing = false
+        }
+        
+        func discardContentIfPossible() {
+            value = nil
+        }
+        
+        func isContentDiscarded() -> Bool {
+            return value == nil
+        }
+    }
+    
     class StorageObject<T> {
-        let value: T
+        var value: T?
         let expiration: StorageExpiration
         let key: String