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
17 changes: 9 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ let package = Package(
.macOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "DataCacheKit",
targets: ["DataCacheKit"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "DataCacheKit",
dependencies: []),
dependencies: ["LRUCache"]),
.testTarget(
name: "DataCacheKitTests",
dependencies: ["DataCacheKit"]),

.target(
name: "LRUCache",
dependencies: []),
.testTarget(
name: "LRUCacheTests",
dependencies: ["LRUCache"]),
]
)
1 change: 1 addition & 0 deletions Sources/DataCacheKit/MemoryCache.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import OSLog
import LRUCache

public actor MemoryCache<Key: Hashable & Sendable, Value: Sendable>: Caching {
public nonisolated let options: Options
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
// https://github.com/swiftlang/swift-corelibs-foundation/blob/25d044f2c4ceb635d9f714f588673fd7a29790c1/Sources/Foundation/NSCache.swift
import os

struct LRUCache<Key: Hashable & Sendable, Value: Sendable>: ~Copyable, Sendable {
var totalCostLimit: Int {
public struct LRUCache<Key: Hashable & Sendable, Value: Sendable>: ~Copyable, Sendable {
public var totalCostLimit: Int {
get { entries.withLock { $0.totalCostLimit } }
nonmutating set { entries.withLock { $0.totalCostLimit = newValue } }
}

var countLimit: Int {
public var countLimit: Int {
get { entries.withLock { $0.countLimit } }
nonmutating set { entries.withLock { $0.countLimit = newValue } }
}

private let entries = OSAllocatedUnfairLock<Entiries>(initialState: .init())

func value(forKey key: Key) -> Value? {
public init() {}

public func value(forKey key: Key) -> Value? {
entries.withLock { entries in
entries.values[key]?.value
guard let entry = entries.values[key] else { return nil }
entries.remove(entry)
entries.insert(entry)
return entry.value
}
}

func setValue(_ value: Value, forKey key: Key) {
public func setValue(_ value: Value, forKey key: Key) {
setValue(value, forKey: key, cost: 0)
}

func setValue(_ value: Value, forKey key: Key, cost: Int) {
public func setValue(_ value: Value, forKey key: Key, cost: Int) {
entries.withLock { entries in
entries.set(value, forKey: key, cost: cost)
}
}

func removeValue(forKey key: Key) {
public func removeValue(forKey key: Key) {
entries.withLock { entries in
if let entry = entries.values.removeValue(forKey: key) {
entries.totalCost -= entry.cost
Expand All @@ -39,15 +44,15 @@ struct LRUCache<Key: Hashable & Sendable, Value: Sendable>: ~Copyable, Sendable
}
}

func removeAllValues() {
public func removeAllValues() {
entries.withLock { entiries in
entiries.removeAll()
}
}
}

extension LRUCache {
subscript (_ key: Key, cost cost: Int = 0) -> Value? {
public subscript (_ key: Key, cost cost: Int = 0) -> Value? {
get {
value(forKey: key)
}
Expand Down Expand Up @@ -155,6 +160,9 @@ private extension LRUCache {
if entry === tail {
tail = oldPrev
}

entry.next = nil
entry.prev = nil
}

mutating func removeAll() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Testing
@testable import DataCacheKit
@testable import LRUCache
import Foundation

struct LRUCacheTests {
Expand Down Expand Up @@ -75,6 +75,46 @@ struct LRUCacheTests {
#expect(cache[3] == 3)
}

@Test
func testCountLimitAccess() {
let cache = LRUCache<Int, Int>()

// Given
cache.countLimit = 2

// When
cache[1] = 1
cache[2] = 2

_ = cache[1]
cache[3] = 3

// Then
#expect(cache[1] == 1)
#expect(cache[2] == nil)
#expect(cache[3] == 3)
}

@Test
func testCountLimitAccessNSCache() {
let cache = NSCacheWrapper<Int, Int>()

// Given
cache.countLimit = 2

// When
cache[1] = 1
cache[2] = 2

_ = cache[1]
cache[3] = 3

// Then
#expect(cache[1] == 1)
#expect(cache[2] == nil)
#expect(cache[3] == 3)
}

@Test
func testCountLimitWithCost1() {
let cache = LRUCache<Int, Int>()
Expand Down