def cached_method method None ttl 300 print method ttl if method is No

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def cached_method(method=None, ttl=300):
print method, ttl
if method is None:
return lambda m: cached_method(m, ttl)
def cached_func(self, *args, **kwargs):
ctx = md5_constructor()
[ctx.update(str(i)) for i in args]
[ctx.update('%s=%s' % (k,v)) for k,v in kwargs.iteritems()]
key = 'cmethod.%s.%s.%s.%s' % (self.__class__.__name__, method.__name__, ctx.hexdigest(), ttl)
result = cache.get(key)
if not result:
result = method(self, *args, **kwargs)
cache.set(key, result, ttl)
return result
return cached_func
# А вызывать
@cached_method(ttl=500)
def genre_cloud(self, steps=4):
pass
# или
@cached_method
def genre_cloud(self, steps=4):
pass