7(CdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z GddZ GddZ Gd d Z Gd d Zdddejejfd ZddZ ddZdS)zDisk Cache Recipes N)ENOVAL args_to_key full_namec,eZdZdZddZdZdZdZdS)AverageraLRecipe for calculating a running average. Sometimes known as "online statistics," the running average maintains the total and count. The average can then be calculated at any time. Assumes the key will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the key is not evicted. >>> import diskcache >>> cache = diskcache.FanoutCache() >>> ave = Averager(cache, 'latency') >>> ave.add(0.080) >>> ave.add(0.120) >>> ave.get() 0.1 >>> ave.add(0.160) >>> ave.pop() 0.12 >>> print(ave.get()) None Nc>||_||_||_||_dSN_cache_key_expire_tagselfcachekeyexpiretags q/builddir/build/BUILD/imunify360-venv-2.6.2/opt/imunify360/venv/lib/python3.11/site-packages/diskcache/recipes.py__init__zAverager.__init__&"    c(|jd5|j|jd\}}||z }|dz }|j|j||f|j|jddddS#1swxYwYdS)zAdd `value` to average.TretrygrdefaultrrrN)r transactgetr setrr)rvaluetotalcounts raddz Averager.add,s [ ! ! ! - -  ;??49h?GGLE5 UNE QJE KOO |I                      sABB B cf|j|jdd\}}|dkrdn||z S)z:Get current average or return `None` if count equals zero.rTrrrN)r r"r rr%r&s rr"z Averager.get97{ty($OO uzzttuu}4rcf|j|jdd\}}|dkrdn||z S)z&Return current average and delete key.rTr)rN)r popr r*s rr-z Averager.pop>r+rNN)__name__ __module__ __qualname____doc__rr'r"r-rrrrs_.   555 55555rrc8eZdZdZd dZdZdZdZdZdZ dS) LockanRecipe for cross-process and cross-thread lock. Assumes the key will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the key is not evicted. >>> import diskcache >>> cache = diskcache.Cache() >>> lock = Lock(cache, 'report-123') >>> lock.acquire() >>> lock.release() >>> with lock: ... pass Nc>||_||_||_||_dSr r rs rrz Lock.__init__Trrc |j|jd|j|jd}|rdSt jdG)z'Acquire lock using spin-lock algorithm.TNrrrMbP?)r r'r rrtimesleep)raddeds racquirez Lock.acquireZsZ KOO |I $E  Ju    rcH|j|jddS)zRelease lock by deleting key.TrN)r deleter rs rreleasez Lock.releasehs% 49D11111rc|j|jvS)z$Return true if the lock is acquired.)r r r@s rlockedz Lock.lockedlsyDK''rc.|dSr r=r@s r __enter__zLock.__enter__p rc.|dSr rArexc_infos r__exit__z Lock.__exit__srGrr.) r/r0r1r2rr=rArCrFrLr3rrr5r5Ds}     222(((rr5c2eZdZdZddZdZdZdZdZdS) RLocka)Recipe for cross-process and cross-thread re-entrant lock. Assumes the key will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the key is not evicted. >>> import diskcache >>> cache = diskcache.Cache() >>> rlock = RLock(cache, 'user-123') >>> rlock.acquire() >>> rlock.acquire() >>> rlock.release() >>> with rlock: ... pass >>> rlock.release() >>> rlock.release() Traceback (most recent call last): ... AssertionError: cannot release un-acquired lock Nc>||_||_||_||_dSr r rs rrzRLock.__init__rrctj}tj}d||} |jd5|j|jd\}}||ks|dkr@|j |j||dzf|j |j  d d d d S d d d n #1swxYwYtj d ) z=Acquire lock by incrementing count using spin-lock algorithm.{}-{}TrNrrrrr Nr9)osgetpid threading get_identformatr r!r"r r#rrr:r;)rpidtidpid_tidr$r&s rr=z RLock.acquiresPikk!##..c** %%D%11  #{ty)LL ue##uzzKOO  %!),#| I $         '1                Ju    sA$CCCctj}tj}d||}|jd5|j|jd\}}||ko|dk}|s Jd|j |j||dz f|j |j d d d d S#1swxYwYd S) z#Release lock by decrementing count.rQTrrRrrzcannot release un-acquired lockrr N) rSrTrUrVrWr r!r"r r#rr)rrXrYrZr$r&is_owneds rrAz RLock.releases'ikk!##..c** [ ! ! ! - -  ;??49i?HHLE5%'5EAIH > >> > >8 KOO  "|I                      sA/CCCc.|dSr rEr@s rrFzRLock.__enter__rGrc.|dSr rIrJs rrLzRLock.__exit__rGrr. r/r0r1r2rr=rArFrLr3rrrNrNwsn* &"rrNc2eZdZdZd dZdZdZdZdZdS) BoundedSemaphorea`Recipe for cross-process and cross-thread bounded semaphore. Assumes the key will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the key is not evicted. >>> import diskcache >>> cache = diskcache.Cache() >>> semaphore = BoundedSemaphore(cache, 'max-cons', value=2) >>> semaphore.acquire() >>> semaphore.acquire() >>> semaphore.release() >>> with semaphore: ... pass >>> semaphore.release() >>> semaphore.release() Traceback (most recent call last): ... AssertionError: cannot release un-acquired semaphore rNcL||_||_||_||_||_dSr )r r _valuerr)rrrr$rrs rrzBoundedSemaphore.__init__s)     rcj |jd5|j|j|j}|dkr>|j|j|dz |j|j ddddS dddn #1swxYwYtj d) zBAcquire semaphore by decrementing value using spin-lock algorithm.Trrrrr Nr9) r r!r"r rcr#rrr:r;rr$s rr=zBoundedSemaphore.acquires %%D%11    4;GG199KOO  #| I $                         Ju    sABBBcH|jd5|j|j|j}|j|ks Jd|dz }|j|j||j|jddddS#1swxYwYdS)z(Release semaphore by incrementing value.Trrz$cannot release un-acquired semaphorerr N)r r!r"r rcr#rrres rrAzBoundedSemaphore.releases [ ! ! ! - -  KOODIt{OCCE;&&&(N&&& QJE KOO |I                      sA.BBBc.|dSr rEr@s rrFzBoundedSemaphore.__enter__rGrc.|dSr rIrJs rrLzBoundedSemaphore.__exit__rGr)rNNr_r3rrrarasn*      rrac.fd}|S)a(Decorator to throttle calls to function. Assumes keys will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the keys are not evicted. >>> import diskcache, time >>> cache = diskcache.Cache() >>> count = 0 >>> @throttle(cache, 2, 1) # 2 calls per 1 second ... def increment(): ... global count ... count += 1 >>> start = time.time() >>> while (time.time() - start) <= 2: ... increment() >>> count in (6, 7) # 6 or 7 calls depending on CPU load True c t z tn }|f dtj fd}|S)NTr8c d5 \}} }|||z zz }d}|kr |dz fn+|dkr ||dz fnd|z z }dddn #1swxYwY|r  |nn |i|S)NTrrr)r!r"r#)argskwargslasttallynowdelayrr&rfuncrrate sleep_func time_funcs rwrapperz,throttle..decorator..wrapper"sG ^^$^// 3 3"'))C..KD%#)++CcDjD00EEu}} #UQY'7@@@@! #UQY'7@@@@!"Ud 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3Ju%%%%# &4((( (sA>B##B'*B')floatrr# functoolswraps) rrrprvrrsrr&rnamesecondsrtrrus ` @@r decoratorzthrottle..decoratorsuW~~%!%iooo4ikk #U|F4 HHH    ) ) ) ) ) ) ) ) ) ) )   ),rr3) rr&r{rzrrrurtr|s ```````` rthrottler}sN<> rc"fd}|S)aBarrier to calling decorated function. Supports different kinds of locks: Lock, RLock, BoundedSemaphore. Assumes keys will not be evicted. Set the eviction policy to 'none' on the cache to guarantee the keys are not evicted. >>> import diskcache, time >>> cache = diskcache.Cache() >>> @barrier(cache, Lock) ... def work(num): ... print('worker started') ... time.sleep(1) ... print('worker finished') >>> import multiprocessing.pool >>> pool = multiprocessing.pool.ThreadPool(2) >>> _ = pool.map(work, range(2)) worker started worker finished worker started worker finished >>> pool.terminate() ctn}|tjfd}|S)Nr cL5|i|cdddS#1swxYwYdSr r3)rlrmrrlocks rrvz+barrier..decorator..wrapper\s - -tT,V,, - - - - - - - - - - - - - - - - - -s )rrxry) rrrrvrrr lock_factoryrzrs ` @rr|zbarrier..decoratorXsj!%iooo4|E3v3???    - - - - -   -rr3)rrrzrrr|s````` rbarrierr>s<4          rFr3c*fd}|S)a/ Memoizing cache decorator with cache stampede protection. Cache stampedes are a type of system overload that can occur when parallel computing systems using memoization come under heavy load. This behaviour is sometimes also called dog-piling, cache miss storm, cache choking, or the thundering herd problem. The memoization decorator implements cache stampede protection through early recomputation. Early recomputation of function results will occur probabilistically before expiration in a background thread of execution. Early probabilistic recomputation is based on research by Vattani, A.; Chierichetti, F.; Lowenstein, K. (2015), Optimal Probabilistic Cache Stampede Prevention, VLDB, pp. 886-897, ISSN 2150-8097 If name is set to None (default), the callable name will be determined automatically. If typed is set to True, function arguments of different types will be cached separately. For example, f(3) and f(3.0) will be treated as distinct calls with distinct results. The original underlying function is accessible through the `__wrapped__` attribute. This is useful for introspection, for bypassing the cache, or for rewrapping the function with a different cache. >>> from diskcache import Cache >>> cache = Cache() >>> @memoize_stampede(cache, expire=1) ... def fib(number): ... if number == 0: ... return 0 ... elif number == 1: ... return 1 ... else: ... return fib(number - 1) + fib(number - 2) >>> print(fib(100)) 354224848179261915075 An additional `__cache_key__` attribute can be used to generate the cache key used for the given arguments. >>> key = fib.__cache_key__(100) >>> del cache[key] Remember to call memoize when decorating a callable. If you forget, then a TypeError will occur. :param cache: cache to store callable arguments and return values :param float expire: seconds until arguments expire :param str name: name given for callable (default None, automatic) :param bool typed: cache different types separately (default False) :param str tag: text to associate with arguments (default None) :param set ignore: positional or keyword args to ignore (default ()) :return: callable decorator c tfn ffdtj fd fd}|_S)z/Decorator created by memoize call for callable.Ncntj}|i|}tj|z }||fS)z:Time execution of `func` and return result and time delta.)r:)rlrmstartresultdeltarrs rtimerz2memoize_stampede..decorator..timers<IKKET4*6**FIKK%'E5= rc0 ji  tdd\}}|tur|\}}tj}||z }| zt jt jz|kr|S tfz}|d|d} | r; fd} tj | } d| _ | |Si} |d|dS) z:Wrapper for callable to cache arguments and return values.T)r expire_timerN)rrc5i}|dddddS#1swxYwYdS)NTr8)r#)pairrlrrrrmrrs r recomputezGmemoize_stampede..decorator..wrapper..recomputes"#(5$#9 #9D!II # $'-$'&* &s #488)targetr8r) __cache_key__r"rr:mathlograndomr'rUThreaddaemonrr#)rlrmrrrrrpttl thread_key thread_addedrthreadrbetarrrrrvs`` @rrvz4memoize_stampede..decorator..wrappersw(''888C %   !*!! D+6!! $ ikk!C'FTMDHV]__$=$==DD!M!F9_ $yy  )    #           '-Y???F$(FMLLNNN 5$)&))D IIc4CtI D D D7Nrc*t||S)z,Make key for cache given function arguments.)r)rlrmbaseignoretypeds rrz:memoize_stampede..decorator..__cache_key__stT65&AA Ar)rrxryr) rrrrrrvrrrrrzrrs ` @@@rr|z#memoize_stampede..decorators%)\ $!!w ! ! ! ! !   1 1 1 1 1 1 1 1 1   1 f B B B B B B B!.rr3)rrrzrrrrr|s``````` rmemoize_stampederfsUxDDDDDDDDDDDL r)NNN)NFNrr3)r2rxrrSrrUr:corerrrrr5rNrar;r}rrr3rrrs  00000000003535353535353535l00000000fDDDDDDDDN========H   iz====@%%%%REGBBBBBBr