diff --git a/HISTORY.rst b/HISTORY.rst index 0fb1246..5ea67bd 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -8,7 +8,8 @@ History ------------------ * **breaking changes** - * none yet + * :class:`Resource` class: replace :meth:`last_request_at()` with :meth:`last_request_age_s()` + (e.g see :meth:`tickets.Ticket.last_request_age_s()`) * **added features** @@ -22,7 +23,7 @@ History ------------------ * **breaking changes** - * none yet + * none * **added features** diff --git a/tests/test_cache.py b/tests/test_cache.py index 2aeb5d7..9942273 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -157,17 +157,16 @@ def test_clear(): assert not cache -def test_timestamp(): +def test_age_s(): import time from datetime import datetime, timezone cache = LruCache() - assert cache.timestamp("foo") is None + assert cache.age_s("foo") is None + cache["foo"] = "bar" - timestamp = cache.timestamp("foo") - assert isinstance(timestamp, datetime) - assert timestamp.tzinfo == timezone.utc - time.sleep(0.01) + time.sleep(0.015) + assert cache.age_s("foo") > 0.015 cache["foo"] = "baz" - assert cache.timestamp("foo") > timestamp + assert cache.age_s("foo") < 0.015 diff --git a/tests/test_tickets.py b/tests/test_tickets.py index 2f734db..3769a3f 100644 --- a/tests/test_tickets.py +++ b/tests/test_tickets.py @@ -180,10 +180,8 @@ def test_ticket_create_article_sender_and_type_attribute(single_ticket): def test_ticket_last_request_at(single_ticket): - from datetime import datetime - - timestamp = single_ticket.last_request_at() - assert isinstance(timestamp, datetime) + age_s = single_ticket.last_request_age_s() + assert isinstance(age_s, float) def test_lazy_attribute_caching(caplog, rclient): diff --git a/zammadoo/cache.py b/zammadoo/cache.py index 4cc78bb..8623bee 100644 --- a/zammadoo/cache.py +++ b/zammadoo/cache.py @@ -2,14 +2,11 @@ # -*- coding: UTF-8 -*- from collections import OrderedDict from collections.abc import Hashable -from datetime import datetime, timedelta, timezone from time import monotonic from typing import Generic, Optional, Tuple, TypeVar _T = TypeVar("_T") -_START_OFFSET = datetime.now(timezone.utc) - timedelta(seconds=monotonic()) - class LruCache(Generic[_T]): def __init__(self, max_size: int = -1) -> None: @@ -93,9 +90,8 @@ def __setitem__(self, item: Hashable, value: _T) -> None: def __delitem__(self, item: Hashable) -> None: del self._cache[item] - def timestamp(self, item: Hashable) -> Optional[datetime]: - ts_sec = self._cache.get(item, (None, None))[0] - if ts_sec: - return _START_OFFSET + timedelta(seconds=ts_sec) - - return None + def age_s(self, item: Hashable) -> Optional[float]: + try: + return monotonic() - self._cache[item][0] + except KeyError: + return None diff --git a/zammadoo/resource.py b/zammadoo/resource.py index a4935bb..03e1a4d 100644 --- a/zammadoo/resource.py +++ b/zammadoo/resource.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- -from datetime import datetime from typing import TYPE_CHECKING, Any, List, Optional, Tuple from .resources import ResourcesT @@ -93,9 +92,9 @@ def reload(self, expand=False) -> None: ) info.update(new_info) - def last_request_at(self) -> Optional[datetime]: - """:return: the last request timestamp""" - return self.parent.cache.timestamp(self.url) + def last_request_age_s(self) -> Optional[float]: + """:return: time in seconds since the last request""" + return self.parent.cache.age_s(self.url) class UserProperty(_AttributeBase):