Skip to content
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

rename EvictionCause to RemovalCause #33

Merged
merged 2 commits into from
Sep 29, 2023
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
2 changes: 1 addition & 1 deletion src/cacheout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__version__ = "0.14.1"

from .cache import Cache, EvictionCause
from .cache import Cache, RemovalCause
from .fifo import FIFOCache
from .lfu import LFUCache
from .lifo import LIFOCache
Expand Down
24 changes: 12 additions & 12 deletions src/cacheout/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
UNSET = object()


class EvictionCause(Enum):
class RemovalCause(Enum):
"""
An enum to represent the cause for the eviction of a cache entry.
An enum to represent the cause for the removal of a cache entry.

- DELETE: indicates that the cache entry was deleted by delete() or delete_many() explicitly.
- SET: indicates that the cache entry was replaced with a new value by set() or set_many().
Expand Down Expand Up @@ -84,7 +84,7 @@ def __init__(
ttl: T_TTL = 0,
timer: t.Callable[[], T_TTL] = time.time,
default: t.Any = None,
on_delete: t.Optional[t.Callable[[t.Hashable, t.Any, EvictionCause], None]] = None,
on_delete: t.Optional[t.Callable[[t.Hashable, t.Any, RemovalCause], None]] = None,
):
self.maxsize = maxsize
self.ttl = ttl
Expand Down Expand Up @@ -239,7 +239,7 @@ def _get(self, key: t.Hashable, default: t.Any = None) -> t.Any:
value = self._cache[key]

if self.expired(key):
self._delete(key, EvictionCause.EXPIRED)
self._delete(key, RemovalCause.EXPIRED)
raise KeyError
except KeyError:
if default is None:
Expand Down Expand Up @@ -334,7 +334,7 @@ def _set(self, key: t.Hashable, value: t.Any, ttl: t.Optional[T_TTL] = None) ->
if key not in self._cache:
self.evict()

self._delete(key, EvictionCause.SET)
self._delete(key, RemovalCause.SET)
self._cache[key] = value

if ttl and ttl > 0:
Expand Down Expand Up @@ -367,9 +367,9 @@ def delete(self, key: t.Hashable) -> int:
int: ``1`` if key was deleted, ``0`` if key didn't exist.
"""
with self._lock:
return self._delete(key, EvictionCause.DELETE)
return self._delete(key, RemovalCause.DELETE)

def _delete(self, key: t.Hashable, cause: EvictionCause) -> int:
def _delete(self, key: t.Hashable, cause: RemovalCause) -> int:
count = 0

try:
Expand Down Expand Up @@ -414,7 +414,7 @@ def _delete_many(self, iteratee: T_FILTER) -> int:
with self._lock:
keys = self._filter_keys(iteratee)
for key in keys:
count += self._delete(key, EvictionCause.DELETE)
count += self._delete(key, RemovalCause.DELETE)
return count

def delete_expired(self) -> int:
Expand All @@ -439,7 +439,7 @@ def _delete_expired(self) -> int:

for key, expiration in expire_times.items():
if expiration <= expires_on:
count += self._delete(key, EvictionCause.EXPIRED)
count += self._delete(key, RemovalCause.EXPIRED)
return count

def expired(self, key: t.Hashable, expires_on: t.Optional[T_TTL] = None) -> bool:
Expand Down Expand Up @@ -512,7 +512,7 @@ def evict(self) -> int:
with self._lock:
while self.full():
try:
self._popitem(EvictionCause.FULL)
self._popitem(RemovalCause.FULL)
except KeyError: # pragma: no cover
break
count += 1
Expand All @@ -530,9 +530,9 @@ def popitem(self) -> t.Tuple[t.Hashable, t.Any]:
"""
with self._lock:
self._delete_expired()
return self._popitem(EvictionCause.POPITEM)
return self._popitem(RemovalCause.POPITEM)

def _popitem(self, cause: EvictionCause):
def _popitem(self, cause: RemovalCause):
try:
key = next(self)
except StopIteration:
Expand Down
4 changes: 2 additions & 2 deletions src/cacheout/lfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import Counter
import typing as t

from .cache import T_TTL, Cache, EvictionCause
from .cache import T_TTL, Cache, RemovalCause


class LFUCache(Cache):
Expand Down Expand Up @@ -59,7 +59,7 @@ def add(self, key: t.Hashable, value: t.Any, ttl: t.Optional[T_TTL] = None) -> N

add.__doc__ = Cache.add.__doc__

def _delete(self, key: t.Hashable, cause: EvictionCause) -> int:
def _delete(self, key: t.Hashable, cause: RemovalCause) -> int:
count = super()._delete(key, cause)

try:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from cacheout import Cache, EvictionCause
from cacheout import Cache, RemovalCause


parametrize = pytest.mark.parametrize
Expand Down Expand Up @@ -722,24 +722,24 @@ def on_delete(key, value, cause):
cache.on_delete = on_delete
cache.set("DELETE", 1)
cache.delete("DELETE")
assert log == f"DELETE:1 {EvictionCause.DELETE.value}"
assert log == f"DELETE:1 {RemovalCause.DELETE.value}"

cache.set("SET", 1)
cache.set("SET", 2)
assert log == f"SET:1 {EvictionCause.SET.value}"
assert log == f"SET:1 {RemovalCause.SET.value}"

cache.clear()
cache.set("POPITEM", 1)
cache.popitem()
assert log == f"POPITEM:1 {EvictionCause.POPITEM.value}"
assert log == f"POPITEM:1 {RemovalCause.POPITEM.value}"

cache.set("EXPIRED", 1, ttl=1)
timer.time = 1
cache.delete_expired()
assert log == f"EXPIRED:1 {EvictionCause.EXPIRED.value}"
assert log == f"EXPIRED:1 {RemovalCause.EXPIRED.value}"

cache.clear()
cache.maxsize = 1
cache.set("FULL", 1)
cache.set("OVERFLOW", 2)
assert log == f"FULL:1 {EvictionCause.FULL.value}"
assert log == f"FULL:1 {RemovalCause.FULL.value}"