Cache structure and functionality (improvement suggestion)


I want to purpose an improvement to django, and want to see what you thinking about it.

The Caching API is pretty simple (to the user), but one behavior is missing (to my limited understanding).

Why isnt a functionality for deleting item by their prefix ?

Lets say I want to delete all the items for one App, or to delete all items from one type…
Currently I cant and have to clear the entire cache.
But isn’t it a good functionality ?

What You thinking about this idea ?


I tried to understand how the Cache work, but reach a function call that is not clear where it is implemented…

Where self.key_func(…) is implemented ?

The cache framework originally targetted memcached, which does not support deletion by prefix because of the data structure it uses underneath. An implementation in memcached would require scanning all keys, making it too slow to be useful. Similarly, the second most popular cache backend Redis does not have a built-in command because it would be too slow.

In my library Django-MySQL I have a cache backend that uses the database. Because the underlying data structure stores keys in order, deleting by a prefix is possible, so I added such methods to my backend: Cache - Django-MySQL 4.2.0 documentation . It might be possible to propose such an extension to Django core but it would only be useful for a handful of the backends.

I’d suggest instead that in general you use the cache only in a way where you don’t need deletion. Relying on deleting data from the cache leads to many race conditions you have to think through. Like what if two processes update the same model instance and expire the same key? Can you guarantee that in all orderings of those two processes, the cache is left in a correct state?

It’s pulled from a setting, with a default. See here and the docs for CACHES.

didn’t thought about race conditions. (it can be solved, but with some overhead)
with clearing all the cache is it still a problem ?
(lets say some key is updated, while you clear you whole cache…)

searched for a method, and didnt see it, thanks.

so complicated idea, that doesnt really worth the effort ?

Thanks !

The classic answer is “it depends”.

You need to be careful to always right cache reading code to never assume that a key will be present. This also means avoiding assumptions like “if I fetched key 1 then I can also fetch key 2”. And that isn’t always obvious in code.

For most use cases I’d say so.

1 Like