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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ lint = [
"ruff>=0.16.9",
]
maturin = [
"maturin == 1.14.1 ; sys_platform != 'linux'",
"maturin[patchelf] == 1.14.1 ; sys_platform == 'linux'",
"maturin == 1.15.0 ; sys_platform != 'linux'",
"maturin[patchelf] == 1.15.0 ; sys_platform == 'linux'",
]
test = [
"hypothesis >= 6.165.0",
Expand Down
9 changes: 4 additions & 5 deletions src/policies/ttlpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,11 @@ impl PolicyExt for TTLPolicy {
}

fn evict(&mut self, shared: &Self::Shared) -> pyo3::PyResult<Self::Handle> {
let front = self.entries.front();
if front.is_none() {
return Err(new_py_error!(PyKeyError, ()));
}
self.expire(shared.generation_version());

let front = unsafe { front.unwrap_unchecked() };
let Some(front) = self.entries.front() else {
return Err(new_py_error!(PyKeyError, ()));
};

let eq = |index: &usize| Ok::<_, pyo3::PyErr>(*index - self.front_offset == 0);
let removed_key_table = self.table.remove_entry(front.key().hash(), eq).unwrap();
Expand Down
9 changes: 5 additions & 4 deletions src/policies/vttlpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,12 @@ impl PolicyExt for VTTLPolicy {
}

fn evict(&mut self, shared: &Self::Shared) -> pyo3::PyResult<Self::Handle> {
self.expire(shared.generation_version());

{
let front_cursor = self
.heap
.front()
.ok_or_else(|| new_py_error!(PyKeyError, "cache is empty"))?;
let Some(front_cursor) = self.heap.front() else {
return Err(new_py_error!(PyKeyError, "cache is empty"));
};

self.table
.remove_entry(unsafe { front_cursor.element().key.hash() }, |x| {
Expand Down
33 changes: 33 additions & 0 deletions tests/test_impls.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,23 @@ def test_popitem_removes_oldest(self):
assert key == 10
assert value == "x"

def test_popitem_expire_keys(self):
"""popitem() must always expire all expired keys, and return the oldest *alive* inserted entry."""
cache = self.create_cache(5, global_ttl=0.5)

cache.insert(1, "a")
cache.insert(2, "b")

time.sleep(0.5)

cache.insert(3, "c")
cache.insert(4, "d")
cache.insert(5, "e")

key, value = cache.popitem()
assert key == 3
assert value == "c"

def test_popitem_successive_calls_follow_fifo(self):
"""Successive popitem() calls must yield keys in insertion order."""
insertion_order = [(1, "a"), (2, "b"), (3, "c"), (4, "d")]
Expand Down Expand Up @@ -1932,6 +1949,22 @@ def test_popitem_on_empty_raises(self):
with pytest.raises(KeyError):
c.popitem()

def test_popitem_expire_keys(self):
"""popitem() must always expire all expired keys, and return the soonest expiring *alive* inserted entry."""
cache = self.create_cache(5)

cache.insert(1, "a", ttl=0.5)
cache.insert(2, "b", ttl=2)
cache.insert(3, "c", ttl=None)
cache.insert(4, "d", ttl=0.1)
cache.insert(5, "e", ttl=3)

time.sleep(0.5)

key, value = cache.popitem()
assert key == 2
assert value == "b"

def test_popitem_follows_expiry_after_ttl_changes(self):
c = cachebox.VTTLCache(10)
c.insert("a", 1, ttl=100)
Expand Down
56 changes: 28 additions & 28 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading