Skip to content

Commit

Permalink
Resource class: replace method last_request_at() with last_request_ag…
Browse files Browse the repository at this point in the history
…e_s()

Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Jul 27, 2024
1 parent 5289611 commit bd7222f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
5 changes: 3 additions & 2 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand All @@ -22,7 +23,7 @@ History
------------------
* **breaking changes**

* none yet
* none

* **added features**

Expand Down
13 changes: 6 additions & 7 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 2 additions & 4 deletions tests/test_tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 5 additions & 9 deletions zammadoo/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
7 changes: 3 additions & 4 deletions zammadoo/resource.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit bd7222f

Please sign in to comment.