Parcourir la source

自动清除redis保存结果

Sherlock il y a 1 an
Parent
commit
98c2f6a77d
2 fichiers modifiés avec 11 ajouts et 16 suppressions
  1. 4 16
      models/recall/hot_recall.py
  2. 7 0
      models/recall/itemCF/ItemCF.py

+ 4 - 16
models/recall/hot_recall.py

@@ -25,20 +25,6 @@ class HotRecallModel:
         for item, _ in info:
             hotkeys.append(item)
         return hotkeys
-
-
-    # def _load_data_from_dataset(self):
-    #     """从数据库中读取数据"""
-    #     client = Mysql()
-    #     tablename = "mock_order"
-    #     query_text = "*"
-    
-    #     df = client.load_data(tablename, query_text)
-    
-    #     # 去除重复值和填补缺失值
-    #     df.drop_duplicates(inplace=True)
-    #     df.fillna(0, inplace=True)
-    #     return df
         
     def _calculate_hot_score(self, hot_name):
         """
@@ -70,10 +56,12 @@ class HotRecallModel:
     def to_redis(self, rec_content_score, city_uuid):
         hotkey_name = rec_content_score["key"]
         rec_item_id = f"hot:{city_uuid}:{str(hotkey_name)}" # 修正 rec_item_id 拼接方式
-        # rec_item_id = "hot:" + city_uuid + ":" + str(hotkey_name)  
+        
+        # 清空 sorted set 数据,确保不会影响后续的存储
+        self._redis_db.redis.delete(rec_item_id)
+         
         res = {}
 
-        # rec_content_score["value"] 是一个包含字典的列表
         for item in rec_content_score["value"]:  
             for content, score in item.items():  # item 形如 {A001: 75.0}
                 res[content] = float(score)  # 确保 score 是 float 类型

+ 7 - 0
models/recall/itemCF/ItemCF.py

@@ -64,6 +64,13 @@ class ItemCFModel:
         存储格式为 fc:product_code,其中商户 ID 作为成员,得分作为分数
         """
         redis_db = Redis()
+        
+        # 存redis之前,先进行删除操作
+        pattern = f"fc:{city_uuid}:*"
+        keys_to_delete = redis_db.redis.keys(pattern)
+        if keys_to_delete:
+            redis_db.redis.delete(*keys_to_delete)
+            
         for product_code, recommendations in tqdm(self._recommendations.items(), desc="train:正在存储推荐结果"):
             redis_key = f"fc:{city_uuid}:{product_code}"
             zset_data = {}