Skip to content

[FSSDK-11154] chore: add remove method to LRU Cache for CMAB service #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
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
10 changes: 10 additions & 0 deletions Sources/ODP/LruCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ class LruCache<K: Hashable, V> {
}
}

func remove(key: K) {
if maxSize <= 0 { return }
queue.async(flags: .barrier) {
if var item = self.map[key] {
self.removeFromLink(item)
self.map[key] = nil
}
}
}

// read cache contents without order update
func peek(key: K) -> V? {
if maxSize <= 0 { return nil }
Expand Down
60 changes: 60 additions & 0 deletions Tests/OptimizelyTests-Common/LruCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ class LruCacheTests: XCTestCase {
XCTAssertEqual(700, cache.peek(key: 2))
}

func testRemoveKey() {
let cache = LruCache<Int, Int>(size: 3, timeoutInSecs: 1000)

// Save a few items
cache.save(key: 1, value: 100) // [1]
cache.save(key: 2, value: 200) // [1, 2]
cache.save(key: 3, value: 300) // [1, 2, 3]

// Ensure they are saved
XCTAssertEqual(cache.peek(key: 1), 100)
XCTAssertEqual(cache.peek(key: 2), 200)
XCTAssertEqual(cache.peek(key: 3), 300)

// Remove one key
cache.remove(key: 2)

// Check it's gone
XCTAssertNil(cache.peek(key: 2))
XCTAssertNil(cache.lookup(key: 2))

// Ensure others still exist
XCTAssertEqual(cache.peek(key: 1), 100)
XCTAssertEqual(cache.peek(key: 3), 300)

// Remove non-existent key (should not crash or affect others)
cache.remove(key: 999)
XCTAssertEqual(cache.peek(key: 1), 100)
XCTAssertEqual(cache.peek(key: 3), 300)
}


func testSize_zero() {
let cache = LruCache<Int, Int>(size: 0, timeoutInSecs: 1000)

Expand All @@ -132,6 +163,35 @@ class LruCacheTests: XCTestCase {
}
XCTAssertTrue(result, "Concurrent tasks timed out")
}

func testRemoveIsThreadSafe() {
let numThreads = 50
let numIterations = 100

let cache = LruCache<Int, Int>(size: 100, timeoutInSecs: 1000)

// Pre-fill the cache with keys
for i in 0..<(numThreads * numIterations) {
cache.save(key: i, value: i * 10)
}

let result = OTUtils.runConcurrent(count: numThreads, timeoutInSecs: 10) { threadIndex in
let base = threadIndex * numIterations
for i in 0..<numIterations {
let key = base + i
cache.remove(key: key)
XCTAssertNil(cache.lookup(key: key))
}
}

XCTAssertTrue(result, "Concurrent removals timed out or failed")

// Ensure all targeted keys are removed
for i in 0..<(numThreads * numIterations) {
XCTAssertNil(cache.peek(key: i))
}
}


}

Expand Down
Loading