djangodjango-redis

django- how can i save data in redis with django with custom key?


i want to delete some specific data after model.save() fired. i use "post_save()" signal. when I use cache_page() decorator in my views.py, data will save in redis with keys like this: "prefix:1:views.decorators.cache.cache_header..8ce4de6051c3ba05396ff670741d3172.fa-ir.IRST'.

I want to save data with custom key that i specify. how can i do it?

or

how can i delete stored data that are related to specific url ?

url1: a/1/b/
url2: a/2/b/

how can i get data that are saved for url1?


Solution

  • Default function responsible for transforming cache key looks like this:

    def make_key(key, key_prefix, version):
        return ':'.join([key_prefix, str(version), key])
    

    via cache docs

    So you would have to add key_prefix to your cache_page decorator

    @cache_page(60 * 15, key_prefix="site1")
        def my_view(request):
        ...