diff --git a/lyric_search_new/sources/cache.py b/lyric_search_new/sources/cache.py index 62dd2e0..f63fa36 100644 --- a/lyric_search_new/sources/cache.py +++ b/lyric_search_new/sources/cache.py @@ -96,11 +96,24 @@ class Cache: async def store(self, lyr_result: LyricsResult) -> None: """ - Store lyrics to cache + Store lyrics (SQLite, then Redis) Args: - lyr_result: the returned lyrics to cache + lyr_result (LyricsResult): the returned lyrics to cache + Returns: None + """ + + sqlite_insert_id = await self.sqlite_store(lyr_result) + if sqlite_insert_id: + await self.redis_cache.redis_store(sqlite_insert_id, lyr_result) + + + async def sqlite_store(self, lyr_result: LyricsResult) -> int: + """ + Store lyrics to SQLite Cache + Args: + lyr_result (LyricsResult): the returned lyrics to cache Returns: - None + int: the inserted row id """ logging.info("Storing %s", @@ -129,10 +142,12 @@ class Cache: async with await db_conn.executescript(self.cache_pre_query) as _db_cursor: async with await db_conn.execute(insert_query, params) as _cursor: await db_conn.commit() - logging.info("Stored %s!", artistsong.replace("\n", " - ")) + logging.info("Stored %s to SQLite!", artistsong.replace("\n", " - ")) + return _cursor.lastrowid except: logging.critical("Cache storage error!") traceback.print_exc() + # pylint: disable=unused-argument diff --git a/lyric_search_new/sources/redis_cache.py b/lyric_search_new/sources/redis_cache.py index 21e1d48..5859c3a 100644 --- a/lyric_search_new/sources/redis_cache.py +++ b/lyric_search_new/sources/redis_cache.py @@ -6,17 +6,21 @@ import logging import traceback import json +import time import sys sys.path.insert(1,'..') from lyric_search_new import notifier +from lyric_search_new.constructors import LyricsResult import redis.asyncio as redis from redis.commands.search.query import Query from redis.commands.search.indexDefinition import IndexDefinition, IndexType from redis.commands.search.field import TextField +from redis.commands.json.path import Path from . import private + logger = logging.getLogger() log_level = logging.getLevelName(logger.level) @@ -92,6 +96,39 @@ class RedisCache: await self.notifier.send("WARNING", f"Redis cache miss for: \n## *{artist} - {song}*") return search_res_out except Exception as e: - await self.notifier.send(f"ERROR @ {__file__}", str(e)) + await self.notifier.send(f"ERROR @ {__file__}", f"{str(e)}\nSearch was: {artist} - {song}") traceback.print_exc() + + async def redis_store(self, sqlite_id: int, lyr_result: LyricsResult) -> None: + """ + Store lyrics to redis cache + Args: + sqlite_id (int): the row id of the related SQLite db insertion + lyr_result (LyricsResult): the returned lyrics to cache + Returns: + None + """ + redis_mapping = { + 'id': sqlite_id, + 'src': lyr_result.src, + 'date_retrieved': time.time(), + 'artist': lyr_result.artist, + 'song': lyr_result.song, + 'artistsong': f"{lyr_result.artist}\n{lyr_result.song}", + 'confidence': lyr_result.confidence, + 'lyrics': lyr_result.lyrics, + 'tags': '(none)', + 'liked': 0, + } + newkey = f"lyrics:000{sqlite_id}" + jsonset = await self.redis_client.json().set(newkey, Path.root_path(), + redis_mapping) + if not jsonset: + raise RedisException(f"Failed to store {lyr_result.artist} - {lyr_result.song} (SQLite id: {sqlite_id}):\n{jsonset}") + logging.info("Stored %s - %s (related SQLite Row ID: %s) to %s", + lyr_result.artist, lyr_result.song, sqlite_id, newkey) + await self.notifier.send("INFO", + f"Stored {lyr_result.artist} - {lyr_result.song} (related SQLite Row ID: {sqlite_id}) to {newkey}") + + \ No newline at end of file