-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheProtocols.swift
127 lines (109 loc) · 3.71 KB
/
CacheProtocols.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// CacheProtocols.swift
// Pods-PracticeCache_Example
//
// Created by Vassily on 2018/11/2.
//
import Foundation
// MARK: - Cache
public protocol Cacheable {
associatedtype M: CacheStandard
associatedtype D: CacheStandard & CacheAsyncStandard where M.Key == D.Key, M.Value == D.Value
init(memoryCache: M, diskCache: D)
}
public protocol CacheStandard {
associatedtype Value: Codable
associatedtype Key: Hashable
func containsObject(key: Key) -> Bool
mutating func query(key: Key) -> Value?
mutating func save(value: Value, for key: Key)
mutating func remove(key: Key)
mutating func removeAll()
}
public protocol CacheAsyncStandard {
associatedtype Value: Codable
associatedtype Key: Hashable
func containsObject(key: Key, _ result: @escaping ((_ key: Key, _ contain: Bool) -> Void))
mutating func query(key: Key, _ result: @escaping ((_ key: Key, _ value: Value?) -> Void))
mutating func save(value: Value, for key: Key, _ result: @escaping (()->Void))
mutating func remove(key: Key, _ result: @escaping ((_ key: Key) -> Void))
mutating func removeAll(_ result: @escaping (()->Void))
}
// MARK: - Lock
protocol Lock {
func lock()
func unLock()
}
// MARK: - Trim
protocol AutoTrimable: class {
var countLimit: Int { get }
var costLimit: Int { get }
var ageLimit: TimeInterval { get }
var autoTrimInterval: TimeInterval { get }
var shouldAutoTrim: Bool { get set }
func trimToAge(_ age: TimeInterval)
func trimToCost(_ cost: Int)
func trimToCount(_ count: Int)
}
extension AutoTrimable {
func autoTrim() {
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + autoTrimInterval) {
self.trimToAge(self.ageLimit)
self.trimToCost(self.costLimit)
self.trimToCount(self.countLimit)
if self.shouldAutoTrim { self.autoTrim() }
}
}
}
// MARK: - Node and Link
protocol NodeStandard: class, Equatable, CustomStringConvertible {
associatedtype Key where Key: Hashable
associatedtype Value
var key: Key { get }
var value: Value { get }
var pre: Self? { get }
var next: Self? { get }
init(key: Key, value: Value, pre: Self?, next: Self?)
}
extension NodeStandard {
public var description: String {
guard let next = next else { return "\(value)" }
return "\(value) -> \(next)"
}
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.key == rhs.key
}
}
protocol LinkedNodeListIndexStandard: Comparable {
associatedtype Node: NodeStandard
var node: Node? { get }
init(node: Node?)
}
extension LinkedNodeListIndexStandard {
static public func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs.node, rhs.node) {
case let (left?, right?):
return left.next === right.next
case (nil, nil):
return true
default:
return false
}
}
static public func < (lhs: Self, rhs: Self) -> Bool {
guard lhs != rhs else { return false }
let nodes = sequence(first: lhs.node, next: { $0?.next })
return nodes.contains(where: { $0 === rhs.node })
}
}
protocol LinkedNodeListStandard: BidirectionalCollection where Index: LinkedNodeListIndexStandard {
associatedtype Key: Hashable
associatedtype Value
subscript(key: Key) -> Value? { mutating get set }
func contains(where predicate: (Key) throws -> Bool) rethrows -> Bool
mutating func push(_ value: Value, for key: Key)
mutating func remove(for key: Key) -> AnyLinkNode<Key, Value>?
mutating func removeAll()
mutating func removeTrail() -> AnyLinkNode<Key, Value>?
mutating func value(for key: Key) -> Value?
}