<s> from collections import defaultdict <EOL> from redis_cache . backends . base import BaseRedisCache <EOL> from redis_cache . compat import DEFAULT_TIMEOUT <EOL> from redis_cache . sharder import HashRing <EOL> class ShardedRedisCache ( BaseRedisCache ) : <EOL> def __init__ ( self , server , params ) : <EOL> super ( ShardedRedisCache , self ) . __init__ ( server , params ) <EOL> self . sharder = HashRing ( ) <EOL> for server in self . servers : <EOL> client = self . create_client ( server ) <EOL> self . clients [ client . connection_pool . connection_identifier ] = client <EOL> self . sharder . add ( client . connection_pool . connection_identifier ) <EOL> self . client_list = self . clients . values ( ) <EOL> def get_client ( self , key , write = False ) : <EOL> node = self . sharder . get_node ( key ) <EOL> return self . clients [ node ] <EOL> def shard ( self , keys , write = False , version = None ) : <EOL> """<STR_LIT>""" <EOL> clients = defaultdict ( list ) <EOL> for key in keys : <EOL> clients [ self . get_client ( key , write ) ] . append ( <EOL> self . make_key ( key , version ) <EOL> ) <EOL> return clients <EOL> def delete_many ( self , keys , version = None ) : <EOL> """<STR_LIT>""" <EOL> clients = self . shard ( keys , write = True , version = version ) <EOL> for client , keys in clients . items ( ) : <EOL> self . _delete_many ( client , keys ) <EOL> def clear ( self , version = None ) : <EOL> """<STR_LIT>""" <EOL> if version is None : <EOL> for client in self . clients . values ( ) : <EOL> self . _clear ( client ) <EOL> else : <EOL> self . delete_pattern ( '<STR_LIT:*>' , version = version ) <EOL> def get_many ( self , keys , version = None ) : <EOL> data = { } <EOL> clients = self . shard ( keys , version = version ) <EOL> for client , versioned_keys in clients . items ( ) : <EOL> original_keys = [ key . _original_key for key in versioned_keys ] <EOL> data . update ( <EOL> self . _get_many ( <EOL> client , <EOL> original_keys , <EOL> versioned_keys = versioned_keys <EOL> ) <EOL> ) <EOL> return data <EOL> def set_many ( self , data , timeout = DEFAULT_TIMEOUT , version = None ) : <EOL> """<STR_LIT>""" <EOL> timeout = self . get_timeout ( timeout ) <EOL> clients = self . shard ( data . keys ( ) , write = True , version = version ) <EOL> if timeout is None : <EOL> for client , keys in clients . items ( ) : <EOL> subset = { } <EOL> for key in keys : <EOL> subset [ key ] = self . prep_value ( data [ key . _original_key ] ) <EOL> self . _set_many ( client , subset ) <EOL> return <EOL> for client , keys in clients . items ( ) : <EOL> pipeline = client . pipeline ( ) <EOL> for key in keys : <EOL> value = self . prep_value ( data [ key . _original_key ] ) <EOL> self . _set ( pipeline , key , value , timeout ) <EOL> pipeline . execute ( ) <EOL> def incr_version ( self , key , delta = <NUM_LIT:1> , version = None ) : <EOL> """<STR_LIT>""" <EOL> if version is None : <EOL> version = self . version <EOL> client = self . get_client ( key , write = True ) <EOL> old = self . make_key ( key , version = version ) <EOL> new = self . make_key ( key , version = version + delta ) <EOL> return self . _incr_version ( client , old , new , delta , version ) <EOL> def delete_pattern ( self , pattern , version = None ) : <EOL> pattern = self . make_key ( pattern , version = version ) <EOL> for client in self . clients . values ( ) : <EOL> self . _delete_pattern ( client , pattern ) <EOL> def reinsert_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> for client in self . clients . values ( ) : <EOL> self . _reinsert_keys ( client ) </s>
