Sigo haciendo feliz a pylint

This commit is contained in:
Daniel Cortes
2020-06-07 22:29:38 -04:00
parent 40a5d37bb8
commit a0a5857a8b
2 changed files with 53 additions and 39 deletions

View File

@@ -1,6 +1,7 @@
"""Modulo que contiene la clase/decorador Cache, contiene su logger y una conexión a redis"""
import json
import logging
from blake3 import blake3
from blake3 import blake3 # pylint: disable=no-name-in-module
from redis import Redis
from django.conf import settings
@@ -20,10 +21,10 @@ class Cache:
It is assumed that the function will returns a dictionary that can be formatted to json.
"""
"""If caching is enabled"""
# If caching is enabled
enabled = settings.CUSTOM_CACHE['enabled']
"""Time to expire, by default a week"""
# Time to expire, by default a week
expire = 60 * 60 * 24 * 7
def __init__(self, function):
@@ -34,21 +35,23 @@ class Cache:
_log.info('Cache is disabled, executing function directly')
return self.function(*args, **kwargs)
_log.info(f'Caching function {self.function.__name__} with argument list {args} and dictionary {kwargs}')
_log.info('Caching function %s with argument list %s and dictionary %s',
self.function.__name__, args, kwargs)
key = f'{self.function.__name__}:{self.create_hash_key(args, kwargs)}'
_log.debug(f'Resolved key for function is "{key}"')
_log.debug('Resolved key for function is "%s"', key)
if _redis.exists(key):
_log.info(f'Key was in cache')
_log.info('Key was in cache')
result = json.loads(_redis.get(key))
return result
else:
_log.info('Key was not in cache')
result = self.function(*args, **kwargs)
_redis.set(key, json.dumps(result), ex=self.expire)
return result
_log.info('Key was not in cache')
result = self.function(*args, **kwargs)
_redis.set(key, json.dumps(result), ex=self.expire)
return result
@staticmethod
def create_hash_key(args, kwargs):
"""Crea la key dados unos argumentos arbitrarios"""
return blake3((str(args) + str(kwargs)).encode('utf-8')).hexdigest()