pydanny/cached-property

cached_property doesn't cache

geweb7 opened this issue · 1 comments

I can't understand why it's not working

from cached_property import cached_property_with_ttl, cached_property

class Item(models.Model):

@cached_property
def test(self):
     return timezone.now()

{{ item.test }}

Every time I see different time.

cached_property 1.4.3
django 1.11.15
python 2.7.12

That's because you are testing incorrectly. Try this:

# items/models.py
from django.db import models
from django.utils import timezone
from cached_property import cached_property
from time import sleep

class Item(models.Model):
    @cached_property
    def test(self):
        return timezone.now()

# In your shell
>>> from items.models import Item
>>> Item.test
>>> Item.test
>>> Item.test
>>> Item.test
>>> Item.test
>>> Item.test