o
    Df                     @   s   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ dZd	gZd
ZdZe	ddZdd Zdd ZG dd dZeeedd dZG dd deZd	S )z-Memcached and in-memory cache result backend.    )bytes_to_strensure_bytes)cached_property)ImproperlyConfigured)LRUCache   )KeyValueStoreBackend)CacheBackendNzBThe Memcached backend requires either pylibmc or python-memcached.zYThe cache backend {0!r} is unknown,
Please use one of the following backends instead: {1}i  )limitc                  C   st   t d d u r6dt} }zdd l}d} W n ty.   zdd l}W n ty+   ttw Y nw | ||ft d< t d S )Nr   FT)_impr   pylibmcImportErrormemcacher   REQUIRES_BACKEND)
is_pylibmcmemcache_key_tr    r   N/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/backends/cache.pyimport_best_memcache   s   
r   c                     s.   t  \}}}|j } |s fdd}||fS )Nc                     s   | dd   | i |S )N	behaviors)pop)argskwargs_Clientr   r   Client2   s   z!get_best_memcache.<locals>.Client)r   r   )r   r   r   r   key_tr   r   r   r   get_best_memcache+   s
   
r   c                   @   sF   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdddZdd Z	dS )DummyClientc                 O   s
   t | _d S N)_DUMMY_CLIENT_CACHEcache)selfr   r   r   r   r   __init__;   s   
zDummyClient.__init__c                 O      | j |S r   )r!   getr"   keyr   r   r   r   r   r%   >      zDummyClient.getc                    s   | j   fdd|D S )Nc                    s   i | ]}| v r| | qS r   r   ).0kr!   r   r   
<dictcomp>C   s    z)DummyClient.get_multi.<locals>.<dictcomp>r+   r"   keysr   r+   r   	get_multiA   s   zDummyClient.get_multic                 O   s   || j |< d S r   r+   )r"   r'   valuer   r   r   r   r   setE      zDummyClient.setc                 O   s   | j |d  d S r   )r!   r   r&   r   r   r   deleteH      zDummyClient.deleter   c                 C      | j ||S r   )r!   incr)r"   r'   deltar   r   r   r6   K   r2   zDummyClient.incrc                 C   s   d S r   r   )r"   r'   expirer   r   r   touchN   s   zDummyClient.touchN)r   )
__name__
__module____qualname__r#   r%   r/   r1   r3   r6   r9   r   r   r   r   r   9   s    
r   c                   C   s   t tfS r   )r   r   r   r   r   r   <lambda>V   s    r=   )r   	memcachedr   memoryc                       s   e Zd ZdZdZdZdZdZ		d fdd	Zdd Z	dd	 Z
d
d Zdd Z fddZdd Zdd Zedd Zd fdd	Zdd Z  ZS )r	   zCache result backend.NTc           	   
      s   |si n|}t  j|fi | || _t| jjjfi || _|p'|p'| jjj| _	| j	r?| j	
d\| _	}}|dd| _| j|td| _zt| j	  \| _| _W n tyf   tt| j	dtw |   d S )N:///;)typez, )superr#   urldictappconfcache_backend_optionsoptionscache_backendbackend	partitionrstripsplitserversprepare_expiresintexpiresbackendsr   r   KeyErrorr   UNKNOWN_BACKENDformatjoin_encode_prefixes)	r"   rG   rS   rL   rJ   rE   r   _rP   	__class__r   r   r#   b   s&   zCacheBackend.__init__c                 C   r$   r   )clientr%   r"   r'   r   r   r   r%   w   r(   zCacheBackend.getc                 C   r$   r   )r]   r/   r-   r   r   r   mgetz   r(   zCacheBackend.mgetc                 C   s   | j ||| jS r   )r]   r1   rS   r"   r'   r0   r   r   r   r1   }   r4   zCacheBackend.setc                 C   r$   r   )r]   r3   r^   r   r   r   r3      r(   zCacheBackend.deletec                    s8   |  |d }| jj|d| jd t j||fi |S )Nr   )time)get_key_for_chordr]   r1   rS   rD   _apply_chord_incr)r"   header_result_argsbodyr   	chord_keyr[   r   r   rc      s   zCacheBackend._apply_chord_incrc                 C   r$   r   )r]   r6   r^   r   r   r   r6      r(   zCacheBackend.incrc                 C   r5   r   )r]   r9   r`   r   r   r   r8      r2   zCacheBackend.expirec                 C   s   | j | jfi | jS r   )r   rP   rJ   )r"   r   r   r   r]      s   zCacheBackend.clientr   c                    sN   |si n|}d | j}| j d| d}||| j| jd t ||S )NrB   r@   rA   )rL   rS   rJ   )rX   rP   rL   updaterS   rJ   rD   
__reduce__)r"   r   r   rP   rL   r[   r   r   rh      s   zCacheBackend.__reduce__c                 O   s   d | j}| j d| dS )zcReturn the backend as an URI.

        This properly handles the case of multiple servers.
        rB   r@   rA   )rX   rP   rL   )r"   r   r   rP   r   r   r   as_uri   s   zCacheBackend.as_uri)NNNN)r   N)r:   r;   r<   __doc__rP   supports_autoexpiresupports_native_joinimplements_incrr#   r%   r_   r1   r3   rc   r6   r8   r   r]   rh   ri   __classcell__r   r   r[   r   r	   Z   s(    

r	   )rj   kombu.utils.encodingr   r   kombu.utils.objectsr   celery.exceptionsr   celery.utils.functionalr   baser   __all__r   r   rV   r    r   r   r   rT   r	   r   r   r   r   <module>   s(    
