Created
April 1, 2018 09:46
-
-
Save ripiuk/61ad0cf32a81411175284944e1b3c8a1 to your computer and use it in GitHub Desktop.
An example of an interface for using redis as a cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis | |
import logging | |
def create_logger(logger_name): | |
logger = logging.getLogger(logger_name) | |
return logger | |
class BatchDateCache: | |
BATCH_DATE_PREFIX = 'batch_date:' | |
BATCH_DATE_TTL_SEC = 24 * 60 * 60 # destroy record after 24 hours (TTL) | |
HOST = '127.0.0.10' | |
PORT = 6379 | |
DB = 1 | |
def __init__(self, context): | |
super().__init__() | |
self.logger = create_logger(type(self).__name__) | |
self.context = context | |
redis_connection_pool = redis.ConnectionPool(host=self.HOST, port=self.PORT, db=self.DB) | |
self.redis = redis.Redis(connection_pool=redis_connection_pool) | |
def add_batch_date(self, assignment_id: str, batch_date: str) -> None: | |
""" | |
The method sets a batch_date to the assignment_id | |
if it does not already exist in the cache | |
:param assignment_id: e.g. "2147483647" | |
:param batch_date: e.g. "21/01/18" | |
:return: None | |
""" | |
self.logger.debug("Adding a batch_date %s to the assignment id %s", batch_date, assignment_id) | |
key = '{}{}'.format(self.BATCH_DATE_PREFIX, assignment_id) | |
assignment_exist = self.redis.exists(key) | |
if assignment_exist: | |
self.logger.error("Assignment id %s already exists in the batch date cache", assignment_id) | |
return | |
self.redis.set(key, batch_date, ex=self.BATCH_DATE_TTL_SEC) | |
def get_batch_date(self, assignment_id: str, del_after: bool=False) -> str or None: | |
""" | |
The method returns a batch date for the assignment_id | |
and can remove the record from the cache after getting a value | |
:param assignment_id: e.g. "2147483647" | |
:param del_after: flag to remove the assignment_id from the cache after getting a batch date | |
:return: a batch date for the assignment_id or None | |
""" | |
self.logger.debug("Getting a batch_date for the assignment id %s", assignment_id) | |
key = '{}{}'.format(self.BATCH_DATE_PREFIX, assignment_id) | |
batch_date = self.redis.get(key) | |
if not batch_date: | |
self.logger.error("The key %s does not exist in the batch date cache", key) | |
return | |
if del_after: | |
self._del_assignment(key) | |
return batch_date.decode('utf-8') | |
def _del_assignment(self, key: str) -> None: | |
self.redis.delete(key) | |
self.logger.debug("The key %s was deleted from the batch date cache", key) | |
def purge(self) -> None: | |
self.logger.info("Clearing the batch date cache for assignment ids") | |
for key in self.redis.keys('{}*'.format(self.BATCH_DATE_PREFIX)): | |
self.redis.delete(key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment