store new cache results to both sqlite and redis, in that order
This commit is contained in:
parent
d27bc450f5
commit
9e6feb90a5
@ -96,11 +96,24 @@ class Cache:
|
|||||||
|
|
||||||
async def store(self, lyr_result: LyricsResult) -> None:
|
async def store(self, lyr_result: LyricsResult) -> None:
|
||||||
"""
|
"""
|
||||||
Store lyrics to cache
|
Store lyrics (SQLite, then Redis)
|
||||||
Args:
|
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:
|
Returns:
|
||||||
None
|
int: the inserted row id
|
||||||
"""
|
"""
|
||||||
|
|
||||||
logging.info("Storing %s",
|
logging.info("Storing %s",
|
||||||
@ -129,12 +142,14 @@ class Cache:
|
|||||||
async with await db_conn.executescript(self.cache_pre_query) as _db_cursor:
|
async with await db_conn.executescript(self.cache_pre_query) as _db_cursor:
|
||||||
async with await db_conn.execute(insert_query, params) as _cursor:
|
async with await db_conn.execute(insert_query, params) as _cursor:
|
||||||
await db_conn.commit()
|
await db_conn.commit()
|
||||||
logging.info("Stored %s!", artistsong.replace("\n", " - "))
|
logging.info("Stored %s to SQLite!", artistsong.replace("\n", " - "))
|
||||||
|
return _cursor.lastrowid
|
||||||
except:
|
except:
|
||||||
logging.critical("Cache storage error!")
|
logging.critical("Cache storage error!")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
async def search(self, artist: str, song: str, **kwargs) -> Optional[LyricsResult]:
|
async def search(self, artist: str, song: str, **kwargs) -> Optional[LyricsResult]:
|
||||||
"""
|
"""
|
||||||
|
@ -6,17 +6,21 @@
|
|||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
import sys
|
import sys
|
||||||
sys.path.insert(1,'..')
|
sys.path.insert(1,'..')
|
||||||
from lyric_search_new import notifier
|
from lyric_search_new import notifier
|
||||||
|
from lyric_search_new.constructors import LyricsResult
|
||||||
import redis.asyncio as redis
|
import redis.asyncio as redis
|
||||||
from redis.commands.search.query import Query
|
from redis.commands.search.query import Query
|
||||||
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
|
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
|
||||||
from redis.commands.search.field import TextField
|
from redis.commands.search.field import TextField
|
||||||
|
from redis.commands.json.path import Path
|
||||||
from . import private
|
from . import private
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
log_level = logging.getLevelName(logger.level)
|
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}*")
|
await self.notifier.send("WARNING", f"Redis cache miss for: \n## *{artist} - {song}*")
|
||||||
return search_res_out
|
return search_res_out
|
||||||
except Exception as e:
|
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()
|
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}")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user