Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong cache timing for long-running functions #66

Open
goncalopp opened this issue Jul 8, 2017 · 1 comment
Open

Wrong cache timing for long-running functions #66

goncalopp opened this issue Jul 8, 2017 · 1 comment

Comments

@goncalopp
Copy link

goncalopp commented Jul 8, 2017

I'm not sure if this actually a bug, or intended behaviour, but cached_property_with_ttl doesn't consider the time that the cached function takes to execute, only the time between attribute accesses.
This means that, for long-running functions, the cache misses when it shouldn't (my use case is caching the result of HTTP requests).

Here's code that demonstrates the issue

from cached_property import cached_property_with_ttl
import time

class C(object):
    @cached_property_with_ttl(ttl=1)
    def value(self):
        print("computing")
        time.sleep(2)

c = C()
c.value
c.value

The function is called twice, despite the fact that the second attribute access happens "immediately"

This can be solved with the following patch

            if not ttl_expired:
                return value

+      now = time()
        value = self.func(obj)
        obj_dict[name] = (value, now)
        return value
@ljluestc
Copy link

from cached_property import cached_property_with_ttl
import time

class C(object):
@cached_property_with_ttl(ttl=1)
def value(self):
print("computing")
time.sleep(2)

Add this import for the 'time' function

from time import time as current_time

c = C()
c.value
c.value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants