|
2 | 2 | Advanced Section |
3 | 3 | ******************* |
4 | 4 |
|
5 | | -Persistent Caching |
6 | | -******************** |
| 5 | +Caching |
| 6 | +********** |
| 7 | + |
| 8 | +By default, caching is **disabled**. You can enable the built-in in-memory cache by passing :code:`use_cache=True`: |
| 9 | + |
| 10 | +.. code-block:: python |
7 | 11 |
|
8 | | -Persistent caching is exactly what it means. It makes your requests cache persist or stay around between :py:class:`Client` objects, and between runs, and contexts (:code:`with client:` statements). |
9 | | -Rather than the default behavior, which is saving the cache to memory or not at all and erasing it after each context and run. |
| 12 | + from homeassistant_api import Client |
10 | 13 |
|
| 14 | + client = Client("<API_URL>", "<TOKEN>", use_cache=True) |
11 | 15 |
|
12 | | -If you want to persist your requests cache you can pass your own custom cached session to :py:class:`Client`'s init method. |
13 | | -You can pass a variety of options to your cached session like how fast to expire the cache, where to cache it (the cache backend), and what to do when the cache is expired. |
| 16 | +This creates an in-memory cache that expires after 300 seconds. |
14 | 17 |
|
15 | | -Depending on whether you are using this in an async or sync project you will want to use either :py:class:`aiohttp_client_cache.backends.CachedSession` or :py:class:`requests_cache.CachedSession` respectively. |
16 | | -See the docs for `requests_cache <https://requests-cache.readthedocs.io/en/latest/>`__ and `aiohttp_client_cache <https://aiohttp-client-cache.readthedocs.io/en/latest/>`__ for how to implement these backends, options, and much more. |
| 18 | +Persistent Caching |
| 19 | +******************** |
17 | 20 |
|
18 | | -You can simply pass them to your client like so. |
| 21 | +If you want your cache to persist between runs (e.g. to a filesystem), you can pass your own custom cached session via the :code:`session` parameter. |
| 22 | + |
| 23 | +Depending on whether you are using a sync or async client you will want to use either :py:class:`requests_cache.CachedSession` or :py:class:`aiohttp_client_cache.session.CachedSession` respectively. |
| 24 | +See the docs for `requests_cache <https://requests-cache.readthedocs.io/en/latest/>`__ and `aiohttp_client_cache <https://aiohttp-client-cache.readthedocs.io/en/latest/>`__ for backend options and more. |
19 | 25 |
|
20 | 26 | .. code-block:: python |
21 | 27 |
|
| 28 | + from datetime import timedelta |
22 | 29 | from homeassistant_api import Client |
23 | 30 | from requests_cache import CachedSession |
24 | 31 |
|
25 | 32 | client = Client( |
26 | 33 | "<API_URL>", |
27 | 34 | "<TOKEN>", |
28 | | - cache_session=CachedSession( |
| 35 | + session=CachedSession( |
29 | 36 | backend="filesystem", |
30 | | - expire_after=timedelta(minutes=5) |
31 | | - ) |
| 37 | + expire_after=timedelta(minutes=5), |
| 38 | + ), |
32 | 39 | ) |
33 | 40 |
|
34 | | - # CachedSession is activated by the `with` statement. |
35 | 41 | with client: |
36 | 42 | # Grab and update some cool entities and services inside your installation. |
37 | 43 | ... |
38 | 44 |
|
| 45 | +.. code-block:: python |
| 46 | +
|
39 | 47 | # Or an example for async |
40 | 48 | import asyncio |
41 | | - from homeassistant_api import Client |
| 49 | + from datetime import timedelta |
| 50 | + from homeassistant_api import AsyncClient |
42 | 51 | from aiohttp_client_cache import CachedSession, FileBackend |
43 | 52 |
|
44 | | - client = Client( |
45 | | - "<URL>", |
| 53 | + client = AsyncClient( |
| 54 | + "<API_URL>", |
46 | 55 | "<TOKEN>", |
47 | | - cache_session=CachedSession( |
| 56 | + session=CachedSession( |
48 | 57 | cache=FileBackend( |
49 | | - expire_after=timedelta(minutes=5) |
50 | | - ) |
| 58 | + expire_after=timedelta(minutes=5), |
| 59 | + ), |
51 | 60 | ), |
52 | | - use_async=True |
53 | 61 | ) |
| 62 | +
|
54 | 63 | async def main(): |
55 | 64 | async with client: |
56 | 65 | # Grab and update some cool entities and services inside your installation. |
57 | 66 | ... |
| 67 | +
|
58 | 68 | asyncio.run(main()) |
59 | 69 |
|
60 | 70 |
|
61 | 71 | Why the heck is :py:class:`Client` a context manager? |
62 | 72 | ******************************************************** |
63 | 73 |
|
64 | | -The :py:class:`Client` is a context manager because it activates the cache session and pings Home Assistant to make sure its running. |
65 | | -You might not want this behavior, if you don't then don't use the :code:`with` or :code:`async with` statement. |
66 | | -You can still use the client without it, but you will have to manually activate the cache session before you use it. |
67 | | - |
68 | | -Disabling Caching |
69 | | -****************** |
70 | | - |
71 | | -To explicitly disable the default cache you can pass :code:`cache_session=False` or :code:`async_cache_session=False` to :py:class:`Client`'s init method depending on your use case. |
72 | | -Otherwise the default cache will be used by default when you use :code:`with client:` or :code:`async with client:`. |
| 74 | +The :py:class:`Client` is a context manager because it manages the underlying HTTP session and pings Home Assistant to make sure it's running. |
| 75 | +You don't have to use the context manager — the client works without it — but you'll need to manage the session lifecycle yourself. |
0 commit comments