<s> import os <EOL> import sys <EOL> import hashlib <EOL> import logging <EOL> import json <EOL> from cPickle import PicklingError <EOL> import redis <EOL> from totalimpact import REDIS_CACHE_DATABASE_NUMBER <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> cache_client = redis . from_url ( os . getenv ( "<STR_LIT>" ) , REDIS_CACHE_DATABASE_NUMBER ) <EOL> MAX_PAYLOAD_SIZE_BYTES = <NUM_LIT:1000> * <NUM_LIT:1000> <EOL> MAX_CACHE_SIZE_BYTES = <NUM_LIT:100> * <NUM_LIT:1000> * <NUM_LIT:1000> <EOL> class CacheException ( Exception ) : <EOL> pass <EOL> class Cache ( object ) : <EOL> """<STR_LIT>""" <EOL> def _build_hash_key ( self , key ) : <EOL> json_key = json . dumps ( key ) <EOL> hash_key = hashlib . md5 ( json_key . encode ( "<STR_LIT:utf-8>" ) ) . hexdigest ( ) <EOL> return hash_key <EOL> def _get_client ( self ) : <EOL> return cache_client <EOL> def __init__ ( self , max_cache_age = <NUM_LIT> * <NUM_LIT> ) : <EOL> self . max_cache_age = max_cache_age <EOL> self . flush_cache ( ) <EOL> def flush_cache ( self ) : <EOL> mc = self . _get_client ( ) <EOL> def get_cache_entry ( self , key ) : <EOL> """<STR_LIT>""" <EOL> mc = self . _get_client ( ) <EOL> hash_key = self . _build_hash_key ( key ) <EOL> response = mc . get ( hash_key ) <EOL> if response : <EOL> response = json . loads ( response ) <EOL> return response <EOL> def set_cache_entry ( self , key , data ) : <EOL> """<STR_LIT>""" <EOL> if sys . getsizeof ( data [ "<STR_LIT:text>" ] ) > MAX_PAYLOAD_SIZE_BYTES : <EOL> logger . debug ( u"<STR_LIT>" ) <EOL> return None <EOL> mc = self . _get_client ( ) <EOL> if mc . info ( ) [ "<STR_LIT>" ] >= MAX_CACHE_SIZE_BYTES : <EOL> logger . debug ( u"<STR_LIT>" ) <EOL> return None <EOL> hash_key = self . _build_hash_key ( key ) <EOL> set_response = mc . set ( hash_key , json . dumps ( data ) ) <EOL> mc . expire ( hash_key , self . max_cache_age ) <EOL> if not set_response : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> raise CacheException ( "<STR_LIT>" ) <EOL> return set_response </s>
