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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if canImport(os)
import os
#endif
import Synchronization
/// Base protocol used for global operations over generic type.
private protocol _HeavyCacheBase: AnyObject {
/// Clear the cache.
func removeAll()
}
/// All heavy-weight caches are available as a global, to support mass eviction.
@TaskLocal private var allHeavyCaches = LockedValue<[WeakRef<any _HeavyCacheBase>]>([])
// MARK: Global Operations
public func clearAllHeavyCaches() {
allHeavyCaches.withLock { allHeavyCaches in
for idx in allHeavyCaches.indices.reversed() {
if let cache = allHeavyCaches[idx].instance {
cache.removeAll()
} else {
allHeavyCaches.remove(at: idx)
}
}
}
}
public func withHeavyCacheGlobalState<T>(isolated: Bool = true, _ body: () throws -> T) rethrows -> T {
if isolated {
try $allHeavyCaches.withValue(.init([])) {
try body()
}
} else {
try body()
}
}
@_spi(Testing) public func withHeavyCacheGlobalState<T>(isolated: Bool = true, _ body: () async throws -> T) async rethrows -> T {
if isolated {
try await $allHeavyCaches.withValue(.init([])) {
try await body()
}
} else {
try await body()
}
}
/// A cache designed for holding few, relatively heavy-weight objects.
///
/// This cache is specifically designed for holding a limited number of objects (usually less than 100) which are expensive enough to merit particular attention in terms of being purgeable under memory pressure, evictable in-mass, or cached with more complex parameters like time-to-live (TTL).
public final class HeavyCache<Key: Hashable & Sendable, Value>: _HeavyCacheBase, KeyValueStorage, @unchecked Sendable {
public typealias HeavyCacheClock = SuspendingClock
/// Controls the non-deterministic eviction policy of the cache. Note that this is distinct from deterministic _pruning_ (due to TTL or size limits).
public enum EvictionPolicy: Sendable {
/// Never evict due to memory pressure.
case never
/// Default `NSCache` eviction behaviors.
case `default`(totalCostLimit: Int?, willEvictCallback: (@Sendable (Value) -> Void)? = nil)
}
fileprivate final class Entry {
/// The actual value.
let value: Value
/// The last access timestamp.
var accessTime: HeavyCacheClock.Instant
init(_ value: Value, _ accessTime: HeavyCacheClock.Instant) {
self.value = value
self.accessTime = accessTime
}
}
/// The lock to protect shared instance state.
private let stateLock = SWBMutex(())
/// The underlying cache.
private let _cache: any HeavyCacheImpl<Key, Entry>
/// The set of keys potentially in the cache (not necessarily, since the cache can itself evict).
///
/// The underlying cache doesn't expose access to the full contents, so we need to duplicate this information.
private var _keys = Set<Key>()
/// The current time-to-live of this cache, if enabled.
private var _timeToLive: Duration? = nil
/// The maximum size of the cache.
private var _maximumSize: Int? = nil
/// The timer used to enforce TTL.
private var _timer: Task<Void, any Error>? = nil
/// Create a new cache instance.
public init(maximumSize: Int? = nil, timeToLive: Duration? = nil, evictionPolicy: EvictionPolicy = .default(totalCostLimit: nil, willEvictCallback: nil)) {
switch evictionPolicy {
case .never:
self._cache = Registry<Key, Entry>()
case .default(let totalCostLimit, let willEvictCallback):
let evictionCallback: (@Sendable (Entry) -> Void)?
if let willEvictCallback {
evictionCallback = { entry in
willEvictCallback(entry.value)
}
} else {
evictionCallback = nil
}
self._cache = Cache<Key, Entry>(willEvictCallback: evictionCallback, totalCostLimit: totalCostLimit)
}
allHeavyCaches.withLock { allHeavyCaches in
allHeavyCaches.append(WeakRef(self))
}
self.maximumSize = maximumSize
self.timeToLive = timeToLive
}
deinit {
_timer?.cancel()
stateLock.withLock {
for waiter in _expirationWaiters {
waiter.resume()
}
_expirationWaiters = []
}
}
// MARK: Cache Operations
/// The number of items in the cache.
///
/// Due to the implementation details, this may overestimate the number of active items, if some items have been recently evicted.
public var count: Int {
return stateLock.withLock { _keys.count }
}
/// Clear all items in the cache.
public func removeAll() {
stateLock.withLock {
_cache.removeAll()
_keys.removeAll()
}
}
/// Get the value for a key, or compute it if missing.
///
/// This function is thread-safe, but may allow computing the value multiple times in case of a race.
public func getOrInsert(_ key: Key, _ body: () throws -> Value) rethrows -> Value {
return try stateLock.withLock {
let entry = try _cache.getOrInsert(key) {
return Entry(try body(), currentTime())
}
_keys.insert(key)
entry.accessTime = currentTime()
_pruneCache()
return entry.value
}
}
/// Subscript access to the cache.
public subscript(_ key: Key) -> Value? {
get {
return stateLock.withLock {
if let entry = _cache[key] {
entry.accessTime = currentTime()
return entry.value
}
return nil
}
}
set {
stateLock.withLock {
if let newValue {
let entry = Entry(newValue, currentTime())
_cache[key] = entry
_keys.insert(key)
_pruneCache()
} else {
_cache.remove(key)
_keys.remove(key)
}
}
}
}
/// Check whether a given key is present in the cache.
public func contains(_ key: Key) -> Bool {
return self[key] != nil
}
/// Prune the cache following an insert.
///
/// This method is expected to be called on `queue`.
private func _pruneCache() {
// Enforce the cache maximum size.
guard let max = maximumSize else { return }
// Prune one item at a time. This is not efficient for pruning large numbers of items, but that is not the intended use case currently.
//
// We take some care to make sure we drop keys already evicted by the underlying cache before anything else.
whileLoop:
while _keys.count > max {
// Prune the oldest entry.
var oldest: (key: Key, entry: Entry)? = nil
for key in _keys {
guard let entry = _cache[key] else {
_keys.remove(key)
_cache.remove(key)
continue whileLoop
}
if oldest == nil || oldest!.entry.accessTime > entry.accessTime {
oldest = (key, entry)
}
}
if let oldest {
_keys.remove(oldest.key)
_cache.remove(oldest.key)
}
}
}
/// Prune the cache based on the TTL value.
///
/// This method is expected to be called on `queue`.
private func _pruneForTTL() {
guard let ttl = _timeToLive else { return }
let time = currentTime()
var keysToRemove = [Key]()
for key in _keys {
guard let entry = _cache[key] else {
keysToRemove.append(key)
continue
}
if time - entry.accessTime > ttl {
keysToRemove.append(key)
}
}
for key in keysToRemove {
_keys.remove(key)
_cache.remove(key)
}
for waiter in _expirationWaiters {
waiter.resume()
}
_expirationWaiters = []
}
// MARK: Cache Tuning Parameters
/// Set the maximum size of the cache, beyond which it will be pruned.
///
/// When a maximum size is configured, the cache will always behave in an LRU manner.
public var maximumSize: Int? {
get {
return _maximumSize
}
set {
stateLock.withLock {
_maximumSize = newValue
_pruneCache()
}
}
}
/// Set the time-to-live for items in the cache.
///
/// The time-to-live is measured against the last access time of the item.
public var timeToLive: Duration? {
get {
return _timeToLive
}
set {
stateLock.withLock {
_timeToLive = newValue
// Install the TTL timer.
if let ttl = _timeToLive {
// We always schedule at the requested TTL, on average an object will live 50% longer than the TTL.
_timer?.cancel()
_timer = Task { [weak self] in
while !Task.isCancelled {
if let self = self {
self.preventExpiration {
self.stateLock.withLock {
self._pruneForTTL()
}
}
}
try await Task.sleep(for: ttl)
}
}
} else {
_timer?.cancel()
_timer = nil
}
}
}
}
private let _preventExpirationLock = Lock()
private var _expirationWaiters: [CheckedContinuation<Void, Never>] = []
private var _currentTimeTestingOverride: HeavyCacheClock.Instant?
private func currentTime() -> HeavyCacheClock.Instant {
_currentTimeTestingOverride ?? .now
}
}
extension HeavyCache {
/// Allows freezing the current time as seen by the object, for TTL pruning testing purposes.
@_spi(Testing) @discardableResult public func setTime(instant: HeavyCacheClock.Instant?) -> HeavyCacheClock.Instant {
stateLock.withLock {
_currentTimeTestingOverride = instant
return instant ?? .now
}
}
/// Waits until the next time pruning for TTL occurs.
@_spi(Testing) public func waitForExpiration() async {
await withCheckedContinuation { continuation in
stateLock.withLock {
_expirationWaiters.append(continuation)
}
}
}
/// Performs the given body while preventing TTL-based cache expiration.
@_spi(Testing) public func preventExpiration(_ body: @Sendable () throws -> Void) rethrows {
try _preventExpirationLock.withLock(body)
}
}
extension HeavyCache.Entry: CacheableValue where Value: CacheableValue {
var cost: Int {
self.value.cost
}
}
private protocol HeavyCacheImpl<Key, Value>: Sendable, KeyValueStorage, _HeavyCacheBase {
subscript(_ key: Key) -> Value? { get set }
func remove(_ key: Key)
}
extension Cache: HeavyCacheImpl {
}
extension Registry: HeavyCacheImpl {
func remove(_ key: K) {
_ = removeValue(forKey: key)
}
}
|