U
    9a]                     @   s(   d dl Z d dlmZ G dd deZdS )    N)	BaseCachec                   @   s   e Zd ZdZd&ddZd	d
 Zdd Zdd Zdd Zdd Z	d'ddZ
d(ddZd)ddZdd Zdd Zdd Zdd  Zd*d"d#Zd+d$d%ZdS ),
RedisCachea  Uses the Redis key-value store as a cache backend.

    The first argument can be either a string denoting address of the Redis
    server or an object resembling an instance of a redis.Redis class.

    Note: Python Redis API already takes care of encoding unicode strings on
    the fly.

    :param host: address of the Redis server or an object which API is
                 compatible with the official Python Redis client (redis-py).
    :param port: port number on which Redis server listens for connections.
    :param password: password authentication for the Redis server.
    :param db: db (zero-based numeric index) on Redis Server to connect.
    :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 should be added to all keys.

    Any additional keyword arguments will be passed to ``redis.Redis``.
    	localhost  Nr   ,  c           	      K   s   t | | |d krtdt|trzdd l}W n tk
rN   tdY nX |dd rdtd|j	f ||||d|| _
n|| _
|pd| _d S )Nz)RedisCache host parameter may not be Noner   zno redis module foundZdecode_responsesz0decode_responses is not supported by RedisCache.)hostportpassworddb )r   __init__
ValueError
isinstancestrredisImportErrorRuntimeErrorgetZRedis_client
key_prefix)	selfr   r   r	   r
   Zdefault_timeoutr   kwargsr    r   2/tmp/pip-unpacked-wheel-bx08cqbu/cachelib/redis.pyr      s(    

   
zRedisCache.__init__c                 C   s   t | |}|dkrd}|S )Nr   )r   _normalize_timeout)r   timeoutr   r   r   r   7   s    zRedisCache._normalize_timeoutc                 C   s.   t |}t|tr t|dS dt| S )zDumps an object into a string for redis.  By default it serializes
        integers as regular string and pickle dumps everything else.
        ascii   !)typer   intr   encodepickledumps)r   valuetr   r   r   dump_object=   s    
zRedisCache.dump_objectc                 C   sl   |dkrdS | drDzt|dd W S  tjk
rB   Y dS X z
t|W S  tk
rf   | Y S X dS )zVThe reversal of :meth:`dump_object`.  This might be called with
        None.
        Nr      )
startswithr"   loadsPickleErrorr    r   )r   r$   r   r   r   load_objectF   s    

zRedisCache.load_objectc                 C   s   |  | j| j| S N)r+   r   r   r   r   keyr   r   r   r   W   s    zRedisCache.getc                    s2    j r fdd|D } fdd j|D S )Nc                    s   g | ]} j | qS r   r   .0r.   r   r   r   
<listcomp>\   s     z'RedisCache.get_many.<locals>.<listcomp>c                    s   g | ]}  |qS r   )r+   )r1   xr2   r   r   r3   ]   s     )r   r   Zmgetr   keysr   r2   r   get_manyZ   s    zRedisCache.get_manyc                 C   sP   |  |}| |}|dkr4| jj| j| |d}n| jj| j| ||d}|S )Nr   namer$   r9   r$   time)r   r&   r   setr   setex)r   r.   r$   r   dumpresultr   r   r   r<   _   s    

  zRedisCache.setc                 C   s@   |  |}| |}| jj| j| |do>| jj| j| |dS )Nr8   )r9   r;   )r   r&   r   Zsetnxr   Zexpire)r   r.   r$   r   r>   r   r   r   addj   s    

 zRedisCache.addc                 C   sp   |  |}| jjdd}| D ]F\}}| |}|dkrP|j| j| |d q |j| j| ||d q | S )NF)Ztransactionr   r8   r:   )	r   r   Zpipelineitemsr&   r<   r   r=   execute)r   mappingr   piper.   r$   r>   r   r   r   set_manyq   s    

zRedisCache.set_manyc                 C   s   | j | j| S r,   )r   deleter   r-   r   r   r   rF      s    zRedisCache.deletec                    s,   |sd S  j r  fdd|D } jj| S )Nc                    s   g | ]} j | qS r   r/   r0   r2   r   r   r3      s     z*RedisCache.delete_many.<locals>.<listcomp>)r   r   rF   r5   r   r2   r   delete_many   s
    zRedisCache.delete_manyc                 C   s   | j | j| S r,   )r   existsr   r-   r   r   r   has   s    zRedisCache.hasc                 C   s<   d}| j r.| j| j d }|r8| jj| }n
| j }|S )NF*)r   r   r6   rF   Zflushdb)r   statusr6   r   r   r   clear   s    
zRedisCache.clearr'   c                 C   s   | j j| j| |dS N)r9   amount)r   incrr   r   r.   deltar   r   r   inc   s    zRedisCache.incc                 C   s   | j j| j| |dS rM   )r   Zdecrr   rP   r   r   r   dec   s    zRedisCache.dec)r   r   Nr   r   N)N)N)N)r'   )r'   )__name__
__module____qualname____doc__r   r   r&   r+   r   r7   r<   r@   rE   rF   rG   rI   rL   rR   rS   r   r   r   r   r      s,         
	




r   )r"   Zcachelib.baser   r   r   r   r   r   <module>   s   