Skip to content

Commit

Permalink
Merge pull request #311 from pareshios/remove_inmemory_cache
Browse files Browse the repository at this point in the history
feat: Added support to remove only in memory cache
  • Loading branch information
3lvis authored May 14, 2023
2 parents eeaf771 + de764f8 commit d048bf4
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Source/Shared/Storage/DiskStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ extension DiskStorage: StorageAware {
// Remove objects if storage size exceeds max size
try removeResourceObjects(resourceObjects, totalSize: totalSize)
}

public func removeInMemoryObject(forKey key: Key) throws { }
}

extension DiskStorage {
Expand Down
5 changes: 5 additions & 0 deletions Source/Shared/Storage/HybridStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ extension HybridStorage: StorageAware {

notifyStorageObservers(about: .remove(key: key))
}

public func removeInMemoryObject(forKey key: Key) throws {
memoryStorage.removeObject(forKey: key)
notifyStorageObservers(about: .removeInMemory(key: key))
}

public func setObject(_ object: Value, forKey key: Key, expiry: Expiry? = nil) throws {
var keyChange: KeyChange<Value>?
Expand Down
5 changes: 5 additions & 0 deletions Source/Shared/Storage/MemoryStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ extension MemoryStorage {
cache.removeObject(forKey: WrappedKey(key))
keys.remove(key)
}

public func removeInMemoryObject(forKey key: Key) throws {
cache.removeObject(forKey: WrappedKey(key))
keys.remove(key)
}

public func entry(forKey key: Key) throws -> Entry<Value> {
guard let capsule = cache.object(forKey: WrappedKey(key)) else {
Expand Down
4 changes: 4 additions & 0 deletions Source/Shared/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public final class Storage<Key: Hashable, Value> {
}

extension Storage: StorageAware {
public func removeInMemoryObject(forKey key: Key) throws {
try self.syncStorage.removeInMemoryObject(forKey: key)
}

public var allKeys: [Key] {
self.syncStorage.allKeys
}
Expand Down
5 changes: 5 additions & 0 deletions Source/Shared/Storage/StorageAware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public protocol StorageAware {
- Parameter key: Unique key to identify the object.
*/
func isExpiredObject(forKey key: Key) throws -> Bool
/**
Removes the object by the given key from cache in memory only.
- Parameter key: Unique key to identify the object.
*/
func removeInMemoryObject(forKey key: Key) throws
}

public extension StorageAware {
Expand Down
3 changes: 2 additions & 1 deletion Source/Shared/Storage/StorageObservationRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ public enum StorageChange<Key: Hashable>: Equatable {
case remove(key: Key)
case removeAll
case removeExpired
case removeInMemory(key: Key)
}

public func == <Key : Hashable>(lhs: StorageChange<Key>, rhs: StorageChange<Key>) -> Bool {
switch (lhs, rhs) {
case (.add(let key1), .add(let key2)), (.remove(let key1), .remove(let key2)):
case (.add(let key1), .add(let key2)), (.remove(let key1), .remove(let key2)), (.removeInMemory(let key1), .removeInMemory(let key2)):
return key1 == key2
case (.removeAll, .removeAll), (.removeExpired, .removeExpired):
return true
Expand Down
7 changes: 7 additions & 0 deletions Source/Shared/Storage/SyncStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ extension SyncStorage: StorageAware {
try innerStorage.removeExpiredObjects()
}
}

public func removeInMemoryObject(forKey key: Key) throws {
try serialQueue.sync {
try self.innerStorage.removeInMemoryObject(forKey: key)
}
}

}

public extension SyncStorage {
Expand Down

0 comments on commit d048bf4

Please sign in to comment.