Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add client cached_session #327

Closed
wants to merge 5 commits into from

Conversation

jooola
Copy link
Member

@jooola jooola commented Nov 21, 2023

Use this cached session in short-lived scopes. Prevent calling the API too often when iterating over a list of resource that contains the same objects (e.g. servers with the same network).

@jooola jooola changed the title feat: add client cached_session feat: add client cached_session Nov 21, 2023
@jooola jooola requested a review from apricote November 21, 2023 14:52
Copy link
Member

@apricote apricote left a comment

Choose a reason for hiding this comment

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

Very nice!

examples/use_cached_session.py Outdated Show resolved Hide resolved
hcloud/_client.py Outdated Show resolved Hide resolved
hcloud/_client.py Show resolved Hide resolved
Comment on lines 245 to 256
@contextmanager
def cached_session(self) -> Generator[Client, None, None]:
"""
Provide a copy of the :class:`Client <hcloud.Client>` as context manager that
will cache all GET requests.

Cached response will not expire automatically, therefore the cached client must
not be used for long living scopes.
"""
client = copy.deepcopy(self)
client.session(CachedSession())
yield client
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not yet convinced about this implementation; I might only allow users to configure the session, and move the CachedSession code to the ansible inventory project for the time we need to iron it out.

hcloud/_client.py Show resolved Hide resolved
@jooola jooola marked this pull request as draft November 22, 2023 16:39
@jooola jooola force-pushed the bound_model_caching branch from c11d5ff to f2470f4 Compare February 1, 2024 15:04
jooola and others added 5 commits February 1, 2024 16:04
Use this cached session in short lived scopes. Prevent calling the API too often when iterating over a list of resource that contains the same objects.
Co-authored-by: Julian Tölle <[email protected]>
If we copy the client, some bound models that where created during the cached session, will still hold  the reference to the cached_client, even outside the scope of the context manager.

By not copying the client, we ensure that the bound model client is not caching request outside the scope of the context.
@jooola jooola force-pushed the bound_model_caching branch from f2470f4 to 2dbf6e0 Compare February 1, 2024 15:04
@jooola
Copy link
Member Author

jooola commented Feb 1, 2024

Closing as won't fix. Users can implement their own caching strategy by extending the Client class.

@jooola jooola closed this Feb 1, 2024
@jooola jooola deleted the bound_model_caching branch February 1, 2024 15:07
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