Skip to content

Commit

Permalink
created_at and updated_at columns
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Jul 24, 2024
1 parent fe57496 commit 1002b66
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cacholote/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
for cache_entry in session.scalars(
sa.select(database.CacheEntry)
.filter(*filters)
.order_by(database.CacheEntry.timestamp.desc())
.order_by(database.CacheEntry.updated_at.desc())
):
try:
return _decode_and_update(session, cache_entry, settings)
Expand Down
12 changes: 8 additions & 4 deletions cacholote/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,13 @@ def _get_method_sorters(
) -> list[sa.orm.InstrumentedAttribute[Any]]:
sorters: list[sa.orm.InstrumentedAttribute[Any]] = []
if method == "LRU":
sorters.extend([database.CacheEntry.timestamp, database.CacheEntry.counter])
sorters.extend(
[database.CacheEntry.updated_at, database.CacheEntry.counter]
)
elif method == "LFU":
sorters.extend([database.CacheEntry.counter, database.CacheEntry.timestamp])
sorters.extend(
[database.CacheEntry.counter, database.CacheEntry.updated_at]
)
else:
raise ValueError(f"{method=}")
sorters.append(database.CacheEntry.expiration)
Expand Down Expand Up @@ -368,9 +372,9 @@ def expire_cache_entries(
if tags is not None:
filters.append(database.CacheEntry.tag.in_(tags))
if before is not None:
filters.append(database.CacheEntry.timestamp < before)
filters.append(database.CacheEntry.created_at < before)
if after is not None:
filters.append(database.CacheEntry.timestamp > after)
filters.append(database.CacheEntry.created_at > after)

count = 0
with config.get().instantiated_sessionmaker() as session:
Expand Down
17 changes: 11 additions & 6 deletions cacholote/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ class CacheEntry(Base):
key = sa.Column(sa.String(32))
expiration = sa.Column(sa.DateTime, default=_DATETIME_MAX)
result = sa.Column(sa.JSON)
timestamp = sa.Column(
sa.DateTime,
default=utils.utcnow,
onupdate=utils.utcnow,
)
created_at = sa.Column(sa.DateTime, default=utils.utcnow)
updated_at = sa.Column(sa.DateTime, default=utils.utcnow, onupdate=utils.utcnow)
counter = sa.Column(sa.Integer)
tag = sa.Column(sa.String)

Expand All @@ -53,7 +50,15 @@ def _result_as_string(self) -> str:
return json.dumps(self.result)

def __repr__(self) -> str:
public_attrs = ("id", "key", "expiration", "timestamp", "counter", "tag")
public_attrs = (
"id",
"key",
"expiration",
"created_at",
"updated_at",
"counter",
"tag",
)
public_attrs_repr = ", ".join(
[f"{attr}={getattr(self, attr)!r}" for attr in public_attrs]
)
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ channels:
# - package2
# DO NOT EDIT ABOVE THIS LINE, ADD DEPENDENCIES BELOW AS SHOWN IN THE EXAMPLE
dependencies:
- alembic
- fsspec
- pydantic
- pydantic-settings
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ classifiers = [
"Topic :: Scientific/Engineering"
]
dependencies = [
"alembic",
"fsspec",
"pydantic",
"pydantic-settings",
Expand Down
12 changes: 8 additions & 4 deletions tests/test_30_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def test_cacheable(tmp_path: pathlib.Path) -> None:
)
]

cur.execute("SELECT timestamp FROM cache_entries", ())
(timestamp,) = cur.fetchone() or []
assert before < datetime.datetime.fromisoformat(timestamp + "+00:00") < after
cur.execute("SELECT updated_at FROM cache_entries", ())
(updated_at,) = cur.fetchone() or []
assert before < datetime.datetime.fromisoformat(updated_at + "+00:00") < after


@pytest.mark.parametrize("raise_all_encoding_errors", [True, False])
Expand Down Expand Up @@ -199,13 +199,17 @@ def test_cached_error() -> None:
def test_cache_entry_repr() -> None:
with config.set(return_cache_entry=True):
cache_entry = cached_now()
assert isinstance(cache_entry, database.CacheEntry)

created_at = cache_entry.created_at
updated_at = cache_entry.updated_at
assert repr(cache_entry) == (
"CacheEntry("
"id=1, "
"key='c3d9e414d0d32337c3672cb29b1b3cc9', "
"expiration=datetime.datetime(9999, 12, 31, 0, 0), "
f"timestamp={cache_entry.timestamp!r}, "
f"created_at={created_at!r}, "
f"updated_at={updated_at!r}, "
"counter=1, "
"tag=None"
")"
Expand Down

0 comments on commit 1002b66

Please sign in to comment.