Skip to content

Instantly share code, notes, and snippets.

@clarksun
Created January 17, 2018 02:32
Show Gist options
  • Save clarksun/792eb0a8f243caad77ced24dbc28f3e7 to your computer and use it in GitHub Desktop.
Save clarksun/792eb0a8f243caad77ced24dbc28f3e7 to your computer and use it in GitHub Desktop.
redis缓存函数结果
# coding=utf-8
# https://github.com/GuozhuHe/webspider/blob/master/webspider/utils/cache.py
import logging
import pickle
from functools import wraps
import redis
from common import config
redis_pool = redis.ConnectionPool(host=config.REDIS_CONF['host'],
port=config.REDIS_CONF['port'])
redis_instance = redis.Redis(connection_pool=redis_pool)
def simple_cache(func):
"""利用redis进行缓存, 暂不支持kwargs类型的参数传入方式"""
@wraps(func)
def wrapper(*args, **kwargs):
if kwargs:
raise ValueError(
"args key generator does not accept kwargs arguments")
redis_key = func.__name__ + '(' + ','.join(map(str, args)) + ')'
result = redis_instance.get(redis_key)
if result:
logging.info('cache: get func result from redis key - {}'.format(redis_key))
result = pickle.loads(result)
else:
logging.info('cache: get func result from func key - {}'.format(redis_key))
result = func(*args)
redis_instance.set(redis_key, pickle.dumps(result))
return result
return wrapper
def cache_clear(func, *args):
"""失效缓存"""
redis_key = func.__name__
if args:
redis_key += ('(' + ','.join(map(str, args)) + ')')
logging.info('remove cache redis-key: {}'.format(redis_key))
keys = redis_instance.keys('*' + redis_key + '*')
if keys:
remove_count = redis_instance.delete(*keys)
logging.info('cache clear count {}'.format(remove_count))
return remove_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment