<s> import json <EOL> import logging <EOL> from functools import update_wrapper , wraps <EOL> from datetime import datetime , timedelta <EOL> from exceptions import BaseException <EOL> import cPickle as pickle <EOL> import types <EOL> from werkzeug . routing import Rule <EOL> from werkzeug . wrappers import Response <EOL> def _make_key_str ( name , * args , ** kwargs ) : <EOL> key_str = name <EOL> if args : <EOL> key_str += '<STR_LIT>' + '<STR_LIT>' . join ( [ str ( a ) for a in args ] ) <EOL> if kwargs : <EOL> key_str += '<STR_LIT>' + '<STR_LIT>' . join ( [ '<STR_LIT>' % ( str ( k ) , str ( v ) ) for k , v in sorted ( kwargs . items ( ) , key = lambda x : x [ <NUM_LIT:0> ] ) ] ) <EOL> return key_str <EOL> _NOOP = lambda x : x <EOL> def query_ttl_strategy ( data ) : <EOL> import chat2query <EOL> import measures <EOL> if '<STR_LIT:q>' in data : <EOL> query = chat2query . parse ( data [ '<STR_LIT:q>' ] ) <EOL> config = measures . get_measure_manifest ( ) . get ( query . measure , { } ) <EOL> ttl = config . get ( '<STR_LIT>' , None ) <EOL> if ttl : <EOL> logging . getLogger ( ) . debug ( '<STR_LIT>' , ttl ) <EOL> return ttl <EOL> return None <EOL> def _default_strategy ( data ) : <EOL> return None <EOL> def response_dumps ( response ) : <EOL> return { <EOL> '<STR_LIT:data>' : response . data , <EOL> '<STR_LIT>' : response . content_type <EOL> } <EOL> def response_loads ( data ) : <EOL> return Response ( data [ '<STR_LIT:data>' ] , mimetype = data [ '<STR_LIT>' ] ) <EOL> def cacheable ( fn ) : <EOL> @ wraps ( fn ) <EOL> def _cache ( self , * args , ** kwargs ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> logging . getLogger ( ) . warn ( '<STR_LIT>' , self ) <EOL> return fn ( self , * args , ** kwargs ) <EOL> additional_args = getattr ( self , '<STR_LIT:args>' , { } ) <EOL> cache_impl = self . cache <EOL> name = '<STR_LIT>' % ( self . __class__ . __name__ , fn . __name__ ) <EOL> key = _make_key_str ( name , * args , ** dict ( additional_args . items ( ) + kwargs . items ( ) ) ) <EOL> try : <EOL> v = cache_impl . get ( key ) <EOL> if not v : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> v = fn ( self , * args , ** kwargs ) <EOL> cache_impl . set ( key , v ) <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> return v <EOL> except Exception as e : <EOL> self . logger . warn ( '<STR_LIT>' ) <EOL> self . logger . exception ( e ) <EOL> v = fn ( self , * args , ** kwargs ) <EOL> try : <EOL> cache_impl . set ( key , v ) <EOL> finally : <EOL> return v <EOL> return update_wrapper ( _cache , fn ) <EOL> class Cache ( object ) : <EOL> def __setitem__ ( self , key , value ) : <EOL> return self . set ( key , value ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . get ( key ) <EOL> def __delitem__ ( self , key ) : <EOL> return self . rem ( key ) <EOL> def trap ( fn ) : <EOL> def _trap ( * args , ** kwargs ) : <EOL> try : <EOL> return fn ( * args , ** kwargs ) <EOL> except BaseException as e : <EOL> logging . getLogger ( ) . error ( '<STR_LIT>' , fn . __name__ , args , kwargs ) <EOL> logging . getLogger ( ) . exception ( e ) <EOL> return _trap <EOL> class MongoCache ( Cache ) : <EOL> def __init__ ( self , name , ds , ttl = <NUM_LIT> , ttl_strategy = _default_strategy , dumps = _NOOP , loads = _NOOP , logger = logging . getLogger ( ) , ** kwargs ) : <EOL> self . ttl = ttl <EOL> self . store = ds . Caches [ name ] <EOL> self . dumps = dumps <EOL> self . loads = loads <EOL> self . ttl_strategy = ttl_strategy <EOL> self . logger = logger <EOL> @ trap <EOL> def set ( self , key , val , ttl = None ) : <EOL> t = ttl or self . ttl_strategy ( val ) or self . ttl <EOL> n = datetime . utcnow ( ) + timedelta ( seconds = t ) <EOL> p = { <EOL> '<STR_LIT>' : key , <EOL> '<STR_LIT:t>' : n , <EOL> '<STR_LIT:d>' : self . dumps ( val ) <EOL> } <EOL> self . logger . debug ( '<STR_LIT>' , key , t ) <EOL> item = self . store . save ( p ) <EOL> return item is not None <EOL> @ trap <EOL> def get ( self , key ) : <EOL> n = datetime . utcnow ( ) <EOL> item = self . store . find_one ( { '<STR_LIT>' : key } ) <EOL> if item : <EOL> item [ '<STR_LIT:d>' ] = self . loads ( item [ '<STR_LIT:d>' ] ) <EOL> exp = item [ '<STR_LIT:t>' ] <EOL> if exp >= n : <EOL> self . logger . debug ( '<STR_LIT>' , key ) <EOL> return item [ '<STR_LIT:d>' ] <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' , key ) <EOL> self . store . remove ( { '<STR_LIT>' : key } ) <EOL> return None <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' , key ) <EOL> return None <EOL> def rem ( self , key ) : <EOL> n = datetime . utcnow ( ) <EOL> item = self . store . find_one ( { '<STR_LIT>' : key } ) <EOL> if item : <EOL> self . logger . debug ( '<STR_LIT>' , key ) <EOL> self . store . remove ( item ) <EOL> return True <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' , key ) <EOL> return False </s>
