HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/cherrypy/lib/__pycache__/caching.cpython-310.pyc
o

�N�gmD�@s�dZddlZddlZddlZddlZddlZddlmZmZGdd�de	�Z
Gdd�de�ZGdd	�d	e
�Z
ddd
�Zdd�Zddd�ZdS)a
CherryPy implements a simple caching system as a pluggable Tool. This tool
tries to be an (in-process) HTTP/1.1-compliant cache. It's not quite there
yet, but it's probably good enough for most sites.

In general, GET responses are cached (along with selecting headers) and, if
another request arrives for the same resource, the caching Tool will return 304
Not Modified if possible, or serve the cached response otherwise. It also sets
request.cached to True if serving a cached representation, and sets
request.cacheable to False (so it doesn't get cached again).

If POST, PUT, or DELETE requests are made for a cached resource, they
invalidate (delete) any cached response.

Usage
=====

Configuration file example::

    [/]
    tools.caching.on = True
    tools.caching.delay = 3600

You may use a class other than the default
:class:`MemoryCache<cherrypy.lib.caching.MemoryCache>` by supplying the config
entry ``cache_class``; supply the full dotted name of the replacement class
as the config value. It must implement the basic methods ``get``, ``put``,
``delete``, and ``clear``.

You may set any attribute, including overriding methods, on the cache
instance by providing them in config. The above sets the
:attr:`delay<cherrypy.lib.caching.MemoryCache.delay>` attribute, for example.
�N)�cptools�httputilc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�Cachez%Base class for Cache implementations.cC�t�)�6Return the current variant if in the cache, else None.��NotImplementedError��self�r�H/opt/saltstack/salt/lib/python3.10/site-packages/cherrypy/lib/caching.py�get0�z	Cache.getcCr)�'Store the current variant in the cache.r)r
�obj�sizerrr�put4rz	Cache.putcCr)�3Remove ALL cached variants of the current resource.rr	rrr�delete8rzCache.deletecCr)�,Reset the cache to its initial, empty state.rr	rrr�clear<rzCache.clearN)�__name__�
__module__�__qualname__�__doc__r
rrrrrrrr,src@s"eZdZdZd	dd�Zdd�ZdS)
�AntiStampedeCachezDA storage system for cached items which reduces stampede collisions.�FcCs�|�|�}t|tj�rP|dur|rt�dd�dS|r#t�d|d�|�|�|jdur8|r5t�dd�|jS|r@t�dd�t��}d|_t�	|||�dS|durj|r\t�dd�t��}d|_t�	|||�|S)a�Return the cached value for the given key, or None.

        If timeout is not None, and the value is already
        being calculated by another thread, wait until the given timeout has
        elapsed. If the value is available before the timeout expires, it is
        returned. If not, None is returned, and a sentinel placed in the cache
        to signal other threads to wait.

        If timeout is None, no waiting is performed nor sentinels used.
        Nz
No timeout�
TOOLS.CACHINGzWaiting up to %s secondszResult!z	Timed out)
r
�
isinstance�	threading�Event�cherrypy�log�wait�result�dict�__setitem__)r
�key�timeout�debug�value�errrr#Fs<
��

zAntiStampedeCache.waitcCs:|�|�}t�|||�t|tj�r||_|��dSdS)z'Set the cached value for the given key.N)r
r%r&rrr r$�set)r
r'r*�existingrrrr&vs
�zAntiStampedeCache.__setitem__N)rF)rrrrr#r&rrrrrBs
0rc@sheZdZdZdZ	dZ	dZ	dZ	dZ	dZ		dZ
d	d
�Zdd�Zd
d�Z
dd�Zdd�Zdd�ZdS)�MemoryCachea�An in-memory cache for varying response content.

    Each key in self.store is a URI, and each value is an AntiStampedeCache.
    The response for any given URI may vary based on the values of
    "selecting request headers"; that is, those named in the Vary
    response header. We assume the list of header names to be constant
    for each URI throughout the lifetime of the application, and store
    that list in ``self.store[uri].selecting_headers``.

    The items contained in ``self.store[uri]`` have keys which are tuples of
    request header values (in the same order as the names in its
    selecting_headers), and values which are the actual responses.
    i�i��i���iXrg�������?FcCs0|��tj|jdd�}||_d|_|��dS)N�expire_cache)�target�nameT)rr�Threadr/Zexpiration_thread�daemon�start)r
�trrr�__init__�s
zMemoryCache.__init__cCs4i|_i|_d|_d|_d|_d|_d|_d|_dS)rrN)�store�expirations�tot_puts�tot_gets�tot_hist�tot_expires�tot_non_modified�cursizer	rrrr�s
zMemoryCache.clearc
Cs�trMt��}|j����D]5\}}||krB|D]&\}}}z|j|t|�=|jd7_|j|8_Wqty=Yqw|j|=q
t�	|j
�tsdSdS)z�Continuously examine cached objects, expiring stale ones.

        This function is designed to be run in its own daemon thread,
        referenced at ``self.expiration_thread``.
        �N)�timer8�copy�itemsr7�tupler<r>�KeyError�sleep�expire_freq)r
�now�expiration_timeZobjectsZobj_size�uriZsel_header_valuesrrrr/�s 	���zMemoryCache.expire_cachecs�tjj�|jd7_tj�jd�}|j�|�}|durdS�fdd�|jD�}|j	t
t|��|j|j
d�}|durA|jd7_|S)rr?��qsNc�g|]	}�j�|d��qS����headersr
��.0�h��requestrr�
<listcomp>���z#MemoryCache.get.<locals>.<listcomp>)r'r(r))r!�servingrUr:�url�query_stringr7r
�selecting_headersr#rC�sorted�antistampede_timeoutr)r;)r
rI�uricache�
header_values�variantrrTrr
�s 
��zMemoryCache.getc
s�tjj�tjj}tj�jd�}|j�|�}|dur-t�}dd�|j	�
d�D�|_||j|<t|j�|j
krx|j|}||jkrz||jkr||j|j}|j�|g�}|�|||jf��fdd�|jD�}	||tt|	��<|jd7_||_dSdSdSdS)rrJNcS�g|]}|j�qSr�r*�rRr+rrrrV�s�z#MemoryCache.put.<locals>.<listcomp>ZVarycrLrMrOrQrTrrrV�rWr?)r!rXrU�responserYrZr7r
rrP�elementsr[�len�
maxobjectsr>�maxobj_size�maxsizer@�delayr8�
setdefault�appendrCr\r9)
r
r`rrdrIr^Z
total_sizerHZbucketr_rrTrr�s0
�


�
�zMemoryCache.putcCs$tjtjjjd�}|j�|d�dS)rrJN)r!rYrXrUrZr7�pop)r
rIrrrrszMemoryCache.deleteN)rrrrrgrhrirjr]rFr)r6rr/r
rrrrrrr.�s*	r.��POST�PUTZDELETEFc	Ks�tjj}tjj}ttd�s)|�dt��t_|��D]\}}t	tj||�q|tj_
|j|vrH|r;t�d|j|fd�tj�
�d|_d|_dSddd�|j�d	�D�vr]d|_d
|_dStj��}t|�|_|j|_|j�r7tjj}dd�|j�d�D�D]@}|�d
d�}	|	�d�}
|
dkr�t|	�dks�|	d��s�t�dd��t|	d�}n|
dkr�|r�t�dd�d|_d
|_dSq|r�t�dd�|\}}}
}t|j|�}||kr�|r�t�d|d�d|_d
|_dSt��|_}|D]
}t�||t�||��q�t |�|jd<zt!�"�Wntj#�y-t$�%�d}|j&dk�r,tjj'd7_'�w||_&|
|_(|jS|�r@t�dd�|jS)aTry to obtain cached output. If fresh enough, raise HTTPError(304).

    If POST, PUT, or DELETE:
        * invalidates (deletes) any cached response for this resource
        * sets request.cached = False
        * sets request.cacheable = False

    else if a cached copy exists:
        * sets request.cached = True
        * sets request.cacheable = False
        * sets response.headers to the cached values
        * checks the cached Last-Modified response header against the
          current If-(Un)Modified-Since request headers; raises 304
          if necessary.
        * sets response.status and response.body to the cached values
        * returns True

    otherwise:
        * sets request.cached = False
        * sets request.cacheable = True
        * returns False
    �_cacheZcache_classz'request.method %r in invalid_methods %rrF�no-cachecSrarrbrcrrrrV9�zget.<locals>.<listcomp>�PragmaTcSrarrbrcrrrrVDrs�
Cache-Control�=r?rzmax-agei�zInvalid Cache-Control headerz-Ignoring cache due to Cache-Control: no-cachezReading response from cachezIgnoring cache due to age > %d�Agei0zrequest is not cached))r!rXrUrd�hasattrrmr.rqrB�setattrr)�methodr"r�cached�	cacheablerPrer
�boolrj�splitrf�isdigit�	HTTPError�intr@rZ	HeaderMapr%r&�__getitem__�strrZvalidate_sinceZHTTPRedirect�sys�exc_info�statusr=�body)Zinvalid_methodsr)�kwargsrUrd�k�vZ
cache_dataZmax_ageZatomsZ	directive�srS�bZcreate_timeZageZrh�xrrrr
s�

��




���	
���r
cs@tjj}d|j�d�vrdS�fdd�}tjj�|�j��_dS)z/Tee response output to cache storage. Internal.�no-storeruNc3s��d�j�d�vsd�j�d�vr|D]}|VqdSg}|D]
}|�|�|Vqd�|�}|s8tj��dStj��j�jpAi|�j	ft
|��dS)zTee response.body into a list.rrrtr�ruN�)rP�valuesrl�joinr!rqrrr�r@rf)r��chunk�output�rdrr�tee�s$�

	��ztee_output.<locals>.tee)r!rXrUrPr�rdr�)rUr�rr�r�
tee_outputsr�cCs�tjj}|j}d}|sdD]
}||vrd}nq
|s(|s(|r&t�dd�dSdS|r0t�dd�t|tj�r>d|j|j	}|dkrc|sHd	|vrLd
|d	<tjj
jdkr]|sYd|vr]d
|d<t�
d�}nt�
|j|�}|sqd|vrw||d<dSdS)aTool for influencing cache mechanisms using the 'Expires' header.

    secs
        Must be either an int or a datetime.timedelta, and indicates the
        number of seconds between response.time and when the response should
        expire. The 'Expires' header will be set to response.time + secs.
        If secs is zero, the 'Expires' header is set one year in the past, and
        the following "cache prevention" headers are also set:

            * Pragma: no-cache
            * Cache-Control': no-cache, must-revalidate

    force
        If False, the following headers are checked:

            * Etag
            * Last-Modified
            * Age
            * Expires

        If any are already present, none of the above response headers are set.

    F)ZEtagz
Last-Modifiedrw�ExpiresTzrequest is not cacheablez
TOOLS.EXPIRESzrequest is cacheablei�Qrrtrr)r?r?ruzno-cache, must-revalidateg�n�Ar�N)r!rXrdrPr"r�datetime�	timedelta�days�secondsrU�protocolrZHTTPDater@)Zsecs�forcer)rdrPr|Z	indicatorZexpiryrrr�expires�s8���r�)rnF)rFF)rr�r�rr@r!Zcherrypy.librr�objectrr%rr.r
r�r�rrrr�<module>s"?
t'