Extrayendo funcionalidad de cache en su propio modulo

This commit is contained in:
Daniel Cortes
2020-06-12 02:40:26 -04:00
parent bb27c57c98
commit b8c79da9ca
2 changed files with 124 additions and 93 deletions

79
fetcher/cache.py Normal file
View File

@@ -0,0 +1,79 @@
import json
import logging
from fetcher import jobs
from utils import get_redis_connection
_log = logging.getLogger('fetcher_cache')
_log.addHandler(logging.NullHandler())
def get_artist(mbid):
_log.info('Intentando obtener artista %s desde redis', mbid)
with get_redis_connection() as redis:
artist = redis.get(f'artist:{mbid}')
if not artist:
_log.info('El artista no estaba en redis')
jobs.load_artist_on_cache.delay(mbid)
return None
_log.info('El artista se encontro en redis')
return json.loads(artist)
def get_disc(mbid):
_log.info('Intentando obtener disco %s desde redis', mbid)
with get_redis_connection() as redis:
disc = redis.get(f'release_group:{mbid}')
if not disc:
_log.info('El disco no estaba en redis')
jobs.load_entities_of_release_group.delay(mbid)
return None
_log.info('El disco se encontro en redis')
return json.loads(disc)
def get_discs_of_artist(mbid, limit, offset):
_log.info('Intentando obtener los discos del arista %s', mbid)
with get_redis_connection() as redis:
key_releases = f'artist:{mbid}:release_groups'
if key_releases not in redis:
_log.debug('%s no existe en redis', key_releases)
jobs.load_artist_on_cache.delay(mbid)
return None, 0
count = int(redis.get(f'{key_releases}:count'))
if count != redis.zcard(key_releases):
_log.debug('La cantidad de release_groups del artista %s no coinciden con el total',
mbid)
jobs.load_artist_on_cache.delay(mbid)
return None, 0
release_ids = redis.zrange(key_releases, offset, limit)
keys = [f'release_group:{mbid}' for mbid in release_ids]
if redis.exists(*keys) != len(keys):
_log.debug('Aun no se cargan todas las release_groups del artista %s', mbid)
jobs.load_artist_on_cache.delay(mbid)
return None, None
_log.info('Se encontraron los discos en redis')
return [json.loads(disc) for disc in redis.mget(keys)]
def get_artist_of_disc(mbid):
_log.info('Intentando obtener el artista del disco %s en redis', mbid)
with get_redis_connection() as redis:
artist_id = redis.get(f'release_group:{mbid}:artist')
if not artist_id:
_log.debug('No se encontro el artista')
jobs.load_entities_of_release_group.delay(mbid)
return None
_log.debug('Se encontro el artista')
return json.loads(redis.get(f'artist:{artist_id}'))