Refactor favorites management to use Redis
- Removed local favorites.json file and related file handling in the code. - Implemented Redis caching for managing favorite articles, including methods to set, get, and check favorites. - Updated TelegramManager and server API to interact with Redis for favorite operations. - Added search functionality for articles in Redis, enhancing user experience. - Adjusted frontend components to support searching and displaying articles from Redis.
This commit is contained in:
@@ -82,6 +82,16 @@ class RedisArticleCache:
|
||||
try:
|
||||
key = self._get_article_key(article)
|
||||
# Guardar toda la información del artículo como JSON
|
||||
# Verificar si el artículo ya existe para mantener el estado de favorito
|
||||
existing_value = self._redis_client.get(key)
|
||||
is_favorite = False
|
||||
if existing_value:
|
||||
try:
|
||||
existing_data = json.loads(existing_value)
|
||||
is_favorite = existing_data.get('is_favorite', False)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
article_data = {
|
||||
'id': article.get_id(),
|
||||
'title': article.get_title(),
|
||||
@@ -94,6 +104,7 @@ class RedisArticleCache:
|
||||
'images': article.get_images(),
|
||||
'modified_at': article.get_modified_at(),
|
||||
'platform': article.get_platform(),
|
||||
'is_favorite': is_favorite, # Mantener el estado de favorito
|
||||
}
|
||||
self._redis_client.setex(key, NOTIFIED_ARTICLE_TTL, json.dumps(article_data))
|
||||
except Exception as e:
|
||||
@@ -121,12 +132,65 @@ class RedisArticleCache:
|
||||
'images': article.get_images(),
|
||||
'modified_at': article.get_modified_at(),
|
||||
'platform': article.get_platform(),
|
||||
'is_favorite': False, # Por defecto no es favorito
|
||||
}
|
||||
pipe.setex(key, NOTIFIED_ARTICLE_TTL, json.dumps(article_data))
|
||||
pipe.execute()
|
||||
self.logger.debug(f"{len(article_list)} artículos marcados como notificados en Redis")
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error añadiendo artículos a Redis: {e}")
|
||||
|
||||
def set_favorite(self, platform, article_id, is_favorite=True):
|
||||
"""Marca o desmarca un artículo como favorito en Redis"""
|
||||
try:
|
||||
key = f"notified:{platform}:{article_id}"
|
||||
value = self._redis_client.get(key)
|
||||
if value:
|
||||
article_data = json.loads(value)
|
||||
article_data['is_favorite'] = is_favorite
|
||||
# Mantener el TTL existente o usar el default
|
||||
ttl = self._redis_client.ttl(key)
|
||||
if ttl > 0:
|
||||
self._redis_client.setex(key, ttl, json.dumps(article_data))
|
||||
else:
|
||||
self._redis_client.setex(key, NOTIFIED_ARTICLE_TTL, json.dumps(article_data))
|
||||
return True
|
||||
return False
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error marcando favorito en Redis: {e}")
|
||||
return False
|
||||
|
||||
def get_favorites(self):
|
||||
"""Obtiene todos los artículos marcados como favoritos"""
|
||||
try:
|
||||
keys = self._redis_client.keys('notified:*')
|
||||
favorites = []
|
||||
for key in keys:
|
||||
value = self._redis_client.get(key)
|
||||
if value:
|
||||
try:
|
||||
article_data = json.loads(value)
|
||||
if article_data.get('is_favorite', False):
|
||||
favorites.append(article_data)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
return favorites
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error obteniendo favoritos de Redis: {e}")
|
||||
return []
|
||||
|
||||
def is_favorite(self, platform, article_id):
|
||||
"""Verifica si un artículo es favorito"""
|
||||
try:
|
||||
key = f"notified:{platform}:{article_id}"
|
||||
value = self._redis_client.get(key)
|
||||
if value:
|
||||
article_data = json.loads(value)
|
||||
return article_data.get('is_favorite', False)
|
||||
return False
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error verificando favorito en Redis: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def create_article_cache(cache_type='memory', **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user