Redis Cache - Changing the key format

Hello,

I was wondering if it would be fine to change learn_cache_key behavior in the cache middleware, I want to implement a cache with multiple sources (shared or per user) and also I want to implement a simpler key format for key deletion.

The format i had in mind is this: key_prefix:version:cache:source[shared|user]:view_name:dynamic_view_arg.

I had a look at the process_response method in the cache middleware and saw that the cache.set call was directly in the method so I’m also wondering how I could properly override this method to allow the usage of a custom learn_cache_key def without having to copy/paste the whole method.

Thanks!

Did more research and found this: Django Docs

I changed my format to be:

  • key_prefix:version:cache:source:view_name:kwarg (if any):key

I ended up using key to pass args to the key_function to create this format but this is clearly overkill, but I couldn’t find another way.

Basically what i’m doing is using key_prefix to add something like {<kwarg1.value><kwarg2.value>} to extract my kwargs then i wipe the content between {} and use the original key but with the gigantic prefix, i know what you’re gonna say, this is terrible and i agree but i didn’t had any other idea since i didn’t want to override cache_page and it doesn’t allow kwargs to be passed to the key function