以下代码可以使用装饰器灵活地删除缓存
@wipe_cache(lambda request: user_roles_key(request.userId), 1)
def update(self, request, *args, **kwargs):
...
def wipe_cache_p(key_fun, key_position):
def wrapper(func):
def inner(*args, **kwargs):
ret = func(*args, **kwargs)
key = key_fun(args[key_position])
cache.delete(key)
return ret
return inner
return wrapper