|
| 1 | +# Advanced Async Configuration |
| 2 | + |
| 3 | +most of the subject discussed in [Advanced configuration](../configure/advanced_configurations.md) apply to async mode as well, just don't use a sync client :) |
| 4 | + |
| 5 | +also all the compressor details we talked about in [Compressor support](../configure/compressors.md) work as is in async mode |
| 6 | + |
| 7 | +**Important**: the async clients are not compatible with django's cache middleware. |
| 8 | +if you need those middlewares, consider using a sync client or implement a new middleware |
| 9 | + |
| 10 | +## Clients |
| 11 | + |
| 12 | +We have three async client, `AsyncDefaultClient`, available in `django_valkey.async_cache.client.default`, `AsyncHerdClient` available in `django_valkey.async_cache.client.herd` and `AsyncSentinelClient` at `django_valkey.async_cache.client.sentinel`. |
| 13 | +the default client can also be used with sentinels, as we'll discuss later. |
| 14 | + |
| 15 | +note that all clients are imported and available at `django_valkey.async_cache.client` |
| 16 | + |
| 17 | +### Default client |
| 18 | + |
| 19 | +the `AsyncDefaultClient` is configured by default by `AsyncValkeyCache`, so if you have configured that as your backend you are all set, but if you want to be explicit or use the client with a different backend you can write it like this: |
| 20 | + |
| 21 | +```python |
| 22 | +CACHES = { |
| 23 | + "async": { |
| 24 | + "BACKEND": "path.to.backend", |
| 25 | + "LOCATION": [ |
| 26 | + "valkey://user:[email protected]:6379", |
| 27 | + ] |
| 28 | + "OPTIONS": { |
| 29 | + "CLIENT_CLASS": "django_valkey.async_cache.client.AsyncDefaultClient", |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +or you can replace the client with your own like that. |
| 36 | + |
| 37 | +### Sentinel Client |
| 38 | + |
| 39 | +to support sentinels, django_valkey comes with a client and a connection factory, technically you don't need the connection factory, but it provides you with some nice features. |
| 40 | +a dedicated page on sentinel client has been written in [Sentinel configuration](../configure/sentinel_configurations.md), tho that is for the sync version, the principle is the same. |
| 41 | + |
| 42 | +the connection factory is at `django_valkey.async_cache.pool.AsyncSentinelConnectionFactory`. |
| 43 | + |
| 44 | +to configure the async sentinel client you can write your settings like this: |
| 45 | + |
| 46 | +```python |
| 47 | +SENTINELS = [ |
| 48 | + ("127.0.0.1", 26379), # a list of (host name, port) tuples. |
| 49 | +] |
| 50 | + |
| 51 | +CACHES = { |
| 52 | + "default": { |
| 53 | + "BACKEND": "django_valkey.async_cache.cache.AsyncValkeyCache", |
| 54 | + "LOCATION": "valkey://service_name/db", |
| 55 | + "OPTIONS": { |
| 56 | + "CLIENT_CLASS": "django_valkey.client.SentinelClient", |
| 57 | + "SENTINELS": SENTINELS, |
| 58 | + |
| 59 | + # optional |
| 60 | + "SENTINEL_KWARGS": {} |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +*note*: the sentinel client uses the sentinel connection factory by default, you can change it by setting `DJANGO_VALKEY_CONNECTION_FACTORY` in your django settings or `CONNECTION_FACTORY` in your `CACHES` OPTIONS. |
| 67 | + |
| 68 | +### Herd client |
| 69 | + |
| 70 | +the herd client needs to be configured, but it's as simple as this: |
| 71 | + |
| 72 | +```python |
| 73 | +CACHES = { |
| 74 | + "default": { |
| 75 | + "BACKEND": "django_valkey.async_cache.cache.AsyncValkeyCache", |
| 76 | + "LOCATION": ["valkey://127.0.0.1:6379"], |
| 77 | + "OPTIONS": { |
| 78 | + "CLIENT_CLASS": "django_valkey.async_cache.client.AsyncHerdClient", |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Connection Factory |
| 85 | + |
| 86 | +django_valkey's async library comes with two connection factories, `AsyncConnectionFactory` for general uses and `AsyncSentinelConnectionFactory` for sentinel uses. |
| 87 | + |
| 88 | +the default connection factory is `AsyncConnectionFactory`, so if you are using a sentinel server you should configure your caches like this: |
| 89 | + |
| 90 | +```python |
| 91 | +CACHES = { |
| 92 | + "async": { |
| 93 | + # ... |
| 94 | + "OPTIONS": { |
| 95 | + "CONNECTION_FACTORY": "django_valkey.async_cache.pool.AsyncSentinelConnectionFactory" |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +CACHE_HERD_TIMEOUT = 20 # if not set, it's default to 60 |
| 101 | +``` |
| 102 | + |
| 103 | +or set it as the global connection factory like this: |
| 104 | + |
| 105 | +```python |
| 106 | +DJANGO_VALKEY_CONNECTION_FACTORY = "django_valkey.async_cache.client.default.AsyncDefaultClient" |
| 107 | +``` |
| 108 | + |
| 109 | +note that `"CONNECTION_FACTORY"` overrides `DJANGO_VALKEY_CONNECTION_FACTORY` for the specified server. |
| 110 | + |
| 111 | +if you want to use another factory you can use the same code with the path to your class. |
| 112 | + |
0 commit comments