Format: application/x-python-code
⬇ Download File (7.2 KB)
   1  o
   2  
   3  2'h��@sDddlZddlZddlZddlmZddlmZGdd�de�ZdS)�N)�	BaseCache)�BaseSerializerc
   4  s�eZdZdZe�Z					d0dejdeje	deje	d	e
   5  d
   6  eje	dejf�fdd
   7  �
   8  Zdejfdd�Zdejfdd�Z
   9  de	dejfdd�Zde	defdd�Z		d1de	dejdeje
  10  dejedejf
  11  dd�Zd2de	dejdeje
  12  dejfdd�Z	d2d eje	ejfdeje
  13  dejejfd!d"�Zd#e	dejejfd$d%�Zd#e	deje	ejffd&d'�Zd2de	dejdeje
  14  dejfd(d)�Zde	defd*d+�Zd#e	dejejfd,d-�Zdefd.d/�Z�ZS)3�MongoDbCachea�
  15      Implementation of cachelib.BaseCache that uses mongodb collection
  16      as the backend.
  17  
  18      Limitations: maximum MongoDB document size is 16mb
  19  
  20      :param client: mongodb client or connection string
  21      :param db: mongodb database name
  22      :param collection: mongodb collection name
  23      :param default_timeout: Set the timeout in seconds after which cache entries
  24                              expire
  25      :param key_prefix: A prefix that should be added to all keys.
  26  
  27      N�cache-db�cache-collection�,�client�db�
  28  collection�default_timeout�
  29  key_prefix�kwargsc
  30  s�t��|�zddl}Wntyt�d�Ynw|dus$t|t�r*|j|d�}||||_	|j	�
  31  �}dd�|��D�}	d|	vrK|j	jddd�|pNd	|_
  32  ||_dS)
  33  Nrzno pymongo module found)�hostcSs"h|]
  34  }|dD]}|d�qqS)�keyr�)�.0�value�subkeyrr�FC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/mongodb.py�	<setcomp>.s
  35  ��z(MongoDbCache.__init__.<locals>.<setcomp>�idT)�unique�)�super�__init__�pymongo�ImportError�logging�warning�
  36  isinstance�strZMongoClientrZindex_information�valuesZcreate_indexrr
  37  )
  38  �selfrr	r
  39  rrr
  40  r�
  41  index_info�all_keys��	__class__rrrs"	�
  42  �
  43  
  44  zMongoDbCache.__init__�returncCstj��jtjjd�S)z<Return a tz-aware UTC datetime representing the current time)�tzinfo)�datetime�utcnow�replace�timezone�utc�r"rrr�_utcnow6szMongoDbCache._utcnowcCs|j�dd|��ii�}|S)N�
  45  expirationz$lte)r�delete_manyr/)r"�resrrr�_expire_records:szMongoDbCache._expire_recordsrcCs:|��|j�d|j|i�}d}|r|j�|d�}|S)z�
  46          Get a cache item
  47  
  48          :param key: The cache key of the item to fetch
  49          :return: cache value if not expired, else None
  50          rN�val)r3rZfind_oner�
  51  serializer�loads)r"r�recordrrrr�get>szMongoDbCache.getcCs(|j�d|j|i�}t|jdk�}|S)z�
  52          Deletes an item from the cache.  This is a no-op if the item doesn't
  53          exist
  54  
  55          :param key: Key of the item to delete.
  56          :return: True if the key existed and was deleted
  57          rr)rZ
  58  delete_oner�bool�
  59  deleted_count)r"rr2Zdeletedrrr�deleteLszMongoDbCache.deleteTr�timeout�	overwritecCs~|�|�}|��}|s|�|�rdS|j�|�}|j||d�}|dkr.|tj|d�|d<|j�	d|j|id|id�dS)	a*
  60          Store a cache item, with the option to not overwrite existing items
  61  
  62          :param key: Cache key to use
  63          :param value: a serializable object
  64          :param timeout: The timeout in seconds for the cached item, to override
  65                          the default
  66          :param overwrite: If true, overwrite any existing cache item with key.
  67                            If false, the new value will only be stored if no
  68                            non-expired cache item exists with key.
  69          :return: True if the new item was stored.
  70          F�rr4r��secondsr0r�$setT)
  71  �_normalize_timeoutr/�hasr5�dumpsrr)�	timedeltarZ
  72  update_one)r"rrr<r=�now�dumpr7rrr�_setXs
  73  
  74  zMongoDbCache._setcC�|��|j|||dd�S)NT�r<r=�r3rH�r"rrr<rrr�set|�zMongoDbCache.set�mappingcs���ddlm}g}���}��|�}|��D]1\}}�j�|�}�j||d�}	|dkr9|t	j
  75  |d�|	d<|�|d�j|id|	idd	��q�j�
  76  |�}
  77  t|���}|
  78  jd
  79  t|�kr�j�dd�fdd
  80  �|D�ii�}g}|D]	}
  81  |�|
  82  d�qu|S)Nr)�	UpdateOner>r?r0rrAT)ZupsertZ	nUpserted�$inc�g|]}�j|�qSr�r�rrr.rr�
  83  <listcomp>��z)MongoDbCache.set_many.<locals>.<listcomp>)r3rrPr/rB�itemsr5rDrr)rE�appendrZ
  84  bulk_write�list�keysZbulk_api_result�len�find)r"rOr<rP�
  85  operationsrFrr4rGr7�resultrZ�query�itemrr.r�set_many�s.
  86  ��zMongoDbCache.set_manyrZcGs.|j|�}g}|D]}|�|�|d��q	|S�N)�get_dictrXr8)r"rZ�resultsr!rrrr�get_many�s
  87  
  88  zMongoDbCache.get_manycsn����j�dd�fdd�|D�ii�}t�|d�}|D]}�j�|d�}|||dt�j�d�<q|S)NrrQcrRrrSrTr.rrrU�rVz)MongoDbCache.get_dict.<locals>.<listcomp>r4)	r3rr\�dict�fromkeysr5r6r[r)r"rZr_rdr`rrr.rrc�s�zMongoDbCache.get_dictcCrI)NFrJrKrLrrr�add�rNzMongoDbCache.addcCs|��|�|�}|duSrb)r3r8)r"rr7rrrrC�s
  89  zMongoDbCache.hascst���t|�}dd�fdd�|D�ii}�j�|�}|jt|�kr8�fdd��j�|�D���fdd�|D�}|S)NrrQcrRrrSrTr.rrrU�rVz,MongoDbCache.delete_many.<locals>.<listcomp>cs"g|]
  90  }|dt�j�d��qS)rN)r[r�rr`r.rrrU�s�csg|]}|�vr|�qSrrri)�
  91  existing_keysrrrU�s)r3rYrr1r:r[r\)r"rZr2�filterr^r)rjr"rr1�s
  92  
  93  �zMongoDbCache.delete_manycCs|j��dS�NT)rZdropr.rrr�clear�s
  94  zMongoDbCache.clear)NrrrNrlrb)�__name__�
  95  __module__�__qualname__�__doc__rr5�_t�Any�Optionalr �intrr/r3r8r9r;rHrM�Dict�ListrarercrhrCr1rm�
  96  __classcell__rrr%rr	sj������������
  97  �&$���
  98  
  99  �&r)	r)r�typingrrZ
 100  cachelib.baserZcachelib.serializersrrrrrr�<module>s