diff --git a/PriceSystem/config/__init__.py b/PriceSystem/config/__init__.py index 4796513..32bb56b 100644 --- a/PriceSystem/config/__init__.py +++ b/PriceSystem/config/__init__.py @@ -18,6 +18,10 @@ def get_redis_db(): return 1 +def get_redis_cache_db(): + return 2 + + def get_mongodb_host(): return "mongo.db.fangjia.com" diff --git a/PriceSystem/fangjia/__init__.py b/PriceSystem/fangjia/__init__.py index f783469..6867b9d 100644 --- a/PriceSystem/fangjia/__init__.py +++ b/PriceSystem/fangjia/__init__.py @@ -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 diff --git a/PriceSystem/fangjia/cachedObjectWrapper.py b/PriceSystem/fangjia/cachedObjectWrapper.py new file mode 100644 index 0000000..4e9d56f --- /dev/null +++ b/PriceSystem/fangjia/cachedObjectWrapper.py @@ -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 \ No newline at end of file diff --git a/PriceSystem/ones_Sender.py b/PriceSystem/ones_Sender.py index 9562b31..74be44f 100644 --- a/PriceSystem/ones_Sender.py +++ b/PriceSystem/ones_Sender.py @@ -41,5 +41,10 @@ def get_weekend(tt): if __name__ == "__main__": weekend = "" +<<<<<<< HEAD send_cmd({"city": "西安", "region": "雁塔", "name": "鸿景花园", "weekend": get_weekend('20180325')}) #sys.exit(0) +======= + send_cmd({"city": "西安", "region": "未央", "name": "兴乐园", "weekend": get_weekend('20180325')}) + sys.exit(0) +>>>>>>> 4025e7533b2d0abe68b929bfb7d7c01b201fc112 diff --git a/PriceSystem/test.py b/PriceSystem/test.py index b1ba7e4..c8eac2e 100644 --- a/PriceSystem/test.py +++ b/PriceSystem/test.py @@ -37,16 +37,16 @@ lk = '会展世界城三期' ct='西安' rl='雁塔' lk='鸿景花园' -#成都双流长滩一号 上海 奉贤 朝阳新村 +#成都双流长滩一号 上海 奉贤 朝阳新村## #未央 #weekend = datetime.datetime.strptime("2018-03-11", "%Y-%m-%d") #for i in range(0, 100): -sellPrice.out_put_price(ct, rl, lk, fangjia.get_weekend()) -e = time.time() -print(e - s) +# sellPrice.out_put_price(ct, rl, lk, fangjia.get_weekend()) +# e = time.time() +# print(e - s) ''' ### import pandas as pd @@ -61,3 +61,11 @@ for i in a.index: ''' + +@fangjia.cache(1000) +def sum1(a, b): + print(111) + return a + b + + +print(sum1(1,2))