<s> "<STR_LIT>" <EOL> import time <EOL> from contextlib import contextmanager <EOL> from django . core . cache . backends . base import DEFAULT_TIMEOUT , BaseCache <EOL> from django . utils . synch import RWLock <EOL> try : <EOL> from django . utils . six . moves import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> _caches = { } <EOL> _expire_info = { } <EOL> _locks = { } <EOL> @ contextmanager <EOL> def dummy ( ) : <EOL> """<STR_LIT>""" <EOL> yield <EOL> class LocMemCache ( BaseCache ) : <EOL> def __init__ ( self , name , params ) : <EOL> BaseCache . __init__ ( self , params ) <EOL> self . _cache = _caches . setdefault ( name , { } ) <EOL> self . _expire_info = _expire_info . setdefault ( name , { } ) <EOL> self . _lock = _locks . setdefault ( name , RWLock ( ) ) <EOL> def add ( self , key , value , timeout = DEFAULT_TIMEOUT , version = None ) : <EOL> key = self . make_key ( key , version = version ) <EOL> self . validate_key ( key ) <EOL> pickled = pickle . dumps ( value , pickle . HIGHEST_PROTOCOL ) <EOL> with self . _lock . writer ( ) : <EOL> if self . _has_expired ( key ) : <EOL> self . _set ( key , pickled , timeout ) <EOL> return True <EOL> return False <EOL> def get ( self , key , default = None , version = None , acquire_lock = True ) : <EOL> key = self . make_key ( key , version = version ) <EOL> self . validate_key ( key ) <EOL> pickled = None <EOL> with ( self . _lock . reader ( ) if acquire_lock else dummy ( ) ) : <EOL> if not self . _has_expired ( key ) : <EOL> pickled = self . _cache [ key ] <EOL> if pickled is not None : <EOL> try : <EOL> return pickle . loads ( pickled ) <EOL> except pickle . PickleError : <EOL> return default <EOL> with ( self . _lock . writer ( ) if acquire_lock else dummy ( ) ) : <EOL> try : <EOL> del self . _cache [ key ] <EOL> del self . _expire_info [ key ] <EOL> except KeyError : <EOL> pass <EOL> return default <EOL> def _set ( self , key , value , timeout = DEFAULT_TIMEOUT ) : <EOL> if len ( self . _cache ) >= self . _max_entries : <EOL> self . _cull ( ) <EOL> self . _cache [ key ] = value <EOL> self . _expire_info [ key ] = self . get_backend_timeout ( timeout ) <EOL> def set ( self , key , value , timeout = DEFAULT_TIMEOUT , version = None ) : <EOL> key = self . make_key ( key , version = version ) <EOL> self . validate_key ( key ) <EOL> pickled = pickle . dumps ( value , pickle . HIGHEST_PROTOCOL ) <EOL> with self . _lock . writer ( ) : <EOL> self . _set ( key , pickled , timeout ) <EOL> def incr ( self , key , delta = <NUM_LIT:1> , version = None ) : <EOL> with self . _lock . writer ( ) : <EOL> value = self . get ( key , version = version , acquire_lock = False ) <EOL> if value is None : <EOL> raise ValueError ( "<STR_LIT>" % key ) <EOL> new_value = value + delta <EOL> key = self . make_key ( key , version = version ) <EOL> pickled = pickle . dumps ( new_value , pickle . HIGHEST_PROTOCOL ) <EOL> self . _cache [ key ] = pickled <EOL> return new_value <EOL> def has_key ( self , key , version = None ) : <EOL> key = self . make_key ( key , version = version ) <EOL> self . validate_key ( key ) <EOL> with self . _lock . reader ( ) : <EOL> if not self . _has_expired ( key ) : <EOL> return True <EOL> with self . _lock . writer ( ) : <EOL> try : <EOL> del self . _cache [ key ] <EOL> del self . _expire_info [ key ] <EOL> except KeyError : <EOL> pass <EOL> return False <EOL> def _has_expired ( self , key ) : <EOL> exp = self . _expire_info . get ( key , - <NUM_LIT:1> ) <EOL> if exp is None or exp > time . time ( ) : <EOL> return False <EOL> return True <EOL> def _cull ( self ) : <EOL> if self . _cull_frequency == <NUM_LIT:0> : <EOL> self . clear ( ) <EOL> else : <EOL> doomed = [ k for ( i , k ) in enumerate ( self . _cache ) if i % self . _cull_frequency == <NUM_LIT:0> ] <EOL> for k in doomed : <EOL> self . _delete ( k ) <EOL> def _delete ( self , key ) : <EOL> try : <EOL> del self . _cache [ key ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> del self . _expire_info [ key ] <EOL> except KeyError : <EOL> pass <EOL> def delete ( self , key , version = None ) : <EOL> key = self . make_key ( key , version = version ) <EOL> self . validate_key ( key ) <EOL> with self . _lock . writer ( ) : <EOL> self . _delete ( key ) <EOL> def clear ( self ) : <EOL> self . _cache . clear ( ) <EOL> self . _expire_info . clear ( ) </s>
