question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalidating cache?

See original GitHub issue

First of all: thanks so much for all of the hard work on this! I was able to implement caching super fast.

It’d be awesome if I could somehow invalidate all of the cache keys for a given view, for example if I have caching for “Companies” in my app and I update Companies, it’d be great if I could just say: UserKeyConstructor.invalidate_all() or some such.

Is this possible? I noticed from key construction they are hashed, so we couldn’t do some kind of key removal based on a string/prefix…

Thanks again and have a good one!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ckcollabcommented, Feb 13, 2019

Actually think I figured something out, I can do something like this:

class PrefixedKeyConstructor(KeyConstructor):

    def __init__(self, unique_key_name, *args, **kwargs):
        self.unique_key_name = unique_key_name
        super().__init__(*args, **kwargs)

    def prepare_key(self, key_dict):
        key = super().prepare_key(key_dict)
        return '{}{}'.format(self.unique_key_name, key)


class UserKeyConstructor(PrefixedKeyConstructor):
    unique_method_id = bits.UniqueMethodIdKeyBit()
    user = bits.UserKeyBit()
    query_params = bits.QueryParamsKeyBit(['*'])

And invalidate all the keys like so:

from django.core.cache import cache
cache.delete_pattern("foo_*")

and fire this off with signals:

def invalidate_offices(sender=None, instance=None, *args, **kwargs):
    cache.delete_pattern('*api:office*')

post_save.connect(receiver=invalidate_offices, sender=Office)
post_delete.connect(receiver=invalidate_offices, sender=Office)
1reaction
benrevlcommented, Sep 7, 2017

The solution by @ckcollab here is better than the official documentations solution – this actually removes the cache entries instead of adding new ones. The solution in the official documentation will eventually fill the cache with redundant previously cached data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cache invalidation - Wikipedia
Cache invalidation is a process in a computer system whereby entries in a cache are replaced or removed. It can be done explicitly,...
Read more >
Cache invalidation overview | Cloud CDN
After an object is cached, it normally remains in the cache until it expires or is evicted to make room for new content....
Read more >
What is cache invalidation? - Varnish Software
Cache invalidation refers to process during which web cache proxies declare cached content as invalid, meaning it will not longer be served as...
Read more >
When and How to Invalidate Cache - Lu's blog
By "invalidate" I meant the action of updating/dropping relevant cache entries when the source of truth mutates, so no stale data is stored...
Read more >
On Cache Invalidation - Why is it hard? - Yihui Xie | 谢益辉
The tricky thing is “the same thing”. How do you know that you are computing the same thing? That is all “cache invalidation”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found