redis remove key 之前判断该key是否存在

This commit is contained in:
pengtao 2019-07-17 19:42:24 +08:00
parent a8d2e5975d
commit 3bd7da3a9f

View File

@ -36,8 +36,9 @@ def send_cache_data():
log.info(f"line was {line}") log.info(f"line was {line}")
item = {} item = {}
try: try:
item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], item['ad_url'], \ item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], item[
item['ad_sort'], item['companyid'], item['locationid'], item['gameid'], item['appid'] = line 'ad_url'], item['ad_sort'], item['companyid'], item['locationid'], item['gameid'], item[
'appid'] = line
all.append(item) all.append(item)
except Exception: except Exception:
log.error("split data failed", exc_info=True) log.error("split data failed", exc_info=True)
@ -51,8 +52,9 @@ def send_cache_data():
if line: if line:
item = {} item = {}
try: try:
item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], item['ad_url'], \ item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], item[
item['ad_sort'], item['companyid'], item['locationid'], item['gameid'], item['appid'] = line 'ad_url'], item['ad_sort'], item['companyid'], item['locationid'], item['gameid'], item[
'appid'] = line
all.append(item) all.append(item)
except Exception: except Exception:
log.error("split data failed", exc_info=True) log.error("split data failed", exc_info=True)
@ -72,6 +74,7 @@ def send_cache_data():
n = int(line['ad_num']) - int(num) n = int(line['ad_num']) - int(num)
log.info(f"add {line['id']} to {key} ,num was {line['ad_num']},limit was {n}!") log.info(f"add {line['id']} to {key} ,num was {line['ad_num']},limit was {n}!")
else: else:
if my_redis.sismember(key, line['id']):
my_redis.srem(key, line['id']) my_redis.srem(key, line['id'])
log.info(f"remove {line['id']} from {key}!") log.info(f"remove {line['id']} from {key}!")
elif int(line['ad_num']) == 0: elif int(line['ad_num']) == 0:
@ -95,6 +98,7 @@ def send_cache_data():
try: try:
id, gameid, locationid = line id, gameid, locationid = line
key = f"{gameid}:{locationid}" key = f"{gameid}:{locationid}"
if my_redis.sismember(key, id):
my_redis.srem(key, id) my_redis.srem(key, id)
log.info(f"remove {id} from {key} success!") log.info(f"remove {id} from {key} success!")
except Exception: except Exception: