简体   繁体   中英

When exactly cached_property reset?

Currently I'm using @cached_property for avoiding duplicate access to db.

class MyModel(models.Model):
    ...

    @cached_property
    def my_opts(self):
        options = Option.objects.filter(...)
        return options
    ...

I used this property front template.

It work fine in shell & test. But when I tested in browser, I didn't know exactly when the cached property reset.

Whenever I refresh my browser, the property reset. Then is it useful to use cached property in this scenario? And when exactly the cached_property value reset in the aspect of client side?

Thanks in advance!

The documentation is pretty clear on that:

The cached result will persist as long as the instance does, so if the instance is passed around and the function subsequently invoked, the cached result will be returned.

Whenever you refresh your browser, you recreate the instance, hence invalidating the cache. If you would like a cache that would persist over multiple requests, you should consider using the cache framework .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM