From 4025e7533b2d0abe68b929bfb7d7c01b201fc112 Mon Sep 17 00:00:00 2001 From: wangwenyan Date: Mon, 26 Mar 2018 18:06:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PriceSystem/config/__init__.py | 4 +++ PriceSystem/fangjia/__init__.py | 34 ++++++++++++++++++++++ PriceSystem/fangjia/cachedObjectWrapper.py | 12 ++++++++ PriceSystem/ones_Sender.py | 2 +- PriceSystem/test.py | 14 +++++++-- 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 PriceSystem/fangjia/cachedObjectWrapper.py 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 3312aac..8069bde 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 6d4b148..5bc36a2 100644 --- a/PriceSystem/ones_Sender.py +++ b/PriceSystem/ones_Sender.py @@ -41,5 +41,5 @@ def get_weekend(tt): if __name__ == "__main__": weekend = "" - send_cmd({"city": "西安", "region": "未央", "name": "金仕华城", "weekend": get_weekend('20180318')}) + send_cmd({"city": "西安", "region": "未央", "name": "兴乐园", "weekend": get_weekend('20180325')}) sys.exit(0) diff --git a/PriceSystem/test.py b/PriceSystem/test.py index 4234931..249a1b1 100644 --- a/PriceSystem/test.py +++ b/PriceSystem/test.py @@ -43,9 +43,9 @@ 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 @@ -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))