You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/customize.md
+16-11Lines changed: 16 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,9 @@ django-valkey comes with three base classes: `django_valkey.base.BaseValkeyCache
8
8
9
9
## BaseValkeyCache
10
10
11
-
`BaseValkeyCache`inherits from django's `BaseCache` class and `typing.Generic`.
12
-
`BaseCache` adds basic cache functionality, such as `get()`and `set()`, and `typing.Generic` allows for a more robust type hinting.
13
-
`BaseValkeyCache`adds more valkey oriented methods to BaseCache, things like `expire()` and `get_lock()`, and uses Generic to type hint two things:
11
+
`BaseValkeyCache`is not a standalone class, to make use of it you need to add the actual methods, in `django-valkey` this is done by `django_valkey.base.BackendCommands` or `django_valkey.base.AsyncBackendCommands` depending if you use sync or async clients
12
+
`BaseValkeyCache` contains connection methods and configures the behaviour of the cache
13
+
`BaseValkeyCache`inherits from `typing.Generic` to type hint two things:
14
14
1. the client, such as `django_valkey.client.default.DefaultClient`.
15
15
2. the underlying backend, such as `valkey.Valkey`.
16
16
@@ -19,23 +19,26 @@ to inherit from this base class you can take the example of our own cache backen
19
19
```python
20
20
from valkey import Valkey
21
21
22
-
from django_valkey.base import BaseValkeyCache
22
+
from django_valkey.base import BaseValkeyCache, BackendCommands
the class attribute defined in the example is **mandatory**, it is so we can have imports in other modules.
30
+
the `DEFAULT_CLIENT_CLASS`class attribute defined in the example is **mandatory**, it is so we can have imports in other modules.
31
31
32
-
`BaseValkeyCache`has both *sync* and *async*methods implemented, but there is no logic in them, most methodsneed to be overwritten.
32
+
`BaseValkeyCache`can work with both *sync* and *async*subclasses, but it doesn't implement any of the methods, you need to inherit the command classes for this to work.
33
33
34
34
35
35
## BaseClient
36
-
`BaseClient` inherits from `typing.Generic` to make cleaner type hints.
37
-
this class has all the logic necessary for a cache client (it is a copy of the old DefaultClient class), it finds the different servers and connects to them, add has all the commands that valkey supports.
36
+
like `BaseValkeyCache`, `BaseClient` is not a standalone class.
37
+
this class has all the logic necessary to connect to a cache server, and utility methods that helps with different operations,
38
+
but it does not handle any of the operations by itself, you need one of `django_valkey.base_client.ClientCommands` or `django_valkey.base_client.AsyncClientCommands` for sync or async clients, respectively.
39
+
the command classes implement the actual operations such as `get` and `set`.
38
40
41
+
`BaseClient` inherits from `typing.Generic` to make cleaner type hints.
39
42
the `typing.Generic` needs a backend to be passed in, e.g: `valkey.Valkey`
40
43
41
44
the base class also needs the subclasses to have a `CONNECTION_FACTORY_PATH` class variable pointing to the connection factory class.
@@ -45,14 +48,16 @@ an example code would look like this:
45
48
```python
46
49
from valkey import Valkey
47
50
48
-
from django_valkey.base_client import BaseClient
51
+
from django_valkey.base_client import BaseClient, ClientCommands
*note* that CONNECTION_FACTORY_PATH is only used if `DJANGO_VALKEY_CONNECTION_FACTORY` is not set.
55
58
59
+
`BaseClient` can work with both sync and async subclasses, you would use one of `django_valkey.base_client.ClientCommands` for sync, and `django_valkey.base_client.AsyncClientCommands` for async clients.
0 commit comments