Spaces:
Runtime error
Runtime error
Upload cache_util.py
Browse files- cache_util.py +78 -0
cache_util.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pickle import dumps, loads
|
| 3 |
+
|
| 4 |
+
REDIS_HOST = os.getenv('REDIS_HOST')
|
| 5 |
+
REDIS_PASS = os.getenv('REDIS_PASS')
|
| 6 |
+
|
| 7 |
+
from redis import StrictRedis
|
| 8 |
+
|
| 9 |
+
redis = StrictRedis(host=REDIS_HOST, port=int(os.getenv("REDIS_PORT", "6379")),
|
| 10 |
+
max_connections=256, password=REDIS_PASS)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class PythonNativeRedisClient(object):
|
| 14 |
+
"""A simple redis client for storing and retrieving native python datatypes."""
|
| 15 |
+
|
| 16 |
+
def __init__(self, redis_host=redis):
|
| 17 |
+
"""Initialize client."""
|
| 18 |
+
self.client = redis_host
|
| 19 |
+
|
| 20 |
+
def set(self, key, value, **kwargs):
|
| 21 |
+
"""Store a value in Redis."""
|
| 22 |
+
return self.client.set(key, dumps(value), **kwargs)
|
| 23 |
+
|
| 24 |
+
def get(self, key, default=None):
|
| 25 |
+
"""Retrieve a value from Redis."""
|
| 26 |
+
val = self.client.get(key)
|
| 27 |
+
if val:
|
| 28 |
+
return loads(val)
|
| 29 |
+
return default
|
| 30 |
+
|
| 31 |
+
def expire(self, key, expire_secs):
|
| 32 |
+
self.client.expire(key, expire_secs)
|
| 33 |
+
|
| 34 |
+
def ttl(self, key):
|
| 35 |
+
self.client.ttl(key)
|
| 36 |
+
|
| 37 |
+
def delete(self, key):
|
| 38 |
+
redis.delete(key)
|
| 39 |
+
|
| 40 |
+
redis_client = PythonNativeRedisClient(redis)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Reids 缓存,结果保存到 redis 中, 根据函数的第一个参数缓存结果, 可以指定缓存有效期
|
| 44 |
+
class RedisCache:
|
| 45 |
+
def __init__(self, expire=60, cache_prefix="redis_cache_"):
|
| 46 |
+
self.expire = expire
|
| 47 |
+
self.cache_prefix = cache_prefix
|
| 48 |
+
|
| 49 |
+
def format_key(self, input_key):
|
| 50 |
+
return self.cache_prefix + "_" + input_key
|
| 51 |
+
|
| 52 |
+
def get(self, input_key):
|
| 53 |
+
return redis_client.get(self.format_key(input_key))
|
| 54 |
+
|
| 55 |
+
def __call__(self, func):
|
| 56 |
+
def wrapper(*args, **kwargs):
|
| 57 |
+
# 使用函数的第一个参数作为 limit_key
|
| 58 |
+
key = args[1]
|
| 59 |
+
if key:
|
| 60 |
+
try:
|
| 61 |
+
cached_value = self.get(key)
|
| 62 |
+
if cached_value:
|
| 63 |
+
print("Cached value from redis for method:%s with param:%s" % (func.__name__, str(key)) )
|
| 64 |
+
return cached_value
|
| 65 |
+
except Exception as ex:
|
| 66 |
+
print("Error get data from cache, key:%s error message:%s" % (key, repr(ex)))
|
| 67 |
+
|
| 68 |
+
result = func(*args, **kwargs)
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
redis_key = self.format_key(key)
|
| 72 |
+
redis_client.set(redis_key, result)
|
| 73 |
+
redis_client.expire(redis_key, self.expire)
|
| 74 |
+
except Exception as ex:
|
| 75 |
+
print("Error put data to cache, key:%s error message:%s" % (key, repr(ex)) )
|
| 76 |
+
|
| 77 |
+
return result
|
| 78 |
+
return wrapper # to be used in method within a class
|