# 缓存设置
CACHE_TYPE = 'simple'
# CACHE_TYPE = 'filesystem'
# CACHE_THRESHOLD = 5000
CACHE_DIR = './tmp/'
CACHE_DEFAULT_TIMEOUT = 60 * 60 * 1
@cache.memoize(timeout=7200)
def get_token(self, secret):
uri = f'{self.host}/gettoken?corpid={self.corpid}&corpsecret={secret}'
res = http(uri) # 这里用 http 去统一处理.
token = res.get('access_token', '')
if not token:
errcode = res.get('errcode', '')
errmsg = res.get('errmsg', '')
current_app.logger.error(f'get_token error. errcode: {errcode}; errmsg:{errmsg}.')
else:
current_app.logger.debug(f'get_token success. token: {token}')
return token
上例这样写没错吧? 问题是我在 http 请求函数里面打日志 每次都会进行请求
from flask_caching import Cache
from flask import Flask
app = Flask(__name__)
cache = Cache()
cache.init_app(app, config={
"CACHE_TYPE": "simple"
})
def ab(dd):
print('ab')
return dd
@cache.memoize(timeout=30)
def sum_data(a, b):
c = int(a) + int(b)
c = ab(c)
return c
@app.route('/')
def hello_world():
return str(sum_data(1, 2))
if __name__ == "__main__":
app.run(debug=True)
一气之下, 自己用官方的例子发现并不会重复输出 ab 有点费解. 哪里出错了.
1
timonwong 2019-12-25 17:51:12 +08:00
KEY: instance method, the "self" varies
|
3
tmackan 2019-12-26 09:50:38 +08:00
本地缓存都是根据参数,然后 hash 生成 redis 的 key,所以 self 这种场景不实用,参数不固定
|
4
tmackan 2019-12-26 09:53:04 +08:00
```
def memoize(obj): """ Local cache of the function return value """ cache = obj.cache = {} @functools.wraps(obj) def memoizer(*args, **kwargs): key = str(args) + str(kwargs) if key not in cache: cache[key] = obj(*args, **kwargs) return cache[key] return memoizer ``` |
5
tmackan 2019-12-26 09:53:19 +08:00
def memoize(obj):
""" Local cache of the function return value """ cache = obj.cache = {} @functools.wraps(obj) def memoizer(*args, **kwargs): key = str(args) + str(kwargs) if key not in cache: cache[key] = obj(*args, **kwargs) return cache[key] return memoizer |
6
limboMu 2019-12-26 15:16:39 +08:00
@tmackan 刚刚看完源码,作者对装饰方法的时候做了特殊处理,你要保障 get_id(self)的结果是一致的就好了
“”“ def get_id(obj): return getattr(obj, "__caching_id__", repr)(obj) ”“” |
7
tmackan 2019-12-26 20:08:48 +08:00
class CacheProperty(object):
""" Decorator that converts a method with a single self argument into a property cached on the instance. Optional ``name`` argument allows you to make cached properties of other methods. (e.g. url = cached_property(get_absolute_url, name='url') ) """ def __init__(self, func, name=None): self.func = func self.__doc__ = getattr(func, '__doc__') self.name = name or func.__name__ def __get__(self, instance, type=None): if instance is None: return self res = instance.__dict__[self.name] = self.func(instance) return res class A(object): @CacheProperty def create(self, id): return |
8
lolizeppelin 2019-12-27 10:39:40 +08:00
处理 key generator
args = compat.inspect_getargspec(fn) has_self = args[0] and args[0][0] in ('self', 'cls') if has_self: args = args[1:] 或者直接用 dogpile |