seuling
seuling

Reputation: 2956

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!

Upvotes: 3

Views: 4250

Answers (1)

Selcuk
Selcuk

Reputation: 59229

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.

Upvotes: 6

Related Questions