Skip to content

Commit

Permalink
count expired cached entry
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Jul 24, 2024
1 parent bfa6a94 commit 48eecd0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cacholote/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def expire_cache_entries(
tags: list[str] | None = None,
before: datetime.datetime | None = None,
after: datetime.date | None = None,
) -> None:
) -> int:
now = utils.utcnow()

filters: list[BinaryExpression[bool] | ColumnElement[bool]] = []
Expand All @@ -372,9 +372,12 @@ def expire_cache_entries(
if after is not None:
filters.append(database.CacheEntry.timestamp > after)

count = 0
with config.get().instantiated_sessionmaker() as session:
for cache_entry in session.scalars(
sa.select(database.CacheEntry).filter(*filters)
):
count += 1
cache_entry.expiration = now
database._commit_or_rollback(session)
return count
6 changes: 4 additions & 2 deletions tests/test_60_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,11 @@ def test_expire_cache_entries(
now = cached_now()

# Do not expire
clean.expire_cache_entries(tags=["bar"], before=YESTERDAY, after=TOMORROW)
expired = clean.expire_cache_entries(tags=["bar"], before=YESTERDAY, after=TOMORROW)
assert expired == 0
assert now == cached_now()

# Expire
clean.expire_cache_entries(tags=tags, before=before, after=after)
expired = clean.expire_cache_entries(tags=tags, before=before, after=after)
assert expired == 1
assert now != cached_now()

0 comments on commit 48eecd0

Please sign in to comment.