
    gc                        d Z dZdZddlZddlZddlZddlZddlZddlZddl	m
Z
  G d d      Z G d	 d
ej                  j                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z ej.                  dg d      Ze
j2                  ddfdZe
j6                  dfdZy) z0Extensible memoizing collections and decorators.)
Cache	FIFOCacheLFUCacheLRUCacheMRUCacheRRCache	TLRUCacheTTLCachecachedcachedmethodz5.5.0    N   )keysc                   "    e Zd ZdZd Zd Zd Zy)_DefaultSize c                      yNr   r   self_s     H/var/www/openai/venv/lib/python3.12/site-packages/cachetools/__init__.py__getitem__z_DefaultSize.__getitem__           c                     |dk(  sJ y r   r   )r   r   values      r   __setitem__z_DefaultSize.__setitem__"   s    zzr   c                      yr   r   r   s     r   popz_DefaultSize.pop%   r   r   N)__name__
__module____qualname__	__slots__r   r   r   r   r   r   r   r      s    Ir   r   c                       e Zd ZdZ e       Z e       ZddZd Z	d Z
d Zd Zd Zd	 Zd
 Zd ZddZefdZddZed        Zed        Zed        Zy)r   z?Mutable mapping to serve as a simple cache or cache base class.Nc                     |r|| _         | j                   t        j                   urt               | _        t               | _        d| _        || _        y Nr   )	getsizeofr   dict_Cache__size_Cache__data_Cache__currsize_Cache__maxsizer   maxsizer'   s      r   __init__zCache.__init__0   s<    &DN>>0&DKf r   c                     | j                   j                  dt        | j                        d| j                  d| j
                  dS )N(z
, maxsize=z, currsize=))	__class__r    reprr*   r,   r+   r   s    r   __repr__zCache.__repr__9   s3    NN##NNOO	
 	
r   c                 b    	 | j                   |   S # t        $ r | j                  |      cY S w xY wN)r*   KeyError__missing__r   keys     r   r   zCache.__getitem__A   s6    	);;s## 	)##C((	)s    ..c                    | j                   }| j                  |      }||kD  rt        d      || j                  vs| j                  |   |k  r5| j
                  |z   |kD  r#| j                          | j
                  |z   |kD  r#|| j                  v r|| j                  |   z
  }n|}|| j                  |<   || j                  |<   | xj
                  |z  c_        y )Nzvalue too large)r,   r'   
ValueErrorr*   r)   r+   popitem)r   r<   r   r.   sizediffsizes         r   r   zCache.__setitem__G   s    ..~~e$'>.//dkk!T[[%5%<//D(72 //D(72$++dkk#..HH CC8#r   c                 ~    | j                   j                  |      }| j                  |= | xj                  |z  c_        y r8   )r)   r   r*   r+   )r   r<   r@   s      r   __delitem__zCache.__delitem__W   s.    {{s#KK4r   c                     || j                   v S r8   )r*   r;   s     r   __contains__zCache.__contains__\   s    dkk!!r   c                     t        |      r8   )r9   r;   s     r   r:   zCache.__missing___   s    smr   c                 ,    t        | j                        S r8   )iterr*   r5   s    r   __iter__zCache.__iter__b   s    DKK  r   c                 ,    t        | j                        S r8   )lenr*   r5   s    r   __len__zCache.__len__e   s    4;;r   c                     || v r| |   S |S r8   r   )r   r<   defaults      r   getz	Cache.geth   s    $;9Nr   c                 X    || v r
| |   }| |= |S || j                   u rt        |      |}|S r8   )_Cache__markerr9   r   r<   rN   r   s       r   r   z	Cache.popn   sB    $;IES	
 	 %3-Er   c                 *    || v r| |   }|S |x| |<   }|S r8   r   rR   s       r   
setdefaultzCache.setdefaultx   s.    $;IE  !('DIr   c                     | j                   S )zThe maximum size of the cache.)r,   r5   s    r   r.   zCache.maxsize   s     ~~r   c                     | j                   S )zThe current size of the cache.)r+   r5   s    r   currsizezCache.currsize   s     r   c                      y)z+Return the size of a cache element's value.r   r   )r   s    r   r'   zCache.getsizeof   s     r   r8   )r    r!   r"   __doc__objectrQ   r   r)   r/   r6   r   r   rC   rE   r:   rI   rL   rO   r   rT   propertyr.   rW   staticmethodr'   r   r   r   r   r   )   s    IxH^F!
)$  
"!   (       r   r   c                   Z    e Zd ZdZddZej                  fdZej                  fdZd Zy)r   z/First In First Out (FIFO) cache implementation.Nc                 d    t         j                  | ||       t        j                         | _        y r8   )r   r/   collectionsOrderedDict_FIFOCache__orderr-   s      r   r/   zFIFOCache.__init__   "    tWi0"..0r   c                      || ||       	 | j                   j                  |       y # t        $ r d | j                   |<   Y y w xY wr8   )ra   move_to_endr9   r   r<   r   cache_setitems       r   r   zFIFOCache.__setitem__   sB    dC'	%LL$$S) 	% $DLL	%s   ( AAc                 0     || |       | j                   |= y r8   )ra   r   r<   cache_delitems      r   rC   zFIFOCache.__delitem__       dC LLr   c                     	 t        t        | j                              }|| j                  |      fS # t        $ r# t        dt        |       j                  z        dw xY w)z9Remove and return the `(key, value)` pair first inserted.%s is emptyN)nextrH   ra   r   StopIterationr9   typer    r;   s     r   r?   zFIFOCache.popitem   ^    	(tDLL)*C #''  	J=4:+>+>>?TI	J	   3 ,Ar8   )	r    r!   r"   rY   r/   r   r   rC   r?   r   r   r   r   r      s/    91 5:4E4E % .3->-> (r   r   c                   x    e Zd ZdZddZej                  fdZej                  fdZej                  fdZd Z	y)	r   z1Least Frequently Used (LFU) cache implementation.Nc                 d    t         j                  | ||       t        j                         | _        y r8   )r   r/   r_   Counter_LFUCache__counterr-   s      r   r/   zLFUCache.__init__   s"    tWi0$,,.r   c                 N     || |      }|| v r| j                   |xx   dz  cc<   |S r   ru   r   r<   cache_getitemr   s       r   r   zLFUCache.__getitem__   s.    dC($;NN31$r   c                 F     || ||       | j                   |xx   dz  cc<   y r   rw   re   s       r   r   zLFUCache.__setitem__   s"    dC'sq r   c                 0     || |       | j                   |= y r8   rw   rh   s      r   rC   zLFUCache.__delitem__   s    dC NN3r   c                     	 | j                   j                  d      \  \  }}|| j                  |      fS # t        $ r# t	        dt        |       j                  z        dw xY w)z@Remove and return the `(key, value)` pair least frequently used.r   rl   N)ru   most_commonr   r>   r9   ro   r    )r   r<   r   s      r   r?   zLFUCache.popitem   se    	(..44Q7KXc1 #''  	J=4:+>+>>?TI	Js	    5 ,A!r8   )
r    r!   r"   rY   r/   r   r   r   rC   r?   r   r   r   r   r      s?    ;/ .3->->  5:4E4E ! .3->->  (r   r   c                   ~    e Zd ZdZd	dZej                  fdZej                  fdZej                  fdZd Z	d Z
y)
r   z/Least Recently Used (LRU) cache implementation.Nc                 d    t         j                  | ||       t        j                         | _        y r8   )r   r/   r_   r`   _LRUCache__orderr-   s      r   r/   zLRUCache.__init__   rb   r   c                 B     || |      }|| v r| j                  |       |S r8   _LRUCache__updaterx   s       r   r   zLRUCache.__getitem__   &    dC($;MM#r   c                 :     || ||       | j                  |       y r8   r   re   s       r   r   zLRUCache.__setitem__       dC'cr   c                 0     || |       | j                   |= y r8   )r   rh   s      r   rC   zLRUCache.__delitem__   rj   r   c                     	 t        t        | j                              }|| j                  |      fS # t        $ r# t        dt        |       j                  z        dw xY w)z>Remove and return the `(key, value)` pair least recently used.rl   N)rm   rH   r   r   rn   r9   ro   r    r;   s     r   r?   zLRUCache.popitem   rp   rq   c                 x    	 | j                   j                  |       y # t        $ r d | j                   |<   Y y w xY wr8   )r   rd   r9   r;   s     r   __updatezLRUCache.__update   s6    	%LL$$S) 	% $DLL	%s    99r8   )r    r!   r"   rY   r/   r   r   r   rC   r?   r   r   r   r   r   r      sD    91 .3->->  5:4E4E  .3->-> (%r   r   c                   ~    e Zd ZdZd	dZej                  fdZej                  fdZej                  fdZd Z	d Z
y)
r   z.Most Recently Used (MRU) cache implementation.Nc                     ddl m}  |dt        d       t        j	                  | ||       t        j                         | _        y )Nr   )warnzMRUCache is deprecated   )
stacklevel)warningsr   DeprecationWarningr   r/   r_   r`   _MRUCache__order)r   r.   r'   r   s       r   r/   zMRUCache.__init__   s3    !%'9aHtWi0"..0r   c                 B     || |      }|| v r| j                  |       |S r8   _MRUCache__updaterx   s       r   r   zMRUCache.__getitem__   r   r   c                 :     || ||       | j                  |       y r8   r   re   s       r   r   zMRUCache.__setitem__   r   r   c                 0     || |       | j                   |= y r8   )r   rh   s      r   rC   zMRUCache.__delitem__  rj   r   c                     	 t        t        | j                              }|| j                  |      fS # t        $ r# t        dt        |       j                  z        dw xY w)z=Remove and return the `(key, value)` pair most recently used.rl   N)rm   rH   r   r   rn   r9   ro   r    r;   s     r   r?   zMRUCache.popitem  rp   rq   c                 |    	 | j                   j                  |d       y # t        $ r d | j                   |<   Y y w xY w)NF)last)r   rd   r9   r;   s     r   r   zMRUCache.__update  s;    	%LL$$Su$5 	% $DLL	%s     ;;r8   )r    r!   r"   rY   r/   r   r   r   rC   r?   r   r   r   r   r   r      sD    81 .3->->  5:4E4E  .3->-> (%r   r   c                   F    e Zd ZdZej
                  dfdZed        Zd Zy)r   z-Random Replacement (RR) cache implementation.Nc                 @    t         j                  | ||       || _        y r8   )r   r/   _RRCache__choice)r   r.   choicer'   s       r   r/   zRRCache.__init__  s    tWi0r   c                     | j                   S )z(The `choice` function used by the cache.)r   r5   s    r   r   zRRCache.choice  s     }}r   c                     	 | j                  t        |             }|| j                  |      fS # t        $ r# t	        dt        |       j                  z        dw xY w)z/Remove and return a random `(key, value)` pair.rl   N)r   listr   
IndexErrorr9   ro   r    r;   s     r   r?   zRRCache.popitem$  s]    	(--T
+C #''  	J=4:+>+>>?TI	Js	   / ,A)	r    r!   r"   rY   randomr   r/   r[   r?   r   r   r   r   r     s,    7'-}}   (r   r   c                        e Zd ZdZ G d d      Zej                  dfdZej                  fdZ	ej                  fdZ
e fd       Zed	        Zd
 Zd Zd Zd Z xZS )_TimedCachez0Base class for time aware cache implementations.c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)_TimedCache._Timerc                      || _         d| _        y r&   )_Timer__timer_Timer__nesting)r   timers     r   r/   z_TimedCache._Timer.__init__2  s     DLDNr   c                 X    | j                   dk(  r| j                         S | j                  S r&   r   r   _Timer__timer5   s    r   __call__z_TimedCache._Timer.__call__6  s$    ~~"||~%{{"r   c                     | j                   dk(  r| j                         x| _        }n| j                  }| xj                   dz  c_         |S )Nr   r   r   r   times     r   	__enter__z_TimedCache._Timer.__enter__<  s<    ~~"%)\\^3d{{NNaNKr   c                 .    | xj                   dz  c_         y r   )r   )r   excs     r   __exit__z_TimedCache._Timer.__exit__D  s    NNaNr   c                 <    t         j                  | j                  ffS r8   )r   _Timerr   r5   s    r   
__reduce__z_TimedCache._Timer.__reduce__G  s    %%66r   c                 .    t        | j                  |      S r8   )getattrr   )r   names     r   __getattr__z_TimedCache._Timer.__getattr__J  s    4<<..r   N)	r    r!   r"   r/   r   r   r   r   r   r   r   r   r   r   1  s     		#		 	7	/r   r   Nc                 f    t         j                  | ||       t        j                  |      | _        y r8   )r   r/   r   r   _TimedCache__timer)r   r.   r   r'   s       r   r/   z_TimedCache.__init__M  s$    tWi0"))%0r   c                 x    | j                   5 }| j                  |        ||       cd d d        S # 1 sw Y   y xY wr8   r   expire)r   
cache_reprr   s      r   r6   z_TimedCache.__repr__Q  s)    \\TKKd# \\   09c                 x    | j                   5 }| j                  |        ||       cd d d        S # 1 sw Y   y xY wr8   r   )r   	cache_lenr   s      r   rL   z_TimedCache.__len__V  s(    \\TKKT? \\r   c                 ~    | j                   5 }| j                  |       t        |   cd d d        S # 1 sw Y   y xY wr8   )r   r   superrW   )r   r   r3   s     r   rW   z_TimedCache.currsize[  s*    \\TKK7# \\s   3<c                     | j                   S )z%The timer function used by the cache.)r   r5   s    r   r   z_TimedCache.timera  s     ||r   c                     | j                   5 }| j                  |       t        j                  |        d d d        y # 1 sw Y   y xY wr8   )r   r   r   clearr   s     r   r   z_TimedCache.clearf  s-    \\TKKKK \\s	   '=Ac                 x    | j                   5  t        j                  | g|i |cd d d        S # 1 sw Y   y xY wr8   )r   r   rO   r   argskwargss      r   rO   z_TimedCache.getk  )    \\99T3D3F3 \\r   c                 x    | j                   5  t        j                  | g|i |cd d d        S # 1 sw Y   y xY wr8   )r   r   r   r   s      r   r   z_TimedCache.popo  r   r   c                 x    | j                   5  t        j                  | g|i |cd d d        S # 1 sw Y   y xY wr8   )r   r   rT   r   s      r   rT   z_TimedCache.setdefaults  s+    \\##D:4:6: \\r   )r    r!   r"   rY   r   r   	monotonicr/   r   r6   rL   r[   rW   r   r   rO   r   rT   __classcell__)r3   s   @r   r   r   .  sx    :/ /8 '+nn 1 #(.. $
 !& #
 $ $
  
44;r   r   c                       e Zd ZdZ G d d      Zej                  dfdZd Ze	j                  fdZ
e	j                  fdZe	j                  fd	Zd
 Zd Zed        ZddZd Zd Zy)r	   z@LRU Cache implementation with per-item time-to-live (TTL) value.c                   $    e Zd ZdZddZd Zd Zy)TTLCache._Link)r<   expiresrm   prevNc                      || _         || _        y r8   )r<   r   r   r<   r   s      r   r/   zTTLCache._Link.__init__~  s    DH"DLr   c                 R    t         j                  | j                  | j                  ffS r8   )r	   _Linkr<   r   r5   s    r   r   zTTLCache._Link.__reduce__  s    >>DHHdll#;;;r   c                 P    | j                   }| j                  }||_         ||_        y r8   )rm   r   )r   rm   r   s      r   unlinkzTTLCache._Link.unlink  s"    99D99DDIDIr   NN)r    r!   r"   r#   r/   r   r   r   r   r   r   r   {  s    6		#	<	r   r   Nc                     t         j                  | |||       t        j                         x| _        }|x|_        |_        t        j                         | _	        || _
        y r8   )r   r/   r	   r   _TTLCache__rootr   rm   r_   r`   _TTLCache__links_TTLCache__ttl)r   r.   ttlr   r'   roots         r   r/   zTTLCache.__init__  sN    T7E9=%^^--d $$	DI"..0
r   c                 z    	 | j                   |   }| j                         |j                  k  S # t        $ r Y yw xY wNF)r   r   r   r9   )r   r<   links      r   rE   zTTLCache.__contains__  @    	/<<$D ::<$,,..  		   . 	::c                     	 | j                  |      }| j                         |j                  k   }|r| j	                  |      S  || |      S # t        $ r d}Y )w xY wr   )_TTLCache__getlinkr   r   r9   r:   )r   r<   ry   r   expireds        r   r   zTTLCache.__getitem__  e    	8>>#&D  ::<$,,67G##C(( s++  	G	   A AAc                    | j                   5 }| j                  |        || ||       d d d        	 | j                  |      }|j                          | j                  z   |_	        | j                  x|_        }|j                  x|_        }|x|_        |_        y # 1 sw Y   txY w# t        $ r' t
        j                  |      x| j                  |<   }Y w xY wr8   )r   r   r   r   r9   r	   r   r   r   r   r   rm   r   )r   r<   r   rf   r   r   r   r   s           r   r   zTTLCache.__setitem__  s    ZZ4KK$U+ 	>>#&D KKMdjj(;;&	D99$	D $$	DI Z
  	;'/~~c'::DLL	;s   BB( B%(-CCc                      || |       | j                   j                  |      }|j                          | j                         |j                  k  st        |      y r8   )r   r   r   r   r   r9   )r   r<   ri   r   s       r   rC   zTTLCache.__delitem__  sJ    dC ||$

t||+3- ,r   c              #      K   | j                   }|j                  }||urD| j                  5 }||j                  k  r|j                   d d d        |j                  }||urCy y # 1 sw Y   xY wwr8   )r   rm   r   r   r<   )r   r   currr   s       r   rI   zTTLCache.__iter__  s]     {{yy$t$,,&((N  99D $s"   )A0A$	A0"A0$A-)A0c                 R   | j                   j                  |       | j                  }|x|_        |_        t        | j                  j                         d       D ]*  }||_        |j                  x|_        }|x|_        |_        , | j                  | j                                y )Nc                     | j                   S r8   r   )objs    r   <lambda>z'TTLCache.__setstate__.<locals>.<lambda>  s    #++r   )r<   )
__dict__updater   r   rm   sortedr   valuesr   r   )r   stater   r   r   s        r   __setstate__zTTLCache.__setstate__  s    U#{{ $$	DI4<<..06MNDDI#yy(DI$((DI	 O 	DJJL!r   c                     | j                   S )z,The time-to-live value of the cache's items.)r   r5   s    r   r   zTTLCache.ttl       zzr   c                    || j                         }| j                  }|j                  }| j                  }g }t        j
                  }t        j                  }||ur||j                  k  s|j                  |j                   || |j                        f        || |j                         ||j                  = |j                  }|j                          |}||ur||j                  k  s|S )qRemove expired items from the cache and return an iterable of the
        expired `(key, value)` pairs.

        )r   r   rm   r   r   rC   r   r   appendr<   r   )	r   r   r   r   linksr   ri   ry   rm   s	            r   r   zTTLCache.expire  s    
 <::<D{{yy))))$t||(;NNDHHmD$((&CDE$)dhh99DKKMD $t||(; r   c                 *   | j                   5 }| j                  |       	 t        t        | j                              }|| j                  |      fcddd       S # t        $ r# t        dt        |       j                  z        dw xY w# 1 sw Y   yxY wzmRemove and return the `(key, value)` pair least recently used that
        has not already expired.

        Nrl   )
r   r   rm   rH   r   r   rn   r9   ro   r    r   r   r<   s      r   r?   zTTLCache.popitem  s    
 ZZ4KK,4-. TXXc]+ Z ! N}tDz/B/BBCMN	 Zs!   B	AB	,BB		Bc                 Z    | j                   |   }| j                   j                  |       |S r8   )r   rd   r   r<   r   s      r   	__getlinkzTTLCache.__getlink  '    S!  %r   r8   )r    r!   r"   rY   r   r   r   r/   rE   r   r   r   rC   rI   r   r[   r   r   r?   r   r   r   r   r	   r	   x  s    J   ,0>>T / .3->-> 
, 5:4E4E % .3->->  "  ,,r   r	   c                       e Zd ZdZej
                   G d d             Zej                  dfdZ	d Z
ej                  fdZej                  fdZej                  fd	Zd
 Zed        ZddZd Zd Zy)r   z;Time aware Least Recently Used (TLRU) cache implementation.c                       e Zd ZdZddZd Zy)TLRUCache._Itemr<   r   removedNc                 .    || _         || _        d| _        y r   r  r   s      r   r/   zTLRUCache._Item.__init__  s    DH"DL DLr   c                 4    | j                   |j                   k  S r8   r   )r   others     r   __lt__zTLRUCache._Item.__lt__  s    <<%--//r   r   )r    r!   r"   r#   r/   r  r   r   r   _Itemr    s    1		!
	0r   r  Nc                     t         j                  | |||       t        j                         | _        g | _        || _        y r8   )r   r/   r_   r`   _TLRUCache__items_TLRUCache__order_TLRUCache__ttu)r   r.   ttur   r'   s        r   r/   zTLRUCache.__init__  s4    T7E9="..0
r   c                 z    	 | j                   |   }| j                         |j                  k  S # t        $ r Y yw xY wr   )r  r   r   r9   )r   r<   items      r   rE   zTLRUCache.__contains__  r   r   c                     	 | j                  |      }| j                         |j                  k   }|r| j	                  |      S  || |      S # t        $ r d}Y )w xY wr   )_TLRUCache__getitemr   r   r9   r:   )r   r<   ry   r  r   s        r   r   zTLRUCache.__getitem__  r   r   c                    | j                   5 }| j                  |||      }||k  s
	 d d d        y | j                  |        || ||       d d d        	 d| j                  |      _        t        j                  |      x| j                  |<   }t        j                  | j                  |       y # 1 sw Y   fxY w# t
        $ r Y ]w xY wNT)r   r  r   r  r  r9   r   r  r  heapqheappushr  )r   r<   r   rf   r   r   r  s          r   r   zTLRUCache.__setitem__(  s    ZZ4jjeT2G7N Z KK$U+ 	*.DNN3' $-??3#@@SDt||T* Z  		s"   B0B0B< 0B9<	CCc                     | j                   5 } || |       d d d        | j                  j                  |      }d|_        |j                  k  st        |      y # 1 sw Y   FxY wr!  )r   r  r   r  r   r9   )r   r<   ri   r   r  s        r   rC   zTLRUCache.__delitem__8  sZ    ZZ4$$  ||$t||#3- $ Zs   
AA%c              #      K   | j                   D ]@  }| j                  5 }||j                  k  r|j                  s|j                   d d d        B y # 1 sw Y   MxY wwr8   )r  r   r   r  r<   )r   r   r   s      r   rI   zTLRUCache.__iter__A  sD     LLDt$,,&t||((N  !s   A*AAA	Ac                     | j                   S )z1The local time-to-use function used by the cache.)r  r5   s    r   r  zTLRUCache.ttuH  r  r   c                    || j                         }| j                  }| j                  }t        |      t        |      dz  kD  r9|D cg c]  }|j                  r| c}x| _        }t        j                  |       g }t        j                  }t        j                  }|r|d   j                  s||d   j                  k  st        j                  |      }|j                  sN|j                  |j                   || |j                        f        || |j                         ||j                  = |r#|d   j                  r||d   j                  k  s|S c c}w )r  r   r   )r   r  r  rK   r  r"  heapifyr   rC   r   r   heappopr  r<   )r   r   itemsorderr  r   ri   ry   s           r   r   zTLRUCache.expireM  s   
 <::<Du:E
Q&5:#OUT$,,DU#OODL5MM% ))))q))$q9I9I2I=='D<<-dhh*GHIdDHH-$((O q))$q9I9I2I  $Ps   	EEc                 ,   | j                   5 }| j                  |       	 t        t        | j                              }|| j                  |      fcddd       S # t        $ r$ t        d| j                  j                  z        dw xY w# 1 sw Y   yxY wr  )
r   r   rm   rH   r  r   rn   r9   r3   r    r  s      r   r?   zTLRUCache.popiteme  s    
 ZZ4KK,4-. TXXc]+ Z ! R}t~~/F/FFGTQR	 Zs!   B
AB
-BB

Bc                 Z    | j                   |   }| j                   j                  |       |S r8   )r  rd   r
  s      r   	__getitemzTLRUCache.__getitems  r  r   r8   )r    r!   r"   rY   	functoolstotal_orderingr  r   r   r/   rE   r   r   r   rC   rI   r[   r  r   r?   r  r   r   r   r   r     s    E	0 	0 	0 ,0>>T / .3->-> 
, 5:4E4E +  .3->->  #  0,r   r   	CacheInfo)hitsmissesr.   rW   Fc                       fd}|S )z_Decorator to wrap a function with a memoizing callable that saves
    results in a cache.

    c                     rdxt        t              rfdn2t        t        j                  j                        rfdnfd fd}fd}}nX
 	fd}fd}}nC 	
fd	}
fd
}
fd}n*	 fd}d }n
 	fd}fd}n 	
fd}
fd}d }|_        	|_        
|_        ||_        ||_	        t        j                  |       S )Nr   c                  H    t         j                   j                        S r8   )
_CacheInfor.   rW   cacher2  r3  s   r   getinfoz*cached.<locals>.decorator.<locals>.getinfo  s    %dFEMM5>>RRr   c                  2    t        d t                     S r8   )r7  rK   r8  s   r   r:  z*cached.<locals>.decorator.<locals>.getinfo  s    %dFD#e*EEr   c                       t         dd      S r&   )r7  r2  r3  s   r   r:  z*cached.<locals>.decorator.<locals>.getinfo  s    %dFAq99r   c                      dz   | i |S r   r   )r   r   funcr3  s     r   wrapperz*cached.<locals>.decorator.<locals>.wrapper  s    aKF000r   c                      dx y r&   r   r=  s   r   cache_clearz.cached.<locals>.decorator.<locals>.cache_clear  s    $%%D6r   c                       | i |}	 |   }dz  |S # t         $ r 	dz  	Y nw xY w | i |}	 ||<   |S # t        $ r Y |S w xY wr   r9   r>   )
r   r   kresultvr9  r?  r2  r<   r3  s
        r   r@  z*cached.<locals>.decorator.<locals>.wrapper  s    T,V,A$!&q	%# $!$d-f-A#$a H & Hs    ((; 	AAc                  .     j                          dxy r&   r   r8  s   r   rB  z.cached.<locals>.decorator.<locals>.cache_clear  s    KKM$%%D6r   c                  @    | i |}	 	5  |   }dz  |cd d d        S # 1 sw Y   nxY wn,# t         $ r  	5  
dz  
d d d        n# 1 sw Y   nxY wY nw xY w | i |}	 	5  j                  ||      cd d d        S # 1 sw Y   y xY w# t        $ r |cY S w xY wr   r9   rT   r>   )r   r   rE  rF  rG  r9  r?  r2  r<   lockr3  s        r   r@  z*cached.<locals>.decorator.<locals>.wrapper  s    T,V,A(!%*1XF AID#) "TTT $ (!"aKF "TT( d-f-A!!#(#3#3Aq#9 "TT% ! !sf   0 #	0 ,0 AA
	A
A	AA%B 'B9	B BB B BBc                  \    5   j                          dxd d d        y # 1 sw Y   y xY wr&   rI  )r9  r2  rL  r3  s   r   rB  z.cached.<locals>.decorator.<locals>.cache_clear  s#    ())v s   "+c                  B    5           cd d d        S # 1 sw Y   y xY wr8   r   )r:  rL  s   r   
cache_infoz-cached.<locals>.decorator.<locals>.cache_info  s    &y s   c                       | i |S r8   r   )r   r   r?  s     r   r@  z*cached.<locals>.decorator.<locals>.wrapper  s    000r   c                       y r8   r   r   r   r   rB  z.cached.<locals>.decorator.<locals>.cache_clear  s    r   c                  ~     | i |}	 |   S # t         $ r Y nw xY w | i |}	 ||<   |S # t        $ r Y |S w xY wr8   rD  )r   r   rE  rG  r9  r?  r<   s       r   r@  z*cached.<locals>.decorator.<locals>.wrapper  so    T,V,A$Qx# d-f-A#$a H & Hs    	/ 	<<c                  &     j                          y r8   rI  )r9  s   r   rB  z.cached.<locals>.decorator.<locals>.cache_clear  s    KKMr   c                       | i |}	 5  |   cd d d        S # 1 sw Y   nxY wn# t         $ r Y nw xY w | i |}	 5  j                  ||      cd d d        S # 1 sw Y   y xY w# t        $ r |cY S w xY wr8   rK  )r   r   rE  rG  r9  r?  r<   rL  s       r   r@  z*cached.<locals>.decorator.<locals>.wrapper  s    T,V,A!#(8 "TTT# d-f-A!!#(#3#3Aq#9 "TT% ! !sL   ) 	) %) 	55A+ A	A+ A($A+ (A+ +A98A9c                  T    5   j                          d d d        y # 1 sw Y   y xY wr8   rI  )r9  rL  s   r   rB  z.cached.<locals>.decorator.<locals>.cache_clear  s     s   ')
isinstancer   r_   abcMappingr9  	cache_key
cache_lockrB  rO  r/  update_wrapper)r?  r@  rB  rO  r:  r2  r3  r9  infor<   rL  s   `   @@@r   	decoratorzcached.<locals>.decorator  s    D6%'S E;??#:#:;F: }1
& %
  &
 %
! !&*)
 }1 "
!& J!)'''66r   r   )r9  r<   rL  r\  r]  s   ```` r   r
   r
   ~  s    S7j r   c                       fd}|S )zoDecorator to wrap a class or instance method with a memoizing
    callable that saves results in a cache.

    c                       fd}fd}n fd}fd}|_         |_        |_        ||_        t	        j
                  |       S )Nc                      |       }| | g|i |S  | g|i |}	 ||   S # t         $ r Y nw xY w | g|i |}	 |||<   |S # t        $ r Y |S w xY wr8   rD  )	r   r   r   crE  rG  r9  r<   methods	         r   r@  z0cachedmethod.<locals>.decorator.<locals>.wrapper%  s    $K9!$8888.t.v.Q4K 41$1&1AaD  " s   * 	66A 	AAc                 <     |       }||j                          y y r8   rI  )r   ra  r9  s     r   r   z.cachedmethod.<locals>.decorator.<locals>.clear5  s    $K=GGI !r   c                 L    |       }| 	| g|i |S  | g|i |}	  |       5  ||   cd d d        S # 1 sw Y   nxY wn# t         $ r Y nw xY w 	| g|i |}	  |       5  |j                  ||      cd d d        S # 1 sw Y   y xY w# t        $ r |cY S w xY wr8   rK  )
r   r   r   ra  rE  rG  r9  r<   rL  rb  s
         r   r@  z0cachedmethod.<locals>.decorator.<locals>.wrapper<  s    $K9!$8888.t.v.d t $ 41$1&1d ||Aq1 $! HsU   A	 <	A	 AA	 		AA%B -B	?	B 	BB B B#"B#c                 v     |       }|" |       5  |j                          d d d        y y # 1 sw Y   y xY wr8   rI  )r   ra  r9  rL  s     r   r   z.cachedmethod.<locals>.decorator.<locals>.clearN  s3    $K=d	 $ !#s   /8)r9  rY  rZ  rB  r/  r[  )rb  r@  r   r9  r<   rL  s   `  r   r]  zcachedmethod.<locals>.decorator"  sP    < $" !#''88r   r   )r9  r<   rL  r]  s   ``` r   r   r     s    79r r   )rY   __all____version__r_   collections.abcr/  r"  r   r    r   r   rW  MutableMappingr   r   r   r   r   r   r   r	   r   
namedtupler7  hashkeyr
   	methodkeyr   r   r   r   <module>rn     s    6        
 
cKOO** cL( (8(u (>"%u "%J&%u &%R(e (,G;% G;TD{ DNw wt $[##:

 llE [| !NN ?r   