o
    wvXj6  ã                   @   s@   d dl mZ d dlmZ d dlmZ G dd„ dƒZd	dd„ZdS )
é    )Úsettings)Úimport_string)ÚDefaultParserc                   @   sH   e Zd Zi Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dS )ÚConnectionFactoryc                 C   sR   |  dd¡}t|ƒ| _|  di ¡| _|  dd¡}t|ƒ| _|  di ¡| _|| _d S )NÚCONNECTION_POOL_CLASSzredis.connection.ConnectionPoolÚCONNECTION_POOL_KWARGSÚREDIS_CLIENT_CLASSzredis.client.RedisÚREDIS_CLIENT_KWARGS)Úgetr   Úpool_clsÚpool_cls_kwargsÚredis_client_clsÚredis_client_cls_kwargsÚoptions)Úselfr   Úpool_cls_pathÚredis_client_cls_path© r   úN/var/www/html/myproject/venv/lib/python3.10/site-packages/django_redis/pool.pyÚ__init__   s   ÿ


zConnectionFactory.__init__c                 C   sŒ   ||   ¡ dœ}| j dd¡}|r||d< | j dd¡}|r,t|ttfƒs(J dƒ‚||d< | j dd¡}|rDt|ttfƒs@J d	ƒ‚||d
< |S )zm
        Given a main connection parameters, build a complete
        dict of connection parameters.
        )ÚurlÚparser_classÚPASSWORDNÚpasswordÚSOCKET_TIMEOUTz)Socket timeout should be float or integerÚsocket_timeoutÚSOCKET_CONNECT_TIMEOUTz1Socket connect timeout should be float or integerÚsocket_connect_timeout)Úget_parser_clsr   r
   Ú
isinstanceÚintÚfloat)r   r   Úkwargsr   r   r   r   r   r   Úmake_connection_params   s.   þÿþÿþz(ConnectionFactory.make_connection_paramsc                 C   s   |   |¡}|  |¡}|S )zW
        Given a basic connection parameters,
        return a new connection.
        )r#   Úget_connection)r   r   ÚparamsÚ
connectionr   r   r   Úconnect<   s   

zConnectionFactory.connectc                 C   s    |   |¡}| jdd|i| j¤ŽS )z­
        Given a now preformated params, return a
        new connection.

        The default implementation uses a cached pools
        for create new connection.
        Úconnection_poolNr   )Úget_or_create_connection_poolr   r   )r   r%   Úpoolr   r   r   r$   E   s   
ÿÿz ConnectionFactory.get_connectionc                 C   s"   | j  dd ¡}|d u rtS t|ƒS )NÚPARSER_CLASS)r   r
   r   r   )r   Úclsr   r   r   r   R   s   z ConnectionFactory.get_parser_clsc                 C   s,   |d }|| j vr|  |¡| j |< | j | S )zÕ
        Given a connection parameters and return a new
        or cached connection pool for them.

        Reimplement this method if you want distinct
        connection pool instance caching behavior.
        r   )Ú_poolsÚget_connection_pool)r   r%   Úkeyr   r   r   r)   X   s   

z/ConnectionFactory.get_or_create_connection_poolc                 C   sV   t |ƒ}| | j¡ | jjdi |¤Ž}|j dd¡du r)| dd¡|jd< | ¡  |S )zÁ
        Given a connection parameters, return a new
        connection pool for them.

        Overwrite this method if you want a custom
        behavior on creating connection pool.
        r   Nr   )ÚdictÚupdater   r   Úfrom_urlÚconnection_kwargsr
   Úreset)r   r%   Ú	cp_paramsr*   r   r   r   r.   e   s   z%ConnectionFactory.get_connection_poolN)Ú__name__Ú
__module__Ú__qualname__r-   r   r#   r'   r$   r   r)   r.   r   r   r   r   r      s    	r   Nc                 C   s(   | d u r
t tddƒ} t| ƒ}||pi ƒS )NÚDJANGO_REDIS_CONNECTION_FACTORYz#django_redis.pool.ConnectionFactory)Úgetattrr   r   )Úpathr   r,   r   r   r   Úget_connection_factoryx   s   ýr<   )NN)Údjango.confr   Údjango.utils.module_loadingr   Úredis.connectionr   r   r<   r   r   r   r   Ú<module>   s
    r