Function decorator for caching function results in local memory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from time import time

class cache(object):
    def __init__(self, seconds):
        self.seconds = seconds
        self.value = None
        self.timestamp = None
    def __call__(self, f):
        def cached():
            now = time()
            if now - self.seconds > self.timestamp:
                self.value = f()
                self.timestamp = now
            return self.value
        return cached

More like this

  1. per-function cache decorator by nicois 6 years, 3 months ago
  2. Automatic Memoization Decorator by nikmolnar 1 year, 3 months ago
  3. Clear Django Cache by girasquid 5 years, 7 months ago
  4. Run and cache only one instance of a heavy request by farnsworth 3 years, 8 months ago
  5. template + cache = crazy delicious by jacobian 6 years, 11 months ago

Comments

(Forgotten your password?)