o
    2'hõ  ã                   @   sD   d dl Z d dlZd dlZd dlmZ d dlmZ G dd„ deƒZdS )é    N)Ú	BaseCache)ÚBaseSerializerc                       sÎ  e Zd ZdZeƒ Z					d0dejdeje	 deje	 d	e
d
eje	 dejf‡ fdd„Zdejfdd„Zdejfdd„Zde	dejfdd„Zde	defdd„Z		d1de	dejdeje
 deje dejf
dd„Zd2de	dejdeje
 dejfdd„Z	d2d eje	ejf deje
 dejej fd!d"„Zd#e	dejej fd$d%„Zd#e	deje	ejf fd&d'„Zd2de	dejdeje
 dejfd(d)„Zde	defd*d+„Zd#e	dejej fd,d-„Zdefd.d/„Z‡  ZS )3ÚMongoDbCacheaÚ  
    Implementation of cachelib.BaseCache that uses mongodb collection
    as the backend.

    Limitations: maximum MongoDB document size is 16mb

    :param client: mongodb client or connection string
    :param db: mongodb database name
    :param collection: mongodb collection name
    :param default_timeout: Set the timeout in seconds after which cache entries
                            expire
    :param key_prefix: A prefix that should be added to all keys.

    Núcache-dbúcache-collectioné,  ÚclientÚdbÚ
collectionÚdefault_timeoutÚ
key_prefixÚkwargsc           
         sª   t ƒ  |¡ zdd l}W n ty   t d¡ Y nw |d u s$t|tƒr*|j|d�}|| | | _	| j	 
¡ }dd„ | ¡ D ƒ}	d|	vrK| j	jddd� |pNd	| _|| _d S )
Nr   zno pymongo module found)Úhostc                 S   s"   h | ]}|d  D ]}|d ’qqS )Úkeyr   © )Ú.0ÚvalueÚsubkeyr   r   úFC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/mongodb.pyÚ	<setcomp>.   s
    ÿÿz(MongoDbCache.__init__.<locals>.<setcomp>ÚidT)ÚuniqueÚ )ÚsuperÚ__init__ÚpymongoÚImportErrorÚloggingÚwarningÚ
isinstanceÚstrZMongoClientr   Zindex_informationÚvaluesZcreate_indexr   r
   )
Úselfr   r	   r
   r   r   r   r   Ú
index_infoÚall_keys©Ú	__class__r   r   r      s"   	ÿ
ÿ

zMongoDbCache.__init__Úreturnc                 C   s   t j  ¡ jt jjd�S )z<Return a tz-aware UTC datetime representing the current time)Útzinfo)ÚdatetimeÚutcnowÚreplaceÚtimezoneÚutc©r"   r   r   r   Ú_utcnow6   s   zMongoDbCache._utcnowc                 C   s   | j  dd|  ¡ ii¡}|S )NÚ
expirationz$lte)r   Údelete_manyr/   )r"   Úresr   r   r   Ú_expire_records:   s   zMongoDbCache._expire_recordsr   c                 C   s:   |   ¡  | j d| j| i¡}d}|r| j |d ¡}|S )z‘
        Get a cache item

        :param key: The cache key of the item to fetch
        :return: cache value if not expired, else None
        r   NÚval)r3   r   Zfind_oner   Ú
serializerÚloads)r"   r   Úrecordr   r   r   r   Úget>   s   zMongoDbCache.getc                 C   s(   | j  d| j| i¡}t|jdkƒ}|S )zÍ
        Deletes an item from the cache.  This is a no-op if the item doesn't
        exist

        :param key: Key of the item to delete.
        :return: True if the key existed and was deleted
        r   r   )r   Z
delete_oner   ÚboolÚdeleted_count)r"   r   r2   Zdeletedr   r   r   ÚdeleteL   s   zMongoDbCache.deleteTr   ÚtimeoutÚ	overwritec                 C   s~   |   |¡}|  ¡ }|s|  |¡rdS | j |¡}| j| |dœ}|dkr.|tj|d� |d< | j 	d| j| id|id¡ dS )	a*  
        Store a cache item, with the option to not overwrite existing items

        :param key: Cache key to use
        :param value: a serializable object
        :param timeout: The timeout in seconds for the cached item, to override
                        the default
        :param overwrite: If true, overwrite any existing cache item with key.
                          If false, the new value will only be stored if no
                          non-expired cache item exists with key.
        :return: True if the new item was stored.
        F©r   r4   r   ©Úsecondsr0   r   ú$setT)
Ú_normalize_timeoutr/   Úhasr5   Údumpsr   r)   Ú	timedeltar   Z
update_one)r"   r   r   r<   r=   ÚnowÚdumpr7   r   r   r   Ú_setX   s   

zMongoDbCache._setc                 C   ó   |   ¡  | j|||dd�S )NT©r<   r=   ©r3   rH   ©r"   r   r   r<   r   r   r   Úset|   ó   zMongoDbCache.setÚmappingc                    s  ˆ   ¡  ddlm} g }ˆ  ¡ }ˆ  |¡}| ¡ D ]1\}}ˆ j |¡}ˆ j| |dœ}	|dkr9|t	j
|d� |	d< | |dˆ j| id|	idd	�¡ qˆ j |¡}
t| ¡ ƒ}|
jd
 t|ƒkrˆ j dd‡ fdd„|D ƒii¡}g }|D ]	}| |d ¡ qu|S )Nr   )Ú	UpdateOner>   r?   r0   r   rA   T)ZupsertZ	nUpsertedú$inc                    ó   g | ]}ˆ j | ‘qS r   ©r   ©r   r   r.   r   r   Ú
<listcomp>˜   ó    z)MongoDbCache.set_many.<locals>.<listcomp>)r3   r   rP   r/   rB   Úitemsr5   rD   r   r)   rE   Úappendr   Z
bulk_writeÚlistÚkeysZbulk_api_resultÚlenÚfind)r"   rO   r<   rP   Ú
operationsrF   r   r4   rG   r7   ÚresultrZ   ÚqueryÚitemr   r.   r   Úset_many€   s.   
ÿÿzMongoDbCache.set_manyrZ   c                 G   s.   | j |Ž }g }|D ]}| | |d ¡¡ q	|S ©N)Úget_dictrX   r8   )r"   rZ   Úresultsr!   r   r   r   r   Úget_manyŸ   s
   
zMongoDbCache.get_manyc                    sn   ˆ   ¡  ˆ j dd‡ fdd„|D ƒii¡}t |d ¡}|D ]}ˆ j |d ¡}|||d tˆ jƒd … < q|S )Nr   rQ   c                    rR   r   rS   rT   r.   r   r   rU   ©   rV   z)MongoDbCache.get_dict.<locals>.<listcomp>r4   )	r3   r   r\   ÚdictÚfromkeysr5   r6   r[   r   )r"   rZ   r_   rd   r`   r   r   r.   r   rc   ¦   s   ÿzMongoDbCache.get_dictc                 C   rI   )NFrJ   rK   rL   r   r   r   Úadd±   rN   zMongoDbCache.addc                 C   s   |   ¡  |  |¡}|d uS rb   )r3   r8   )r"   r   r7   r   r   r   rC   µ   s   
zMongoDbCache.hasc                    st   ˆ  ¡  t|ƒ}dd‡fdd„|D ƒii}ˆj |¡}|jt|ƒkr8‡fdd„ˆj |¡D ƒ‰ ‡ fdd„|D ƒ}|S )Nr   rQ   c                    rR   r   rS   rT   r.   r   r   rU   ½   rV   z,MongoDbCache.delete_many.<locals>.<listcomp>c                    s"   g | ]}|d  t ˆ jƒd… ‘qS )r   N)r[   r   ©r   r`   r.   r   r   rU   Á   s    ÿc                    s   g | ]}|ˆ vr|‘qS r   r   ri   )Úexisting_keysr   r   rU   Ä   s    )r3   rY   r   r1   r:   r[   r\   )r"   rZ   r2   Úfilterr^   r   )rj   r"   r   r1   º   s   

ÿzMongoDbCache.delete_manyc                 C   s   | j  ¡  dS ©NT)r   Zdropr.   r   r   r   ÚclearÈ   s   
zMongoDbCache.clear)Nr   r   r   Nrl   rb   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r5   Ú_tÚAnyÚOptionalr    Úintr   r/   r3   r8   r9   r;   rH   rM   ÚDictÚListra   re   rc   rh   rC   r1   rm   Ú__classcell__r   r   r%   r   r   	   sj    úþýüûúùûþýüû
ú&$ÿÿÿ

þ&r   )	r)   r   Útypingrr   Zcachelib.baser   Zcachelib.serializersr   r   r   r   r   r   Ú<module>   s    