B
    é¹`  ã               @   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   úZ/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/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 )N)r	   )r
   Úkeyr   r   r   Ú	_getentry   s    zBasicCache._getentryc             C   s   |   ¡  || j|< d S )N)Ú_prunelowestweightr	   )r
   r   Úentryr   r   r   Ú	_putentry   s    zBasicCache._putentryFc             C   s,   y| j |= W n tk
r&   |r"‚ Y nX d S )N)r	   ÚKeyError)r
   r   Úraisingr   r   r   Údelentry   s
    zBasicCache.delentryc             C   sB   y|   |¡}W n, tk
r:   |  ||¡}|  ||¡ Y nX |jS )N)r   r   Ú_buildr   Úvalue)r
   r   Úbuilderr   r   r   r   Ú
getorbuild$   s    zBasicCache.getorbuildc             C   sn   t | jƒ}|| jkrjdd„ | j ¡ D ƒ}| ¡  || j }|dkrjx&|d|… D ]\}}| j|dd qPW d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 )N)Ú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 )N)Ú_valuer   Ú
_oneweight)r
   r   Z	oneweightr   r   r   r   O   s    zWeightedCountingEntry.__init__c             C   s   |  j | j7  _ | jS )N)r   r-   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 )N)Úsuperr/   r   Ú
maxseconds)r
   r   r2   )Ú	__class__r   r   r   [   s    zAgingCache.__init__c             C   s(   | j | }| ¡ r$|  |¡ t|ƒ‚|S )N)r	   Ú	isexpiredr   r   )r
   r   r   r   r   r   r   _   s
    

zAgingCache._getentryc             C   s   |ƒ }t |tƒ | j ƒ}|S )N)Ú
AgingEntryr&   r2   )r
   r   r   r)   r   r   r   r   r   f   s    zAgingCache._build)r   r0   )r"   r#   r$   r+   r   r   r   Ú__classcell__r   r   )r3   r   r/   X   s   r/   c               @   s   e Zd Zdd„ Zdd„ ZdS )r5   c             C   s   || _ || _d S )N)r   r   )r
   r   Zexpirationtimer   r   r   r   l   s    zAgingEntry.__init__c             C   s   t ƒ }|| jkS )N)r&   r   )r
   Útr   r   r   r4   p   s    zAgingEntry.isexpiredN)r"   r#   r$   r   r4   r   r   r   r   r5   k   s   r5   N)	r+   r   r&   Úobjectr   r%   r'   r/   r5   r   r   r   r   Ú<module>
   s   -
