May 2017
Beginner to intermediate
220 pages
5h 2m
English
Now we are ready to build our cache on Redis using the same class interface as the earlier DiskCache class:
import jsonfrom datetime import timedelta from redis import StrictRedisclass RedisCache: def __init__(self, client=None, expires=timedelta(days=30), encoding='utf-8'): # if a client object is not passed then try # connecting to redis at the default localhost port self.client = StrictRedis(host='localhost', port=6379, db=0) if client is None else client self.expires = expires self.encoding = encoding def __getitem__(self, url): """Load value from Redis for the given URL""" record = self.client.get(url) if record: return json.loads(record.decode(self.encoding)) else: raise KeyError(url + ' does not ...
Read now
Unlock full access