a
    /§»a  ã                   @   sd   d Z ddlmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZdS )zÄ
This module contains multithread-safe cache implementations.

All Caches have

    getorbuild(key, builder)
    delentry(key)

methods and allow configuration when instantiating the cache class.
é    )Útimec                   @   sH   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zddd„Zdd„ Zdd„ Z	dS )Ú
BasicCacheé€   c                 C   s"   || _ t||d  ƒ| _i | _d S )Né   )Ú
maxentriesÚintÚprunenumÚ_dict)Úselfr   © r   úL/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/py/_path/cacheutil.pyÚ__init__   s    zBasicCache.__init__c                 C   s   | j  ¡  d S ©N)r	   Úclear©r
   r   r   r   r      s    zBasicCache.clearc                 C   s
   | j | S r   )r	   )r
   Úkeyr   r   r   Ú	_getentry   s    zBasicCache._getentryc                 C   s   |   ¡  || j|< d S r   )Ú_prunelowestweightr	   ©r
   r   Úentryr   r   r   Ú	_putentry   s    zBasicCache._putentryFc                 C   s*   z| j |= W n ty$   |r ‚ Y n0 d S r   )r	   ÚKeyError)r
   r   Úraisingr   r   r   Údelentry   s
    zBasicCache.delentryc                 C   s@   z|   |¡}W n* ty8   |  ||¡}|  ||¡ Y n0 |jS r   )r   r   Ú_buildr   Úvalue)r
   r   Úbuilderr   r   r   r   Ú
getorbuild$   s    zBasicCache.getorbuildc                 C   sj   t | jƒ}|| jkrfdd„ | j ¡ D ƒ}| ¡  || j }|dkrf|d|… D ]\}}| j|dd� qNdS )z' prune out entries with lowest weight. c                 S   s   g | ]\}}|j |f‘qS r   )Úweight)Ú.0r   r   r   r   r   Ú
<listcomp>1   s   ÿz1BasicCache._prunelowestweight.<locals>.<listcomp>r   NF)r   )Úlenr	   r   ÚitemsÚsortr   r   )r
   Z
numentriesr"   Úindexr   r   r   r   r   r   ,   s    

ÿ
zBasicCache._prunelowestweightN)r   )F)
Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   r   r   r      s   

r   c                   @   s   e Zd ZdZdd„ ZdS )ÚBuildcostAccessCachea‡   A BuildTime/Access-counting cache implementation.
        the weight of a value is computed as the product of

            num-accesses-of-a-value * time-to-build-the-value

        The values with the least such weights are evicted
        if the cache maxentries threshold is superceded.
        For implementation flexibility more than one object
        might be evicted at a time.
    c                 C   s    t ƒ }|ƒ }t ƒ }t||| ƒS r   )ÚgettimeÚWeightedCountingEntry)r
   r   r   ÚstartÚvalÚendr   r   r   r   G   s    zBuildcostAccessCache._buildN)r%   r&   r'   Ú__doc__r   r   r   r   r   r(   :   s   r(   c                   @   s$   e Zd Zdd„ Zdd„ ZeeƒZdS )r*   c                 C   s   || _ | | _| _d S r   )Ú_valuer   Ú
_oneweight)r
   r   Z	oneweightr   r   r   r   O   s    zWeightedCountingEntry.__init__c                 C   s   |  j | j7  _ | jS r   )r   r0   r/   r   r   r   r   r   S   s    zWeightedCountingEntry.valueN)r%   r&   r'   r   r   Úpropertyr   r   r   r   r*   N   s   r*   c                       s2   e Zd ZdZd
‡ fdd„	Zdd„ Zdd	„ Z‡  ZS )Ú
AgingCachez; This cache prunes out cache entries that are too old.
    r   ç      $@c                    s   t t| ƒ |¡ || _d S r   )Úsuperr2   r   Ú
maxseconds)r
   r   r5   ©Ú	__class__r   r   r   [   s    zAgingCache.__init__c                 C   s(   | j | }| ¡ r$|  |¡ t|ƒ‚|S r   )r	   Ú	isexpiredr   r   r   r   r   r   r   _   s
    

zAgingCache._getentryc                 C   s   |ƒ }t |tƒ | j ƒ}|S r   )Ú
AgingEntryr)   r5   )r
   r   r   r,   r   r   r   r   r   f   s    zAgingCache._build)r   r3   )r%   r&   r'   r.   r   r   r   Ú__classcell__r   r   r6   r   r2   X   s   r2   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r9   c                 C   s   || _ || _d S r   )r   r   )r
   r   Zexpirationtimer   r   r   r   l   s    zAgingEntry.__init__c                 C   s   t ƒ }|| jkS r   )r)   r   )r
   Útr   r   r   r8   p   s    zAgingEntry.isexpiredN)r%   r&   r'   r   r8   r   r   r   r   r9   k   s   r9   N)	r.   r   r)   Úobjectr   r(   r*   r2   r9   r   r   r   r   Ú<module>   s   
-
