添加缓存
This commit is contained in:
@@ -18,6 +18,10 @@ def get_redis_db():
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def get_redis_cache_db():
|
||||||
|
return 2
|
||||||
|
|
||||||
|
|
||||||
def get_mongodb_host():
|
def get_mongodb_host():
|
||||||
return "mongo.db.fangjia.com"
|
return "mongo.db.fangjia.com"
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,11 @@ import datetime
|
|||||||
import calendar
|
import calendar
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
import pickle
|
||||||
|
import redis
|
||||||
from fangjia import mongoClients as mongoClients
|
from fangjia import mongoClients as mongoClients
|
||||||
from fangjia import ElasticsearchClient as es
|
from fangjia import ElasticsearchClient as es
|
||||||
|
from fangjia.cachedObjectWrapper import CachedObjectWrapper
|
||||||
|
|
||||||
formatter = logging.Formatter('%(processName)s %(asctime)s %(name)-6s %(levelname)-6s %(message)s')
|
formatter = logging.Formatter('%(processName)s %(asctime)s %(name)-6s %(levelname)-6s %(message)s')
|
||||||
|
|
||||||
@@ -64,6 +67,37 @@ def timer(func):
|
|||||||
return _timer
|
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):
|
def get_collection(db, name, host="192.168.0.136", port=27017):
|
||||||
return mongoClients.MongoClients(db, name, host, port).collection
|
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
|
||||||
@@ -41,5 +41,5 @@ def get_weekend(tt):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
weekend = ""
|
weekend = ""
|
||||||
send_cmd({"city": "西安", "region": "未央", "name": "金仕华城", "weekend": get_weekend('20180318')})
|
send_cmd({"city": "西安", "region": "未央", "name": "兴乐园", "weekend": get_weekend('20180325')})
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|||||||
+11
-3
@@ -43,9 +43,9 @@ lk='金仕华城'
|
|||||||
|
|
||||||
#weekend = datetime.datetime.strptime("2018-03-11", "%Y-%m-%d")
|
#weekend = datetime.datetime.strptime("2018-03-11", "%Y-%m-%d")
|
||||||
#for i in range(0, 100):
|
#for i in range(0, 100):
|
||||||
sellPrice.out_put_price(ct, rl, lk, fangjia.get_weekend())
|
# sellPrice.out_put_price(ct, rl, lk, fangjia.get_weekend())
|
||||||
e = time.time()
|
# e = time.time()
|
||||||
print(e - s)
|
# print(e - s)
|
||||||
'''
|
'''
|
||||||
###
|
###
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
@@ -60,3 +60,11 @@ for i in a.index:
|
|||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
@fangjia.cache(1000)
|
||||||
|
def sum1(a, b):
|
||||||
|
print(111)
|
||||||
|
return a + b
|
||||||
|
|
||||||
|
|
||||||
|
print(sum1(1,2))
|
||||||
|
|||||||
Reference in New Issue
Block a user