<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import tempfile <EOL> try : <EOL> from hashlib import md5 <EOL> except ImportError : <EOL> from md5 import new as md5 <EOL> from itertools import izip <EOL> from time import time <EOL> from werkzeug . posixemulation import rename <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> def _items ( mappingorseq ) : <EOL> """<STR_LIT>""" <EOL> return mappingorseq . iteritems ( ) if hasattr ( mappingorseq , '<STR_LIT>' ) else mappingorseq <EOL> class BaseCache ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default_timeout = <NUM_LIT> ) : <EOL> self . default_timeout = default_timeout <EOL> def get ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def delete ( self , key ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_many ( self , * keys ) : <EOL> """<STR_LIT>""" <EOL> return map ( self . get , keys ) <EOL> def get_dict ( self , * keys ) : <EOL> """<STR_LIT>""" <EOL> return dict ( izip ( keys , self . get_many ( * keys ) ) ) <EOL> def set ( self , key , value , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def add ( self , key , value , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_many ( self , mapping , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> for key , value in _items ( mapping ) : <EOL> self . set ( key , value , timeout ) <EOL> def delete_many ( self , * keys ) : <EOL> """<STR_LIT>""" <EOL> for key in keys : <EOL> self . delete ( key ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def inc ( self , key , delta = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . set ( key , ( self . get ( key ) or <NUM_LIT:0> ) + delta ) <EOL> def dec ( self , key , delta = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . set ( key , ( self . get ( key ) or <NUM_LIT:0> ) - delta ) <EOL> class NullCache ( BaseCache ) : <EOL> """<STR_LIT>""" <EOL> class SimpleCache ( BaseCache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , threshold = <NUM_LIT> , default_timeout = <NUM_LIT> ) : <EOL> BaseCache . __init__ ( self , default_timeout ) <EOL> self . _cache = { } <EOL> self . clear = self . _cache . clear <EOL> self . _threshold = threshold <EOL> def _prune ( self ) : <EOL> if len ( self . _cache ) > self . _threshold : <EOL> now = time ( ) <EOL> for idx , ( key , ( expires , _ ) ) in enumerate ( self . _cache . items ( ) ) : <EOL> if expires <= now or idx % <NUM_LIT:3> == <NUM_LIT:0> : <EOL> self . _cache . pop ( key , None ) <EOL> def get ( self , key ) : <EOL> now = time ( ) <EOL> expires , value = self . _cache . get ( key , ( <NUM_LIT:0> , None ) ) <EOL> if expires > time ( ) : <EOL> return pickle . loads ( value ) <EOL> def set ( self , key , value , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> self . _prune ( ) <EOL> self . _cache [ key ] = ( time ( ) + timeout , pickle . dumps ( value , <EOL> pickle . HIGHEST_PROTOCOL ) ) <EOL> def add ( self , key , value , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> if len ( self . _cache ) > self . _threshold : <EOL> self . _prune ( ) <EOL> item = ( time ( ) + timeout , pickle . dumps ( value , <EOL> pickle . HIGHEST_PROTOCOL ) ) <EOL> self . _cache . setdefault ( key , item ) <EOL> def delete ( self , key ) : <EOL> self . _cache . pop ( key , None ) <EOL> _test_memcached_key = re . compile ( r'<STR_LIT>' ) . match <EOL> class MemcachedCache ( BaseCache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , servers = None , default_timeout = <NUM_LIT> , key_prefix = None ) : <EOL> BaseCache . __init__ ( self , default_timeout ) <EOL> if servers is None or isinstance ( servers , ( list , tuple ) ) : <EOL> if servers is None : <EOL> servers = [ '<STR_LIT>' ] <EOL> self . _client = self . import_preferred_memcache_lib ( servers ) <EOL> if self . _client is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> else : <EOL> self . _client = servers <EOL> self . key_prefix = key_prefix <EOL> def get ( self , key ) : <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> if _test_memcached_key ( key ) : <EOL> return self . _client . get ( key ) <EOL> def get_dict ( self , * keys ) : <EOL> key_mapping = { } <EOL> have_encoded_keys = False <EOL> for key in keys : <EOL> if isinstance ( key , unicode ) : <EOL> encoded_key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> have_encoded_keys = True <EOL> else : <EOL> encoded_key = key <EOL> if self . key_prefix : <EOL> encoded_key = self . key_prefix + encoded_key <EOL> if _test_memcached_key ( key ) : <EOL> key_mapping [ encoded_key ] = key <EOL> d = rv = self . _client . get_multi ( key_mapping . keys ( ) ) <EOL> if have_encoded_keys or self . key_prefix : <EOL> rv = { } <EOL> for key , value in d . iteritems ( ) : <EOL> rv [ key_mapping [ key ] ] = value <EOL> if len ( rv ) < len ( keys ) : <EOL> for key in keys : <EOL> if key not in rv : <EOL> rv [ key ] = None <EOL> return rv <EOL> def add ( self , key , value , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> self . _client . add ( key , value , timeout ) <EOL> def set ( self , key , value , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> self . _client . set ( key , value , timeout ) <EOL> def get_many ( self , * keys ) : <EOL> d = self . get_dict ( * keys ) <EOL> return [ d [ key ] for key in keys ] <EOL> def set_many ( self , mapping , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> new_mapping = { } <EOL> for key , value in _items ( mapping ) : <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> new_mapping [ key ] = value <EOL> self . _client . set_multi ( new_mapping , timeout ) <EOL> def delete ( self , key ) : <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> if _test_memcached_key ( key ) : <EOL> self . _client . delete ( key ) <EOL> def delete_many ( self , * keys ) : <EOL> new_keys = [ ] <EOL> for key in keys : <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> if _test_memcached_key ( key ) : <EOL> new_keys . append ( key ) <EOL> self . _client . delete_multi ( new_keys ) <EOL> def clear ( self ) : <EOL> self . _client . flush_all ( ) <EOL> def inc ( self , key , delta = <NUM_LIT:1> ) : <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> self . _client . incr ( key , delta ) <EOL> def dec ( self , key , delta = <NUM_LIT:1> ) : <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT:utf-8>' ) <EOL> if self . key_prefix : <EOL> key = self . key_prefix + key <EOL> self . _client . decr ( key , delta ) <EOL> def import_preferred_memcache_lib ( self , servers ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import pylibmc <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> return pylibmc . Client ( servers ) <EOL> try : <EOL> from google . appengine . api import memcache <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> return memcache . Client ( ) <EOL> try : <EOL> import memcache <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> return memcache . Client ( servers ) <EOL> GAEMemcachedCache = MemcachedCache <EOL> class RedisCache ( BaseCache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , password = None , <EOL> default_timeout = <NUM_LIT> , key_prefix = None ) : <EOL> BaseCache . __init__ ( self , default_timeout ) <EOL> if isinstance ( host , basestring ) : <EOL> try : <EOL> import redis <EOL> except ImportError : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . _client = redis . Redis ( host = host , port = port , password = password ) <EOL> else : <EOL> self . _client = host <EOL> self . key_prefix = key_prefix or '<STR_LIT>' <EOL> def dump_object ( self , value ) : <EOL> """<STR_LIT>""" <EOL> t = type ( value ) <EOL> if t is int or t is long : <EOL> return str ( value ) <EOL> return '<STR_LIT:!>' + pickle . dumps ( value ) <EOL> def load_object ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> if value . startswith ( '<STR_LIT:!>' ) : <EOL> return pickle . loads ( value [ <NUM_LIT:1> : ] ) <EOL> try : <EOL> return int ( value ) <EOL> except ValueError : <EOL> return value <EOL> def get ( self , key ) : <EOL> return self . load_object ( self . _client . get ( self . key_prefix + key ) ) <EOL> def get_many ( self , * keys ) : <EOL> if self . key_prefix : <EOL> keys = [ self . key_prefix + key for key in keys ] <EOL> return [ self . load_object ( x ) for x in self . _client . mget ( keys ) ] <EOL> def set ( self , key , value , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> dump = self . dump_object ( value ) <EOL> self . _client . setex ( self . key_prefix + key , dump , timeout ) <EOL> def add ( self , key , value , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> dump = self . dump_object ( value ) <EOL> added = self . _client . setnx ( self . key_prefix + key , dump ) <EOL> if added : <EOL> self . _client . expire ( self . key_prefix + key , timeout ) <EOL> def set_many ( self , mapping , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> pipe = self . _client . pipeline ( ) <EOL> for key , value in _items ( mapping ) : <EOL> dump = self . dump_object ( value ) <EOL> pipe . setex ( self . key_prefix + key , dump , timeout ) <EOL> pipe . execute ( ) <EOL> def delete ( self , key ) : <EOL> self . _client . delete ( self . key_prefix + key ) <EOL> def delete_many ( self , * keys ) : <EOL> if not keys : <EOL> return <EOL> if self . key_prefix : <EOL> keys = [ self . key_prefix + key for key in keys ] <EOL> self . _client . delete ( * keys ) <EOL> def clear ( self ) : <EOL> if self . key_prefix : <EOL> keys = self . _client . keys ( self . key_prefix + '<STR_LIT:*>' ) <EOL> if keys : <EOL> self . _client . delete ( * keys ) <EOL> else : <EOL> self . _client . flushdb ( ) <EOL> def inc ( self , key , delta = <NUM_LIT:1> ) : <EOL> return self . _client . incr ( self . key_prefix + key , delta ) <EOL> def dec ( self , key , delta = <NUM_LIT:1> ) : <EOL> return self . _client . decr ( self . key_prefix + key , delta ) <EOL> class FileSystemCache ( BaseCache ) : <EOL> """<STR_LIT>""" <EOL> _fs_transaction_suffix = '<STR_LIT>' <EOL> def __init__ ( self , cache_dir , threshold = <NUM_LIT> , default_timeout = <NUM_LIT> , mode = <NUM_LIT:0> <NUM_LIT> ) : <EOL> BaseCache . __init__ ( self , default_timeout ) <EOL> self . _path = cache_dir <EOL> self . _threshold = threshold <EOL> self . _mode = mode <EOL> if not os . path . exists ( self . _path ) : <EOL> os . makedirs ( self . _path ) <EOL> def _list_dir ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ os . path . join ( self . _path , fn ) for fn in os . listdir ( self . _path ) <EOL> if not fn . endswith ( self . _fs_transaction_suffix ) ] <EOL> def _prune ( self ) : <EOL> entries = self . _list_dir ( ) <EOL> if len ( entries ) > self . _threshold : <EOL> now = time ( ) <EOL> for idx , fname in enumerate ( entries ) : <EOL> remove = False <EOL> f = None <EOL> try : <EOL> try : <EOL> f = open ( fname , '<STR_LIT:rb>' ) <EOL> expires = pickle . load ( f ) <EOL> remove = expires <= now or idx % <NUM_LIT:3> == <NUM_LIT:0> <EOL> finally : <EOL> if f is not None : <EOL> f . close ( ) <EOL> except Exception : <EOL> pass <EOL> if remove : <EOL> try : <EOL> os . remove ( fname ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def clear ( self ) : <EOL> for fname in self . _list_dir ( ) : <EOL> try : <EOL> os . remove ( fname ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def _get_filename ( self , key ) : <EOL> hash = md5 ( key ) . hexdigest ( ) <EOL> return os . path . join ( self . _path , hash ) <EOL> def get ( self , key ) : <EOL> filename = self . _get_filename ( key ) <EOL> try : <EOL> f = open ( filename , '<STR_LIT:rb>' ) <EOL> try : <EOL> if pickle . load ( f ) >= time ( ) : <EOL> return pickle . load ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> os . remove ( filename ) <EOL> except Exception : <EOL> return None <EOL> def add ( self , key , value , timeout = None ) : <EOL> filename = self . _get_filename ( key ) <EOL> if not os . path . exists ( filename ) : <EOL> self . set ( key , value , timeout ) <EOL> def set ( self , key , value , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = self . default_timeout <EOL> filename = self . _get_filename ( key ) <EOL> self . _prune ( ) <EOL> try : <EOL> fd , tmp = tempfile . mkstemp ( suffix = self . _fs_transaction_suffix , <EOL> dir = self . _path ) <EOL> f = os . fdopen ( fd , '<STR_LIT:wb>' ) <EOL> try : <EOL> pickle . dump ( int ( time ( ) + timeout ) , f , <NUM_LIT:1> ) <EOL> pickle . dump ( value , f , pickle . HIGHEST_PROTOCOL ) <EOL> finally : <EOL> f . close ( ) <EOL> rename ( tmp , filename ) <EOL> os . chmod ( filename , self . _mode ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def delete ( self , key ) : <EOL> try : <EOL> os . remove ( self . _get_filename ( key ) ) <EOL> except ( IOError , OSError ) : <EOL> pass </s>
