<s> """<STR_LIT>""" <EOL> import memcache <EOL> import time <EOL> import os <EOL> from google . appengine . api import apiproxy_stub <EOL> from google . appengine . api . memcache import memcache_service_pb <EOL> MemcacheSetResponse = memcache_service_pb . MemcacheSetResponse <EOL> MemcacheSetRequest = memcache_service_pb . MemcacheSetRequest <EOL> MemcacheIncrementRequest = memcache_service_pb . MemcacheIncrementRequest <EOL> MemcacheDeleteResponse = memcache_service_pb . MemcacheDeleteResponse <EOL> class CacheEntry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , expiration , flags , gettime ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( value , basestring ) <EOL> assert isinstance ( expiration , ( int , long ) ) <EOL> self . _gettime = gettime <EOL> self . value = value <EOL> self . flags = flags <EOL> self . created_time = self . _gettime ( ) <EOL> self . will_expire = expiration != <NUM_LIT:0> <EOL> self . locked = False <EOL> self . _SetExpiration ( expiration ) <EOL> def _SetExpiration ( self , expiration ) : <EOL> """<STR_LIT>""" <EOL> if expiration > ( <NUM_LIT> * <NUM_LIT:30> ) : <EOL> self . expiration_time = expiration <EOL> else : <EOL> self . expiration_time = self . _gettime ( ) + expiration <EOL> def CheckExpired ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . will_expire and self . _gettime ( ) >= self . expiration_time <EOL> def ExpireAndLock ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> self . will_expire = True <EOL> self . locked = True <EOL> self . _SetExpiration ( timeout ) <EOL> def CheckLocked ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . locked and not self . CheckExpired ( ) <EOL> class MemcacheService ( apiproxy_stub . APIProxyStub ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , gettime = time . time , service_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> super ( MemcacheService , self ) . __init__ ( service_name ) <EOL> self . _gettime = gettime <EOL> f = open ( "<STR_LIT>" ) <EOL> all_ips = f . read ( ) . split ( "<STR_LIT:\n>" ) <EOL> f . close ( ) <EOL> memcaches = [ ip + "<STR_LIT>" for ip in all_ips if ip != '<STR_LIT>' ] <EOL> self . _memcache = memcache . Client ( memcaches , debug = <NUM_LIT:0> ) <EOL> self . _ResetStats ( ) <EOL> self . _the_cache = { } <EOL> def _ResetStats ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _hits = <NUM_LIT:0> <EOL> self . _misses = <NUM_LIT:0> <EOL> self . _byte_hits = <NUM_LIT:0> <EOL> self . _cache_creation_time = self . _gettime ( ) <EOL> def _GetKey ( self , namespace , key ) : <EOL> """<STR_LIT>""" <EOL> appname = os . environ [ '<STR_LIT>' ] <EOL> internal_key = "<STR_LIT>" + appname + "<STR_LIT>" + namespace + "<STR_LIT>" + key <EOL> entry = self . _memcache . get ( internal_key ) <EOL> if entry is None : <EOL> return None <EOL> else : <EOL> return entry <EOL> def _Dynamic_Get ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> namespace = request . name_space ( ) <EOL> keys = set ( request . key_list ( ) ) <EOL> for key in keys : <EOL> value = self . _GetKey ( namespace , key ) <EOL> if value is None : <EOL> continue <EOL> item = response . add_item ( ) <EOL> item . set_key ( key ) <EOL> item . set_value ( value ) <EOL> def _Dynamic_Set ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> namespace = request . name_space ( ) <EOL> for item in request . item_list ( ) : <EOL> key = item . key ( ) <EOL> set_policy = item . set_policy ( ) <EOL> old_entry = self . _GetKey ( namespace , key ) <EOL> set_status = MemcacheSetResponse . NOT_STORED <EOL> if ( ( set_policy == MemcacheSetRequest . SET ) or <EOL> ( set_policy == MemcacheSetRequest . ADD and old_entry is None ) or <EOL> ( set_policy == MemcacheSetRequest . REPLACE and old_entry is not None ) ) : <EOL> if ( old_entry is None or <EOL> set_policy == MemcacheSetRequest . SET <EOL> or not old_entry . CheckLocked ( ) ) : <EOL> appname = os . environ [ '<STR_LIT>' ] <EOL> internal_key = "<STR_LIT>" + appname + "<STR_LIT>" + namespace + "<STR_LIT>" + key <EOL> if self . _memcache . set ( internal_key , item . value ( ) , item . expiration_time ( ) ) : <EOL> set_status = MemcacheSetResponse . STORED <EOL> response . add_set_status ( set_status ) <EOL> def _Dynamic_Delete ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> namespace = request . name_space ( ) <EOL> for item in request . item_list ( ) : <EOL> key = item . key ( ) <EOL> entry = self . _GetKey ( namespace , key ) <EOL> delete_status = MemcacheDeleteResponse . DELETED <EOL> if entry is None : <EOL> delete_status = MemcacheDeleteResponse . NOT_FOUND <EOL> else : <EOL> appname = os . environ [ '<STR_LIT>' ] <EOL> internal_key = "<STR_LIT>" + appname + "<STR_LIT>" + namespace + "<STR_LIT>" + key <EOL> self . _memcache . delete ( internal_key ) <EOL> response . add_delete_status ( delete_status ) <EOL> def _Dynamic_Increment ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> namespace = request . name_space ( ) <EOL> key = request . key ( ) <EOL> delta = request . delta ( ) <EOL> new_value = <NUM_LIT:0> <EOL> try : <EOL> appname = os . environ [ '<STR_LIT>' ] <EOL> internal_key = "<STR_LIT>" + appname + "<STR_LIT>" + namespace + "<STR_LIT>" + key <EOL> if request . direction ( ) == MemcacheIncrementRequest . INCREMENT : <EOL> new_value = self . _memcache . incr ( internal_key , delta ) <EOL> elif request . direction ( ) == MemcacheIncrementRequest . DECREMENT : <EOL> new_value = self . _memcache . decr ( internal_key , delta ) <EOL> else : <EOL> raise ValueError <EOL> except ValueError : <EOL> return <EOL> response . set_new_value ( new_value ) <EOL> def _Dynamic_FlushAll ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . _memcache . flush_all ( ) <EOL> def _Dynamic_Stats ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> mc_stats = self . _memcache . get_stats ( ) <EOL> hits = <NUM_LIT:0> <EOL> misses = <NUM_LIT:0> <EOL> bytes_written = <NUM_LIT:0> <EOL> total_items = <NUM_LIT:0> <EOL> bytes = <NUM_LIT:0> <EOL> oldest_item_age = <NUM_LIT:0> <EOL> for mc in mc_stats : <EOL> memcache_stats = mc [ <NUM_LIT:1> ] <EOL> hits += int ( memcache_stats [ '<STR_LIT>' ] ) <EOL> misses += int ( memcache_stats [ '<STR_LIT>' ] ) <EOL> bytes_written += int ( memcache_stats [ '<STR_LIT>' ] ) <EOL> bytes += int ( memcache_stats [ '<STR_LIT>' ] ) <EOL> stats = response . mutable_stats ( ) <EOL> stats . set_hits ( hits ) <EOL> stats . set_misses ( misses ) <EOL> stats . set_byte_hits ( bytes_written ) <EOL> stats . set_items ( total_items ) <EOL> stats . set_bytes ( bytes ) <EOL> stats . set_oldest_item_age ( oldest_item_age ) </s>
