-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathDefaultLeafCache.swift
More file actions
110 lines (98 loc) · 3.85 KB
/
DefaultLeafCache.swift
File metadata and controls
110 lines (98 loc) · 3.85 KB
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
import NIOConcurrencyHelpers
import NIO
/// `@unchecked Sendable` because uses locks to guarantee Sendability.
public final class DefaultLeafCache: SynchronousLeafCache, @unchecked Sendable {
// MARK: - Public - `LeafCache` Protocol Conformance
var __isEnabled = true
/// Global setting for enabling or disabling the cache
public var _isEnabled: Bool {
get {
self.lock.withLock {
self.__isEnabled
}
}
set(newValue) {
self.lock.withLock {
self.__isEnabled = newValue
}
}
}
/// Global setting for enabling or disabling the cache
public var isEnabled: Bool {
get {
self._isEnabled
}
set(newValue) {
self._isEnabled = newValue
}
}
/// Current count of cached documents
public var count: Int { self.lock.withLock { cache.count } }
/// Initializer
public init() {
self.lock = .init()
self.cache = [:]
}
/// - Parameters:
/// - document: The `LeafAST` to store
/// - loop: `EventLoop` to return futures on
/// - replace: If a document with the same name is already cached, whether to replace or not.
/// - Returns: The document provided as an identity return
public func insert(
_ document: LeafAST,
on loop: EventLoop,
replace: Bool = false
) -> EventLoopFuture<LeafAST> {
// future fails if caching is enabled
guard isEnabled else { return loop.makeSucceededFuture(document) }
self.lock.lock()
defer { self.lock.unlock() }
// return an error if replace is false and the document name is already in cache
switch (self.cache.keys.contains(document.name),replace) {
case (true, false): return loop.makeFailedFuture(LeafError(.keyExists(document.name)))
default: self.cache[document.name] = document
}
return loop.makeSucceededFuture(document)
}
/// - Parameters:
/// - documentName: Name of the `LeafAST` to try to return
/// - loop: `EventLoop` to return futures on
/// - Returns: `EventLoopFuture<LeafAST?>` holding the `LeafAST` or nil if no matching result
public func retrieve(
documentName: String,
on loop: EventLoop
) -> EventLoopFuture<LeafAST?> {
guard isEnabled == true else { return loop.makeSucceededFuture(nil) }
self.lock.lock()
defer { self.lock.unlock() }
return loop.makeSucceededFuture(self.cache[documentName])
}
/// - Parameters:
/// - documentName: Name of the `LeafAST` to try to purge from the cache
/// - loop: `EventLoop` to return futures on
/// - Returns: `EventLoopFuture<Bool?>` - If no document exists, returns nil. If removed,
/// returns true. If cache can't remove because of dependencies (not yet possible), returns false.
public func remove(
_ documentName: String,
on loop: EventLoop
) -> EventLoopFuture<Bool?> {
guard isEnabled == true else { return loop.makeFailedFuture(LeafError(.cachingDisabled)) }
self.lock.lock()
defer { self.lock.unlock() }
guard self.cache[documentName] != nil else { return loop.makeSucceededFuture(nil) }
self.cache[documentName] = nil
return loop.makeSucceededFuture(true)
}
// MARK: - Internal Only
internal let lock: NIOLock
internal var cache: [String: LeafAST]
/// Blocking file load behavior
internal func retrieve(documentName: String) throws -> LeafAST? {
guard isEnabled == true else { throw LeafError(.cachingDisabled) }
self.lock.lock()
defer { self.lock.unlock() }
let result = self.cache[documentName]
guard result != nil else { throw LeafError(.noValueForKey(documentName)) }
return result
}
}