-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
54 lines (41 loc) · 1.12 KB
/
Contents.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
import UIKit
/**
Cache
- Cache should be able to store
- Cache should be able to update
- cache should be able to delete the a particular key
- cache should be able to retrieve a particular data from cache
- clears the entire cache
*/
protocol Cacheable {
var cache: [String: Any] { get set }
func store(_ key: String, data: Any)
func delete(for key: String)
func getData(for key: String) -> Any
func updateData(for key: String, data: Any)
mutating func flush()
}
extension Cacheable {
mutating func flush() {
self.cache = [:]
}
}
class CustomCache: Cacheable {
var cache: [String : Any] = [:]
// MARK: - Store into cache
func store(_ key: String, data: Any) {
// stores the data into the key
cache["\(key)"] = data
}
func delete(for key: String) {
cache["\(key)"] = nil
}
func getData(for key: String) -> Any {
return cache["\(key)"]
}
func updateData(for key: String, data: Any) {
if cache["\(key)"] != nil {
cache["\(key)"] = data
}
}
}