o
    ]f                     @   sn   d Z ddlZddlZddlmZ ddlmZ edj	Z
G dd deeZG dd	 d	eZG d
d deZdS )z
    flask_caching.backends.memcache
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The memcache caching backend.

    :copyright: (c) 2018 by Peter Justin.
    :copyright: (c) 2010 by Thadeus Burgess.
    :license: BSD, see LICENSE for more details.
    N)MemcachedCache)	BaseCachez[^\x00-\x21\xff]{1,250}$c                   @   sB   e Zd ZdZdddZedd Zdd	 ZdddZdddZ	dS )r   a  A cache that uses memcached as backend.

    The first argument can either be an object that resembles the API of a
    :class:`memcache.Client` or a tuple/list of server addresses. In the
    event that a tuple/list is passed, Werkzeug tries to import the best
    available memcache library.

    This cache looks into the following packages/modules to find bindings for
    memcached:

        - ``pylibmc``
        - ``google.appengine.api.memcached``
        - ``memcached``
        - ``libmc``

    Implementation notes:  This cache backend works around some limitations in
    memcached to simplify the interface.  For example unicode keys are encoded
    to utf-8 on the fly.  Methods such as :meth:`~BaseCache.get_dict` return
    the keys in the same format as passed.  Furthermore all get methods
    silently ignore key errors to not cause problems when untrusted user data
    is passed to the get methods which is often the case in web applications.

    :param servers: a list or tuple of server addresses or alternatively
                    a :class:`memcache.Client` or a compatible client.
    :param default_timeout: the default timeout that is used if no timeout is
                            specified on :meth:`~BaseCache.set`. A timeout of
                            0 indicates that the cache never expires.
    :param key_prefix: a prefix that is added before all keys.  This makes it
                       possible to use the same memcached server for different
                       applications.  Keep in mind that
                       :meth:`~BaseCache.clear` will also clear keys with a
                       different prefix.
    N,  c                 C   s$   t j| |d tj| |||d d S )Ndefault_timeout)serversr   
key_prefix)r   __init__CachelibMemcachedCache)selfr   r   r    r   X/home/ubuntu/webapp/venv/lib/python3.10/site-packages/flask_caching/backends/memcache.pyr	   :   s   
zMemcachedCache.__init__c                 C   s0   | |d  |t|d d | |i |S )NCACHE_MEMCACHED_SERVERSCACHE_KEY_PREFIX)r   appendupdatedictclsappconfigargskwargsr   r   r   factoryC   s   zMemcachedCache.factoryc                 G   s6   g }|D ]}|  |}t|r|| q| j|S N)_normalize_key_test_memcached_keyr   _clientdelete_multi)r   keysnew_keyskeyr   r   r   delete_manyI   s   

zMemcachedCache.delete_many   c                 C      |  |}| j||S r   )r   r   incrr   r"   deltar   r   r   incQ      
zMemcachedCache.incc                 C   r%   r   )r   r   decrr'   r   r   r   decU   r*   zMemcachedCache.dec)Nr   N)r$   )
__name__
__module____qualname____doc__r	   classmethodr   r#   r)   r,   r   r   r   r   r      s    
"	

r   c                       s4   e Zd Z					d fdd	Zedd Z  ZS )SASLMemcachedCacheNr   c                    sJ   t  j|d |d u rdg}dd l}|j|f||dd|| _|| _d S )Nr   z127.0.0.1:11211r   T)usernamepasswordbinary)superr	   pylibmcClientr   r   )r   r   r   r   r3   r4   r   r7   	__class__r   r   r	   [   s   	
zSASLMemcachedCache.__init__c                 C   s<   | |d  |t|d |d |d d | |i |S Nr   CACHE_MEMCACHED_USERNAMECACHE_MEMCACHED_PASSWORDr   )r3   r4   r   r   r   r   r   r   r   q   s   zSASLMemcachedCache.factory)Nr   NNN)r-   r.   r/   r	   r1   r   __classcell__r   r   r9   r   r2   Z   s    r2   c                       sv   e Zd ZdZ fddZedd Z fddZd fd
d	Zd fdd	Z	d fdd	Z
dd Z fddZ  ZS )SpreadSASLMemcachedCachezSimple Subclass of SASLMemcached client that will spread the value
    across multiple keys if they are bigger than a given threshold.

    Spreading requires using pickle to store the value, which can significantly
    impact the performance.
    c                    s2   | dd| _| dd| _t j|i | dS )z
        Kwargs:
            chunksize (int): max length of a pickled object that can fit in
                memcached (memcache has an upper limit of 1MB for values,
                default: 1048448)
        	chunksizei maxchunk    N)getr@   rA   r6   r	   )r   r   r   r9   r   r   r	      s   z!SpreadSASLMemcachedCache.__init__c                 C   sB   | |d  |t|d|d|dd | |i |S r;   )r   r   r   rC   r   r   r   r   r      s   z SpreadSASLMemcachedCache.factoryc                    s    |  |D ]}t | qd S r   )_genkeysr6   delete)r   r"   skeyr9   r   r   rE      s   zSpreadSASLMemcachedCache.deleteNTc                    s&   |r
| j |||dS t j|||dS )a4  Set a value in cache, potentially spreading it across multiple key.

        :param key: The cache key.
        :param value: The value to cache.
        :param timeout: The timeout after which the cache will be invalidated.
        :param chunk: If set to `False`, then spreading across multiple keys
                      is disabled. This can be faster, but it will fail if
                      the value is bigger than the chunks. It requires you
                      to get back the object by specifying that it is not
                      spread.
        )timeout)_setr6   set)r   r"   valuerG   chunkr9   r   r   rI      s   zSpreadSASLMemcachedCache.setc           	         s   t |d}i }t|}td|| j}t|| jkr!td| j |D ]}|||| j  || d|| j  < q#t || d S )N   r   z'Cannot store value in less than %s keys.)	pickledumpslenranger@   rA   
ValueErrorr6   set_many)	r   r"   rJ   rG   
serializedvalueslen_serchksir9   r   r   rH      s   (zSpreadSASLMemcachedCache._setc                    s   |r|  |S t |S )zGet a cached value.

        :param chunk: If set to ``False``, it will return a cached value
                      that is spread across multiple keys.
        )_getr6   rC   )r   r"   rK   r9   r   r   rC      s   
zSpreadSASLMemcachedCache.getc                    s    fddt | jD S )Nc                       g | ]	}  d | qS rM   r   .0rX   r"   r   r   
<listcomp>       z5SpreadSASLMemcachedCache._genkeys.<locals>.<listcomp>)rQ   rA   )r   r"   r   r^   r   rD      s   z!SpreadSASLMemcachedCache._genkeysc                    sJ    fddt | jD }t j| }ddd |D }|s d S t|S )Nc                    rZ   r[   r   r\   r^   r   r   r_      r`   z1SpreadSASLMemcachedCache._get.<locals>.<listcomp>    c                 s   s    | ]	}|d ur|V  qd S r   r   )r]   vr   r   r   	<genexpr>   s    z0SpreadSASLMemcachedCache._get.<locals>.<genexpr>)rQ   rA   r6   get_manyjoinrN   loads)r   r"   to_getresultrT   r9   r^   r   rY      s   
zSpreadSASLMemcachedCache._get)NTr   )T)r-   r.   r/   r0   r	   r1   r   rE   rI   rH   rC   rD   rY   r>   r   r   r9   r   r?   ~   s    
r?   )r0   rN   recachelibr   r
   flask_caching.backends.baser   compilematchr   r2   r?   r   r   r   r   <module>   s    C$