o
    .d                     @   sF   d Z ddlZddlmZ ddlmZ ddlmZ G dd dejZdS )aB  A simple LRU Cache implementation.

Copied from https://github.com/pallets/jinja/blob/master/src/jinja2/utils.py
BSD-3-Clause License

Copyright 2007 Pallets

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
    N)abc)deque)Lockc                   @   s   e Zd ZdZdefddZdd Zdd Zd	d
 Zdd Z	dd Z
d.dedefddZd.ddZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Ze
ZdS )/LRUCachez"A simple LRU Cache implementation.capacityc                 C   s    || _ i | _t | _|   d S N)r   _mappingr   _queue	_postinit)selfr    r   J/var/www/Matress/matenv/lib/python3.10/site-packages/liquid/utils/cache.py__init__/   s   zLRUCache.__init__c                 C   s4   | j j| _| j j| _| j j| _t | _| j j	| _
d S r   )r	   popleft_popleftpop_popremove_remover   _wlockappend_appendr   r   r   r   r
   5   s
   


zLRUCache._postinitc                 C   s   | j | j| jdS )Nr   r   r	   r   r   r   r   r   __getstate__=   s   zLRUCache.__getstate__c                 C   s   | j | |   d S r   )__dict__updater
   )r   dr   r   r   __setstate__D   s   zLRUCache.__setstate__c                 C   s   | j fS r   )r   r   r   r   r   __getnewargs__H   s   zLRUCache.__getnewargs__c                 C   s,   |  | j}|j| j |j| j |S )z&Return a shallow copy of the instance.)	__class__r   r   r   r	   extend)r   rvr   r   r   copyK   s   zLRUCache.copyNkeydefaultc                 C   s"   z| | W S  t y   | Y S w )z0Return an item from the cache dict or `default`.KeyErrorr   r$   r%   r   r   r   getR   s
   
zLRUCache.getc                 C   s*   z| | W S  t y   || |< | Y S w )zvSet `default` if the key is not in the cache otherwise
        leave unchanged. Return the value of this key.
        r&   r(   r   r   r   
setdefaultY   s   
zLRUCache.setdefaultc                 C   s<   | j   z| j  | j  W | j   dS | j   w )zClear the cache.N)r   acquirer   clearr	   releaser   r   r   r   r,   c   s
   

zLRUCache.clearc                 C   s
   || j v S )z$Check if a key exists in this cache.r   r   r$   r   r   r   __contains__l      
zLRUCache.__contains__c                 C   s
   t | jS )z%Return the current size of the cache.)lenr   r   r   r   r   __len__p   r1   zLRUCache.__len__c                 C   s   d| j j d| jdS )N< >)r    __name__r   r   r   r   r   __repr__t   s   zLRUCache.__repr__c                 C   s~   | j   z3| j| }| jd |kr1tt | | W d   n1 s'w   Y  | | |W | j 	  S | j 	  w )zGet an item from the cache.

        Moves the item up so that it has the highest priority then.
        Raise a `KeyError` if it does not exist.
        N)
r   r+   r   r	   
contextlibsuppress
ValueErrorr   r   r-   )r   r$   r"   r   r   r   __getitem__w   s   


zLRUCache.__getitem__c                 C   sn   | j   z+|| jv r| | nt| j| jkr| j|  = | | || j|< W | j   dS | j   w )ziSets the value for an item.

        Moves the item up so that it has the highest priority then.
        N)	r   r+   r   r   r2   r   r   r   r-   )r   r$   valuer   r   r   __setitem__   s   


zLRUCache.__setitem__c                 C   st   | j   z.| j|= tt | | W d   n1 sw   Y  W | j   dS W | j   dS | j   w )z^Remove an item from the cache dict.

        Raise a `KeyError` if it does not exist.
        N)r   r+   r   r:   r;   r<   r   r-   r/   r   r   r   __delitem__   s   
zLRUCache.__delitem__c                    s$    fddt  jD }|  |S )zReturn a list of items.c                    s   g | ]	}| j | fqS r   r.   ).0r$   r   r   r   
<listcomp>   s    z"LRUCache.items.<locals>.<listcomp>)listr	   reverse)r   resultr   r   r   items   s   zLRUCache.itemsc                 C   s   dd |   D S )zReturn a list of all values.c                 S   s   g | ]}|d  qS )   r   )rA   xr   r   r   rB      s    z#LRUCache.values.<locals>.<listcomp>)rF   r   r   r   r   values   s   zLRUCache.valuesc                 C   s   t | S )z7Return a list of all keys ordered by most recent usage.)rC   r   r   r   r   keys   s   zLRUCache.keysc                 C      t t| jS r   )reversedtupler	   r   r   r   r   __iter__   s   zLRUCache.__iter__c                 C   rK   )zCIterate over the keys in the cache dict, oldest items coming first.)iterrM   r	   r   r   r   r   __reversed__   s   zLRUCache.__reversed__r   )r7   
__module____qualname____doc__intr   r
   r   r   r   r#   strobjectr)   r*   r,   r0   r3   r8   r=   r?   r@   rF   rI   rJ   rN   rP   __copy__r   r   r   r   r   (   s.    

	r   )	rS   r:   collectionsr   r   	threadingr   MutableMappingr   r   r   r   r   <module>   s    !