So I’m inserting data into a redis url in an external script via a cron.
I want to read this in Django 4.1.7.
A simple print("name = ", cache.get('name'))
doesn’t work.
Because if I do in my views.py
cache.set('name', 'John')
print("name = ", cache.get('name'))
works as it stores it in the Redis cache as :
["somePrefix:1:name","string"]
���John�.
So how do I get just name stored from elsewhere ?
I am using upstash.
Looking up the source code from django, i had found the default_key_func
on django/core/cache/backends/base.py this function is used by the BaseBackend
class also defined later on that file. This is the base class for all backends, and on the RedisBackend
defined on django/core/cache/backends/redis.py on the set
method, it will make use of this key.
This function will use the make_default_key
function that creates a namespace around the key, so on that case name
can be something like: 1:name
where 1
prefix is the version (this parameter is the default when you call set, without the version
argument), considering that the KEY_PREFIX
is the default (a empty string).
You can provide a custom function to make the key, this is documented here
For the time-being to make it work I did in my views.py file r = redis.Redis(...)