-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathStorageAware.swift
112 lines (96 loc) · 2.74 KB
/
StorageAware.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Foundation
/// A protocol used for saving and loading from storage
public protocol StorageAware {
associatedtype Key: Hashable
associatedtype Value
/**
Get all keys in the storage
*/
var allKeys: [Key] { get }
/**
Get all objects from the storage
*/
var allObjects: [Value] { get }
/**
Tries to retrieve the object from the storage.
- Parameter key: Unique key to identify the object in the cache
- Returns: Cached object or nil if not found
*/
func object(forKey key: Key) throws -> Value
/**
Get cache entry which includes object with metadata.
- Parameter key: Unique key to identify the object in the cache
- Returns: Object wrapper with metadata or nil if not found
*/
func entry(forKey key: Key) throws -> Entry<Value>
/**
Removes the object by the given key.
- Parameter key: Unique key to identify the object.
*/
func removeObject(forKey key: Key) throws
/**
Saves passed object.
- Parameter key: Unique key to identify the object in the cache.
- Parameter object: Object that needs to be cached.
- Parameter expiry: Overwrite expiry for this object only.
*/
func setObject(_ object: Value, forKey key: Key, expiry: Expiry?) throws
/**
Check if an object exist by the given key.
- Parameter key: Unique key to identify the object.
*/
@available(*, deprecated, renamed: "objectExists(forKey:)")
func existsObject(forKey key: Key) throws -> Bool
/**
Check if an object exist by the given key.
- Parameter key: Unique key to identify the object.
*/
func objectExists(forKey key: Key) -> Bool
/**
Removes all objects from the cache storage.
*/
func removeAll() throws
/**
Clears all expired objects.
*/
func removeExpiredObjects() throws
/**
Check if an expired object by the given key.
- 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 {
func object(forKey key: Key) throws -> Value {
return try entry(forKey: key).object
}
func existsObject(forKey key: Key) throws -> Bool {
do {
let _: Value = try object(forKey: key)
return true
} catch {
return false
}
}
func objectExists(forKey key: Key) -> Bool {
do {
let _: Value = try object(forKey: key)
return true
} catch {
return false
}
}
func isExpiredObject(forKey key: Key) throws -> Bool {
do {
let entry = try self.entry(forKey: key)
return entry.expiry.isExpired
} catch {
return true
}
}
}