-
Notifications
You must be signed in to change notification settings - Fork 43
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
Conversation
cached_session
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!
hcloud/_client.py
Outdated
@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 |
There was a problem hiding this comment.
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.
c11d5ff
to
f2470f4
Compare
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]>
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.
f2470f4
to
2dbf6e0
Compare
Closing as won't fix. Users can implement their own caching strategy by extending the Client class. |
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).