diff --git a/cacholote/clean.py b/cacholote/clean.py index c24cc9a..2740e6d 100644 --- a/cacholote/clean.py +++ b/cacholote/clean.py @@ -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]] = [] @@ -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 diff --git a/tests/test_60_clean.py b/tests/test_60_clean.py index d624f86..98020a1 100644 --- a/tests/test_60_clean.py +++ b/tests/test_60_clean.py @@ -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()