Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions Sources/LRUCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,40 @@ public final class LRUCache<Key: Hashable & Sendable, Value>: @unchecked Sendabl
private var _totalCostLimit: Int
private unowned(unsafe) var head: Container?
private unowned(unsafe) var tail: Container?
private let clearsOnMemoryPressure: Bool
private let lock: NSLock = .init()

#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
private let memoryPressureSource: DispatchSourceMemoryPressure
private let memoryPressureSource: DispatchSourceMemoryPressure?
#endif

/// Initialize the cache with the specified `totalCostLimit` and `countLimit`
public init(totalCostLimit: Int = .max, countLimit: Int = .max) {
public init(
totalCostLimit: Int = .max,
countLimit: Int = .max,
clearsOnMemoryPressure: Bool = true
) {
self._totalCostLimit = totalCostLimit
self._countLimit = countLimit
self.clearsOnMemoryPressure = clearsOnMemoryPressure

#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
self.memoryPressureSource = DispatchSource
.makeMemoryPressureSource(eventMask: [.warning, .critical], queue: .global())
memoryPressureSource.setEventHandler { [weak self] in
self?.removeAll()
if clearsOnMemoryPressure {
self.memoryPressureSource = DispatchSource
.makeMemoryPressureSource(eventMask: [.warning, .critical], queue: .global())
memoryPressureSource?.setEventHandler { [weak self] in
self?.removeAll()
}
memoryPressureSource?.resume()
} else {
self.memoryPressureSource = nil
}
memoryPressureSource.resume()
#endif

#if !os(WASI)
registerNotifications(for: .default)
if clearsOnMemoryPressure {
registerNotifications(for: .default)
}
#endif
}

Expand Down Expand Up @@ -101,7 +113,7 @@ public final class LRUCache<Key: Hashable & Sendable, Value>: @unchecked Sendabl
token.map(notificationCenter.removeObserver)

#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
self.memoryPressureSource.cancel()
self.memoryPressureSource?.cancel()
#endif
}

Expand Down
15 changes: 15 additions & 0 deletions Tests/LRUCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ class LRUCacheTests: XCTestCase {
XCTAssert(cache.isEmpty)
}

@available(*, deprecated, message: "Obsolete")
func testClearsOnMemoryPressureDisabled() {
let cache = LRUCache<Int, Int>(clearsOnMemoryPressure: false)
for i in 0 ..< 100 {
cache.setValue(i, forKey: i)
}
XCTAssertEqual(cache.count, 100)
NotificationCenter.default.post(
name: LRUCacheMemoryWarningNotification,
object: nil
)
// Cache should NOT be cleared when clearsOnMemoryPressure is false
XCTAssertEqual(cache.count, 100)
}

@available(*, deprecated, message: "Obsolete")
func testNotificationObserverIsRemoved() {
#if !os(Linux)
Expand Down