|
@@ -1,22 +1,26 @@
|
|
|
import redis
|
|
|
+from datetime import timedelta
|
|
|
|
|
|
-class SyncRedisHelper(object):
|
|
|
+
|
|
|
+class SyncRedisHelper:
|
|
|
_pool: redis.ConnectionPool = None
|
|
|
_instance = None
|
|
|
|
|
|
- def __new__(cls, *args, **kwargs):
|
|
|
- if cls._instance is None:
|
|
|
- cls._instance = super().__new__(cls, *args, **kwargs)
|
|
|
- return cls._instance
|
|
|
+ def __init__(self):
|
|
|
+ if not self._instance:
|
|
|
+ self._pool = self._get_pool()
|
|
|
+ self._instance = self
|
|
|
|
|
|
def _get_pool(self) -> redis.ConnectionPool:
|
|
|
if self._pool is None:
|
|
|
self._pool = redis.ConnectionPool(
|
|
|
- host='r-bp1mb0v08fqi4hjffu.redis.rds.aliyuncs.com',
|
|
|
- # host='r-bp1mb0v08fqi4hjffupd.redis.rds.aliyuncs.com', # 外网地址
|
|
|
+ # host="r-bp1mb0v08fqi4hjffu.redis.rds.aliyuncs.com", # 内网地址
|
|
|
+ host="r-bp1mb0v08fqi4hjffupd.redis.rds.aliyuncs.com", # 外网地址
|
|
|
port=6379,
|
|
|
db=2,
|
|
|
- password='Wqsd@2019'
|
|
|
+ password="Wqsd@2019",
|
|
|
+ # password="Qingqu2019",
|
|
|
+
|
|
|
)
|
|
|
return self._pool
|
|
|
|
|
@@ -28,3 +32,36 @@ class SyncRedisHelper(object):
|
|
|
def close(self):
|
|
|
if self._pool:
|
|
|
self._pool.disconnect(inuse_connections=True)
|
|
|
+
|
|
|
+
|
|
|
+def store_data(platform, out_video_id, condition, day_time):
|
|
|
+ key = f"crawler:duplicate:{platform}:{out_video_id}"
|
|
|
+ value = 1
|
|
|
+ if condition:
|
|
|
+ timeout = timedelta(days=int(day_time))
|
|
|
+ else:
|
|
|
+ timeout = timedelta(hours=int(day_time))
|
|
|
+ helper = SyncRedisHelper()
|
|
|
+ client = helper.get_client()
|
|
|
+
|
|
|
+ client.set(key, value)
|
|
|
+ client.expire(key, timeout)
|
|
|
+
|
|
|
+
|
|
|
+def get_data(platform, out_video_id):
|
|
|
+ key = f"crawler:duplicate:{platform}:{out_video_id}"
|
|
|
+ helper = SyncRedisHelper()
|
|
|
+ client = helper.get_client()
|
|
|
+ value = client.exists(key)
|
|
|
+ return value
|
|
|
+
|
|
|
+
|
|
|
+# 示例:存储一个数据
|
|
|
+# store_data('xiaoniangao', '123457', True, 60)
|
|
|
+
|
|
|
+# 示例:获取一个数据
|
|
|
+value = get_data('xiaoniangao', '1234857')
|
|
|
+if value is None:
|
|
|
+ print("Value does not exist")
|
|
|
+else:
|
|
|
+ print(f"Retrieved value: {value}")
|