Format: application/x-python-code
⬇ Download File (6.9 KB)
   1  o
   2  
   3  2'h��@sHddlZddlZddlmZddlmZe�d�jZGdd�de�Z	dS)�N)�time)�	BaseCachez[^\x00-\x21\xff]{1,250}$c
   4  @s�eZdZdZ			d/dejdedejefdd�Z	d	ed
   5  efdd�Z
   6  d
   7  ejed
   8  efdd�Zd	ed
   9  ejfdd�Zded
  10  ej
  11  eejffdd�Zd0d	edejd
  12  ejed
  13  efdd�Z	d0d	edejd
  14  ejed
  15  ejefdd�Zded
  16  ejejfdd�Z	d0dej
  17  eejfd
  18  ejed
  19  ejejfdd�Zd	ed
  20  efdd �Zded
  21  ejejfd!d"�Zd	ed
  22  efd#d$�Zd
  23  efd%d&�Zd1d	ed(ed
  24  ejefd)d*�Zd1d	ed(ed
  25  ejefd+d,�Zdejd
  26  ejfd-d.�ZdS)2�MemcachedCachea�A cache that uses memcached as backend.
  27  
  28      The first argument can either be an object that resembles the API of a
  29      :class:`memcache.Client` or a tuple/list of server addresses. In the
  30      event that a tuple/list is passed, Werkzeug tries to import the best
  31      available memcache library.
  32  
  33      This cache looks into the following packages/modules to find bindings for
  34      memcached:
  35  
  36          - ``pylibmc``
  37          - ``google.appengine.api.memcached``
  38          - ``memcached``
  39          - ``libmc``
  40  
  41      Implementation notes:  This cache backend works around some limitations in
  42      memcached to simplify the interface.  For example unicode keys are encoded
  43      to utf-8 on the fly.  Methods such as :meth:`~BaseCache.get_dict` return
  44      the keys in the same format as passed.  Furthermore all get methods
  45      silently ignore key errors to not cause problems when untrusted user data
  46      is passed to the get methods which is often the case in web applications.
  47  
  48      :param servers: a list or tuple of server addresses or alternatively
  49                      a :class:`memcache.Client` or a compatible client.
  50      :param default_timeout: the default timeout that is used if no timeout is
  51                              specified on :meth:`~BaseCache.set`. A timeout of
  52                              0 indicates that the cache never expires.
  53      :param key_prefix: a prefix that is added before all keys.  This makes it
  54                         possible to use the same memcached server for different
  55                         applications.  Keep in mind that
  56                         :meth:`~BaseCache.clear` will also clear keys with a
  57                         different prefix.
  58      N�,�servers�default_timeout�
  59  key_prefixcCs`t�||�|dust|ttf�r(|durdg}|�|�|_|jdur'td��n||_||_dS)Nz127.0.0.1:11211zno memcache module found)	r�__init__�
  60  isinstance�list�tuple�import_preferred_memcache_lib�_client�RuntimeErrorr)�selfrrr�r�HC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/memcached.pyr	.s
  61  �
  62  zMemcachedCache.__init__�key�returncCs|jr|j|}|S�N)r�rrrrr�_normalize_keyBs
  63  zMemcachedCache._normalize_key�timeoutcCs&t�||�}|dkrtt��|}|S�Nr)r�_normalize_timeout�intr)rrrrrrGsz!MemcachedCache._normalize_timeoutcCs"|�|�}t|�r|j�|�SdSr)r�_test_memcached_keyr�getrrrrrMs
  64  �zMemcachedCache.get�keysc	Gs�i}|D]}|�|�}t|�r|||<qt|�}|j�|�}}|jr4i}|��D]
  65  \}}||||<q)t|�t|�krI|D]
  66  }||vrHd||<q>|Sr)rrrrZ	get_multir�items�len)	rrZkey_mappingrZencoded_keyZ_keys�d�rv�valuerrr�get_dictUs$
  67  ��zMemcachedCache.get_dictr#cC�(|�|�}|�|�}t|j�|||��Sr)rr�boolr�add�rrr#rrrrr'gs
  68  
  69  zMemcachedCache.addcCr%r)rrr&r�setr(rrrr)ls
  70  
  71  zMemcachedCache.setcs|j|���fdd�|D�S)Ncsg|]}�|�qSrr)�.0r�r!rr�
  72  <listcomp>usz+MemcachedCache.get_many.<locals>.<listcomp>)r$)rrrr+r�get_manyss
  73  zMemcachedCache.get_many�mappingcsdi}|��D]
  74  \}}|�|�}|||<q|�|�}|j�||��t|��|���}�fdd�|D�S)Ncsg|]
  75  \}}|�vr|�qSrr)r*�kZnkey�Zfailed_keysrrr,�sz+MemcachedCache.set_many.<locals>.<listcomp>)rrrrZ	set_multi�zipr)rr.rZnew_mappingrr#Z	k_normkeyrr0r�set_manyws
  76  
  77  
  78  �zMemcachedCache.set_manycCs&|�|�}t|�rt|j�|��SdS)NF)rrr&r�deleterrrrr3�s
  79  zMemcachedCache.deletecsHg}|D]}��|�}t|�r|�|�q�j�|��fdd�|D�S)Ncsg|]	}��|�s|�qSr)�has)r*r/�rrrr,�sz.MemcachedCache.delete_many.<locals>.<listcomp>)rr�appendrZdelete_multi)rrZnew_keysrrr5r�delete_many�s
  80  
  81  �zMemcachedCache.delete_manycCs(|�|�}t|�rt|j�|d��SdS)N�F)rrr&rr6rrrrr4�s
  82  zMemcachedCache.hascCst|j���Sr)r&rZ	flush_allr5rrr�clear�szMemcachedCache.clear��deltacCs2|�|�}|j�|�pd|}|�||�r|SdSr�rrrr)�rrr;r#rrr�inc��
  83  zMemcachedCache.inccCs2|�|�}|j�|�pd|}|�||�r|SdSrr<r=rrr�dec�r?zMemcachedCache.deccCs�zddl}Wn	tyYnw|�|�Szddlm}W|��Sty)Ynwzddl}Wn	ty9Ynw|�|�Szddl}Wn
  84  tyOYdSw|�|�S)zAReturns an initialized memcache client.  Used by the constructor.rN)�memcache)�pylibmc�ImportErrorZClientZgoogle.appengine.apirA�libmc)rrrBrArDrrrr
  85  �s0�
  86  ���
  87  �
  88  z,MemcachedCache.import_preferred_memcache_lib)NrNr)r:)�__name__�
  89  __module__�__qualname__�__doc__�_t�Anyr�Optional�strr	rrr�Dictr$r&r'r)�Listr-r2r3r7r4r9r>r@r
  90  rrrrrsV$���
  91  �$����
  92  ����
  93  
  94  �	r)
  95  �re�typingrIrZ
  96  cachelib.baser�compile�matchrrrrrr�<module>s