Invalidating a path from the Django cache recursively

后端 未结 3 420
抹茶落季
抹茶落季 2020-12-24 04:25

I am deleting a single path from the Django cache like this:

from models                   import Graph
from django.http              import HttpRequest
from         


        
3条回答
  •  别那么骄傲
    2020-12-24 04:43

    You might want to consider employing a generational caching strategy, it seems like it would fit what you are trying to accomplish. In the code that you have provided, you would store a "generation" number for each absolute url. So for example you would initialize the "/graph/123" to have a generation of one, then its cache key would become something like "/GENERATION/1/graph/123". When you want to expire the cache for that absolute url you increment its generation value (to two in this case). That way, the next time someone goes to look up "/graph/123" the cache key becomes "/GENERATION/2/graph/123". This also solves the issue of expiring all the sub pages since they should be referring to the same cache key as "/graph/123".

    Its a bit tricky to understand at first but it is a really elegant caching strategy which if done correctly means you never have to actually delete anything from cache. For more information here is a presentation on generational caching, its for Rails but the concept is the same, regardless of language.

提交回复
热议问题