|
| 1 | +# Raw operations |
| 2 | + |
| 3 | +## Access the underlying valkey client |
| 4 | +if for whatever reason you need to do things that the clients don't support, you can access the underlying valkey connections and send commands by hand |
| 5 | +to get a connection, you can do: |
| 6 | + |
| 7 | +```python |
| 8 | +from django_valkey import get_valkey_connection |
| 9 | + |
| 10 | +raw_client = get_valkey_connection("default") |
| 11 | +``` |
| 12 | + |
| 13 | +in this example `"default"` is the alias name of the backend, that you configured in django's `CACHES` setting |
| 14 | +the signature of the function is as follows: |
| 15 | +```python |
| 16 | +def get_valkey_connection(alias: str="default", write: bool=True, key=None): ... |
| 17 | +``` |
| 18 | + |
| 19 | +`alias` is the name you gave each server in django's `CACHES` setting. |
| 20 | +`write` is used to determine if the operation will write to the cache database, so it should be `True` for `set` and `False` for `get`. |
| 21 | +`key` is only used with the shard client, it'll be explaind below. |
| 22 | + |
| 23 | +### get raw access while using shard client |
| 24 | +**note**: this only works as of v0.3.0 |
| 25 | + |
| 26 | +to get access to the client while using the shard client, you need to pass in the key you are going to work with, |
| 27 | +this is because the shard client determines which server to use by the key. |
| 28 | + |
| 29 | +**note**: if you are trying to use a key that was set by django_valkey's interface, you need to make the key before passing it to `get_valkey_connection` |
| 30 | +we explain how to make a key below. |
| 31 | + |
| 32 | + |
| 33 | +### raw operation utilities |
| 34 | + |
| 35 | +as of `django_valkey` v0.3.0, we provide some additional utilities that might be of interest while doing raw operations: |
| 36 | + |
| 37 | +```python |
| 38 | +from django_valkey.util import make_key, make_pattern, encode, decode |
| 39 | +``` |
| 40 | + |
| 41 | +#### make_key |
| 42 | +`make_key` is used to create keys when we are setting a value in the cache server, and so the same operation is used to read that key. |
| 43 | +`make_key` was an internal method, but as of v0.3.0 it's a function you can use easily |
| 44 | + |
| 45 | +```python |
| 46 | +from django.core.cache.backends.base import default_key_func # this is the default key func, if you are using a custom one, use that instead |
| 47 | +from django_valkey.util import make_key |
| 48 | + |
| 49 | +make_key( |
| 50 | + key="my_key", |
| 51 | + key_func=default_key_func, # the default key func, customize based on your configs |
| 52 | + version=1, # 1 is default, customize it based on your configs |
| 53 | + prefix="", # default prefix, customize based on your config |
| 54 | +) |
| 55 | +``` |
| 56 | + |
| 57 | +the above call will generate a key that looks like `":1:my_key"` |
| 58 | +might be worthy to note that the return value of `make_key` is not a `str`, but a subclass of str, you can find it as `django_valkey.util.CacheKey` |
| 59 | + |
| 60 | +to communicate with cache objects created by `django_valkey` in a raw operation, you should to use this function to make things easy, |
| 61 | +but it's recommended to always use it to make things consistent and up to conventions. |
| 62 | + |
| 63 | +if you don't want to handwrite the arguments, you can find the values used by your client from the `cache` object: |
| 64 | +```python |
| 65 | +from django.core.cache import cache |
| 66 | + |
| 67 | +key_func = cache.key_func |
| 68 | +version = cache.version |
| 69 | +key_prefix = cache.key_prefix |
| 70 | +``` |
| 71 | + |
| 72 | +#### make_pattern |
| 73 | +`make_pattern` is used to make a pattern, which is used for searching and finding keys that are similar, |
| 74 | +for example `foo*` will match `foo1`, `foo2`, `foo_something` and so one |
| 75 | + |
| 76 | +`make_pattern` is used in operations such as `iter_keys`, `delete_pattern` and so on. |
| 77 | + |
| 78 | +to use `make_pattern` notice the following example |
| 79 | + |
| 80 | +```python |
| 81 | +from django.core.cache.backends.base import default_key_func # this is the default key func, if you are using a custom one, use that instead |
| 82 | +from django_valkey.util import make_pattern |
| 83 | + |
| 84 | +make_pattern( |
| 85 | + pattern="my_*", |
| 86 | + key_func=default_key_func, # the default key func, customize based on your configs |
| 87 | + version=1, # 1 is default, customize it based on your configs |
| 88 | + prefix="", # default prefix, customize based on your config |
| 89 | +) |
| 90 | +``` |
| 91 | + |
| 92 | +if you don't want to handwrite the arguments, you can find the values used by your client from the `cache` object: |
| 93 | +```python |
| 94 | +from django.core.cache import cache |
| 95 | + |
| 96 | +key_func = cache.key_func |
| 97 | +version = cache.version |
| 98 | +key_prefix = cache.key_prefix |
| 99 | +``` |
| 100 | + |
| 101 | +#### encode and decode |
| 102 | +`encode` and `decode` are called on values that will be saved/read (not on keys, only values) |
| 103 | + |
| 104 | +encode does two things: |
| 105 | +first it serializes the data |
| 106 | +then it compresses that data |
| 107 | + |
| 108 | +decode is the opposite: |
| 109 | +first decompresses the data |
| 110 | +then deserializes it |
| 111 | + |
| 112 | +to use encode and decode, you need an instance of the serializer and compressor you are using |
| 113 | + |
| 114 | +you can access the ones your config uses from `cache.client._compressor` and `cache.client._serializer`, |
| 115 | +or you can pass in any object you want (note that you need to pass an instance, not the class) |
| 116 | + |
| 117 | +```python |
| 118 | +from django.core.cache import cache |
| 119 | + |
| 120 | +from django_valkey.util import encode, decode |
| 121 | + |
| 122 | +encode(value="my_value", serializer=cache.client._serializer, compressor=cache.client._compressor) |
| 123 | +decode(value="my_value", serializer=cache.client._serializer, compressor=cache.client._compressor) |
| 124 | +``` |
| 125 | + |
| 126 | +if you want to pass in the serializer and compressor by hand, you can instantiate one of the classes we provide and pass in the object, or instantiate your custom class, |
| 127 | +just note that the classes need a dictionary of configurations to be passed to them to be instantiated, |
| 128 | +you can see what configs they get from the [serializer](../configure/advanced_configurations.md#configure-the-serializer) and [compressor](../configure/compressors.md) docs. |
0 commit comments