<s> """<STR_LIT>""" <EOL> import datetime <EOL> import pickle <EOL> import random <EOL> import __main__ <EOL> from google . appengine . ext import db <EOL> from google . appengine . api import memcache <EOL> try : <EOL> import settings <EOL> except : <EOL> import settings_default as settings <EOL> class _AppEngineUtilities_Cache ( db . Model ) : <EOL> cachekey = db . StringProperty ( ) <EOL> createTime = db . DateTimeProperty ( auto_now_add = True ) <EOL> timeout = db . DateTimeProperty ( ) <EOL> value = db . BlobProperty ( ) <EOL> class Cache ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , clean_check_percent = settings . cache [ "<STR_LIT>" ] , <EOL> max_hits_to_clean = settings . cache [ "<STR_LIT>" ] , <EOL> default_timeout = settings . cache [ "<STR_LIT>" ] ) : <EOL> """<STR_LIT>""" <EOL> self . clean_check_percent = clean_check_percent <EOL> self . max_hits_to_clean = max_hits_to_clean <EOL> self . default_timeout = default_timeout <EOL> if random . randint ( <NUM_LIT:1> , <NUM_LIT:100> ) < self . clean_check_percent : <EOL> self . _clean_cache ( ) <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> def _clean_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> query = _AppEngineUtilities_Cache . all ( ) <EOL> query . filter ( '<STR_LIT>' , datetime . datetime . now ( ) ) <EOL> results = query . fetch ( self . max_hits_to_clean ) <EOL> db . delete ( results ) <EOL> return True <EOL> def _validate_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key == None : <EOL> raise KeyError <EOL> return True <EOL> def _validate_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value == None : <EOL> raise ValueError <EOL> return True <EOL> def _validate_timeout ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> if timeout == None : <EOL> timeout = datetime . datetime . now ( ) + datetime . timedelta ( seconds = self . default_timeout ) <EOL> if type ( timeout ) == type ( <NUM_LIT:1> ) : <EOL> timeout = datetime . datetime . now ( ) + datetime . timedelta ( seconds = timeout ) <EOL> if type ( timeout ) != datetime . datetime : <EOL> raise TypeError <EOL> if timeout < datetime . datetime . now ( ) : <EOL> raise ValueError <EOL> return timeout <EOL> def add ( self , key = None , value = None , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> self . _validate_key ( key ) <EOL> self . _validate_value ( value ) <EOL> timeout = self . _validate_timeout ( timeout ) <EOL> if key in self : <EOL> raise KeyError <EOL> cacheEntry = _AppEngineUtilities_Cache ( ) <EOL> cacheEntry . cachekey = key <EOL> cacheEntry . value = pickle . dumps ( value ) <EOL> cacheEntry . timeout = timeout <EOL> try : <EOL> cacheEntry . put ( ) <EOL> except : <EOL> pass <EOL> memcache_timeout = timeout - datetime . datetime . now ( ) <EOL> memcache . set ( '<STR_LIT>' % ( key ) , value , int ( memcache_timeout . seconds ) ) <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> return self . get ( key ) <EOL> def set ( self , key = None , value = None , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> self . _validate_key ( key ) <EOL> self . _validate_value ( value ) <EOL> timeout = self . _validate_timeout ( timeout ) <EOL> cacheEntry = self . _read ( key ) <EOL> if not cacheEntry : <EOL> cacheEntry = _AppEngineUtilities_Cache ( ) <EOL> cacheEntry . cachekey = key <EOL> cacheEntry . value = pickle . dumps ( value ) <EOL> cacheEntry . timeout = timeout <EOL> try : <EOL> cacheEntry . put ( ) <EOL> except : <EOL> pass <EOL> memcache_timeout = timeout - datetime . datetime . now ( ) <EOL> memcache . set ( '<STR_LIT>' % ( key ) , value , int ( memcache_timeout . seconds ) ) <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> return self . get ( key ) <EOL> def _read ( self , key = None ) : <EOL> """<STR_LIT>""" <EOL> query = _AppEngineUtilities_Cache . all ( ) <EOL> query . filter ( '<STR_LIT>' , key ) <EOL> query . filter ( '<STR_LIT>' , datetime . datetime . now ( ) ) <EOL> results = query . fetch ( <NUM_LIT:1> ) <EOL> if len ( results ) is <NUM_LIT:0> : <EOL> return None <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> return results [ <NUM_LIT:0> ] <EOL> def delete ( self , key = None ) : <EOL> """<STR_LIT>""" <EOL> memcache . delete ( '<STR_LIT>' % ( key ) ) <EOL> result = self . _read ( key ) <EOL> if result : <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> result . delete ( ) <EOL> return True <EOL> def get ( self , key ) : <EOL> """<STR_LIT>""" <EOL> mc = memcache . get ( '<STR_LIT>' % ( key ) ) <EOL> if mc : <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> return mc <EOL> result = self . _read ( key ) <EOL> if result : <EOL> timeout = result . timeout - datetime . datetime . now ( ) <EOL> memcache . set ( '<STR_LIT>' % ( key ) , pickle . loads ( result . value ) , <EOL> int ( timeout . seconds ) ) <EOL> if '<STR_LIT>' in __main__ . __dict__ : <EOL> __main__ . AEU_Events . fire_event ( '<STR_LIT>' ) <EOL> return pickle . loads ( result . value ) <EOL> else : <EOL> raise KeyError <EOL> def get_many ( self , keys ) : <EOL> """<STR_LIT>""" <EOL> dict = { } <EOL> for key in keys : <EOL> value = self . get ( key ) <EOL> if value is not None : <EOL> dict [ key ] = val <EOL> return dict <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> return self . set ( key , value ) <EOL> def __delitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . delete ( key ) <EOL> def __contains__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . __getitem__ ( key ) <EOL> except KeyError : <EOL> return False <EOL> return True <EOL> def has_key ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> return self . __contains__ ( keyname ) </s>
