Skip to content

feature: Bounded TTL Caching#508

Open
pvbouwel wants to merge 2 commits into
masterfrom
TTLLRUCaching
Open

feature: Bounded TTL Caching#508
pvbouwel wants to merge 2 commits into
masterfrom
TTLLRUCaching

Conversation

@pvbouwel
Copy link
Copy Markdown
Contributor

@pvbouwel pvbouwel commented Jun 3, 2026

Make sure the TtlCache always has an upper bound on the items it can contain.

Breaking changes:

  • No longer support per item TTL warning is emitted upon usage.
  • Boundary semantics are different at the exact boundary the element is expired. We don't rely on second accuracy and the new behavior is safer.

Make sure the TtlCache always has an upper bound on the items it can contain.

Breaking changes:
- No longer support per item TTL warning is emitted upon usage.
- Boundary semantics are different at the exact boundary the element is expired. We don't rely on second accuracy and the new behavior is safer.
@pvbouwel pvbouwel requested a review from soxofaan June 3, 2026 14:45
tests: make sure time is virtual for user auth caching tests
Copy link
Copy Markdown
Member

@soxofaan soxofaan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the complexity of this PR is a bit bigger than I expected, especially because it introduces some breaking of the original API

maybe it's just easier to leave the original TtlCache as is, and just introduce a new BoundedTtlCache class?

self, default_ttl: float = 60, _clock: Callable[[], float] = time.time
self,
default_ttl: float = 60,
*,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also make default_ttl a keyword-only argument

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it also doesn't make sense anymore to call it default_ttl: should just be ttl

default_ttl: float = 60,
*,
max_size: int = 1000,
_clock: Callable[[], float] = time.time,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the tests now use time_machine to time mocking, is it still necessary to have this _clock attribute (which is only there for testability)?

@pvbouwel
Copy link
Copy Markdown
Contributor Author

pvbouwel commented Jun 3, 2026

I wouldn't mind a BoundedTtlCache then it is easier to address comments like signature changes but I'd still deprecate the old class because otherwise if issues are encountered down the road there are 2 implementations to maintain and the replacement protects more against mistakes.

Can as well be 2 separate steps. Add the class and "switch usage and deprecate old class" that way deployment would be staightforward and rollback would be easy.

Comment thread tests/users/test_auth.py


def test_bearer_auth_oidc_caching(app, requests_mock, oidc_provider):
def test_bearer_auth_oidc_caching(app, requests_mock, oidc_provider, simple_time):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using the "fast_sleep" fixture here would be more to the point and more self-explanatory

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did is to be consistent with OpenEO Python client. The problem with fast_sleep is that it does not set a reference time. So simple_time does time_machine.move_to(1000, tick=False) and then allows fast_sleep. If you just use fixture fast_sleep still need to initialize time_machine prior to using

Comment thread tests/users/test_auth.py


def test_userinfo_url_caching(app, requests_mock, oidc_provider):
def test_userinfo_url_caching(app, requests_mock, oidc_provider, simple_time):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same note about fast_sleep

@soxofaan
Copy link
Copy Markdown
Member

soxofaan commented Jun 4, 2026

another way to simplify this task is maybe first to eliminate the per-set() ttl feature and only have a instance wide ttl, and do this really "breaking" (without warning), because this feature is only used internally, so we can migrate in the same commit.

And then the integration with cachetool will be a lot cleaner of a PR

@pvbouwel
Copy link
Copy Markdown
Contributor Author

pvbouwel commented Jun 4, 2026

If class is to be separated that would be #509

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants