修改:     PriceSystem/config/__init__.py
	修改:     PriceSystem/fangjia/__init__.py
	新文件:   PriceSystem/fangjia/cachedObjectWrapper.py
	修改:     PriceSystem/ones_Sender.py
	修改:     PriceSystem/test.py
This commit is contained in:
罗干
2018-03-28 10:16:54 +08:00
5 changed files with 67 additions and 4 deletions
+34
View File
@@ -7,8 +7,11 @@ import datetime
import calendar
import requests
import json
import pickle
import redis
from fangjia import mongoClients as mongoClients
from fangjia import ElasticsearchClient as es
from fangjia.cachedObjectWrapper import CachedObjectWrapper
formatter = logging.Formatter('%(processName)s %(asctime)s %(name)-6s %(levelname)-6s %(message)s')
@@ -64,6 +67,37 @@ def timer(func):
return _timer
QUEUE = "price_system"
redis_pool = redis.ConnectionPool(host=config.get_redis_host(), port=6379, db=config.get_redis_cache_db())
client = redis.Redis(connection_pool=redis_pool)
def cache(expire=None):
def _cache(func):
def __cache(*args, **kw):
arg_list = list()
for arg in args:
arg_list.append(str(arg))
key = func.__name__ + "_" + "_".join(arg_list)
try:
val = client.hget(QUEUE, key)
if val is not None:
wrapper = pickle.loads(val)
if not wrapper.is_expire():
return wrapper.val
else:
client.hset(QUEUE, key, None)
except Exception as ex:
print(ex)
rs = func(*args, **kw)
wrapper = CachedObjectWrapper(rs, time.time(), expire)
client.hset(QUEUE, key, pickle.dumps(wrapper))
return rs
return __cache
return _cache
def get_collection(db, name, host="192.168.0.136", port=27017):
return mongoClients.MongoClients(db, name, host, port).collection
@@ -0,0 +1,12 @@
import time
class CachedObjectWrapper:
def __init__(self, val, timestamp, cached_time=None):
self.val = val
self.timestamp = timestamp
self.cached_time = cached_time
def is_expire(self):
return self.cached_time is not None and time.time() > self.cached_time + self.timestamp