Skip to content

Commit 988602c

Browse files
authored
Merge pull request #280 from asam139/feature/all_keys_and_objects
Added allKeys and allObjects computed vars for storage with sense
2 parents a427e7a + 27c0a86 commit 988602c

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

Source/Shared/Storage/DiskStorage.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ final public class DiskStorage<Key: Hashable, Value> {
5858
}
5959

6060
extension DiskStorage: StorageAware {
61+
public var allKeys: [Key] { [] }
62+
63+
public var allObjects: [Value] { [] }
64+
6165
public func entry(forKey key: Key) throws -> Entry<Value> {
6266
let filePath = makeFilePath(for: key)
6367
let data = try Data(contentsOf: URL(fileURLWithPath: filePath))

Source/Shared/Storage/HybridStorage.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public final class HybridStorage<Key: Hashable, Value> {
2626
}
2727

2828
extension HybridStorage: StorageAware {
29+
public var allKeys: [Key] {
30+
memoryStorage.allKeys
31+
}
32+
33+
public var allObjects: [Value] {
34+
memoryStorage.allObjects
35+
}
36+
2937
public func entry(forKey key: Key) throws -> Entry<Value> {
3038
do {
3139
return try memoryStorage.entry(forKey: key)

Source/Shared/Storage/MemoryStorage.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public class MemoryStorage<Key: Hashable, Value>: StorageAware {
3131
}
3232

3333
extension MemoryStorage {
34+
public var allKeys: [Key] {
35+
Array(keys)
36+
}
37+
38+
public var allObjects: [Value] {
39+
allKeys.compactMap { try? object(forKey: $0) }
40+
}
41+
3442
public func setObject(_ object: Value, forKey key: Key, expiry: Expiry? = nil) {
3543
let capsule = MemoryCapsule(value: object, expiry: .date(expiry?.date ?? config.expiry.date))
3644
cache.setObject(capsule, forKey: WrappedKey(key))

Source/Shared/Storage/Storage.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public final class Storage<Key: Hashable, Value> {
4343
}
4444

4545
extension Storage: StorageAware {
46+
public var allKeys: [Key] {
47+
self.syncStorage.allKeys
48+
}
49+
50+
public var allObjects: [Value] {
51+
self.syncStorage.allObjects
52+
}
53+
4654
public func entry(forKey key: Key) throws -> Entry<Value> {
4755
return try self.syncStorage.entry(forKey: key)
4856
}

Source/Shared/Storage/StorageAware.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import Foundation
44
public protocol StorageAware {
55
associatedtype Key: Hashable
66
associatedtype Value
7+
8+
/**
9+
Get all keys in the storage
10+
*/
11+
var allKeys: [Key] { get }
12+
13+
/**
14+
Get all objects from the storage
15+
*/
16+
var allObjects: [Value] { get }
17+
718
/**
819
Tries to retrieve the object from the storage.
920
- Parameter key: Unique key to identify the object in the cache

Source/Shared/Storage/SyncStorage.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,27 @@ public class SyncStorage<Key: Hashable, Value> {
1414
}
1515

1616
extension SyncStorage: StorageAware {
17+
public var allKeys: [Key] {
18+
var keys: [Key]!
19+
serialQueue.sync {
20+
keys = self.innerStorage.allKeys
21+
}
22+
return keys
23+
}
24+
25+
public var allObjects: [Value] {
26+
var objects: [Value]!
27+
serialQueue.sync {
28+
objects = self.innerStorage.allObjects
29+
}
30+
return objects
31+
}
32+
1733
public func entry(forKey key: Key) throws -> Entry<Value> {
1834
var entry: Entry<Value>!
1935
try serialQueue.sync {
2036
entry = try innerStorage.entry(forKey: key)
2137
}
22-
2338
return entry
2439
}
2540

0 commit comments

Comments
 (0)