<s> "<STR_LIT>" <EOL> import functools as ft <EOL> import math <EOL> import random <EOL> import tempfile <EOL> import time <EOL> from . core import Cache , ENOVAL <EOL> class StampedeBarrier ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cache = None , expire = None ) : <EOL> if isinstance ( cache , Cache ) : <EOL> pass <EOL> elif cache is None : <EOL> cache = Cache ( tempfile . mkdtemp ( ) ) <EOL> else : <EOL> cache = Cache ( cache ) <EOL> self . _cache = cache <EOL> self . _expire = expire <EOL> def __call__ ( self , func ) : <EOL> cache = self . _cache <EOL> expire = self . _expire <EOL> ft . wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> key = ( args , kwargs ) <EOL> try : <EOL> result , expire_time , delta = cache . get ( <EOL> key , default = ENOVAL , expire_time = True , tag = True <EOL> ) <EOL> if result is ENOVAL : <EOL> raise KeyError <EOL> now = time . time ( ) <EOL> ttl = expire_time - now <EOL> if ( - delta * math . log ( random . random ( ) ) ) < ttl : <EOL> return result <EOL> except KeyError : <EOL> pass <EOL> now = time . time ( ) <EOL> result = func ( * args , ** kwargs ) <EOL> delta = time . time ( ) - now <EOL> cache . set ( key , result , expire = expire , tag = delta ) <EOL> return result <EOL> return wrapper </s>
