
    Ջg                     J    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e      Zy)    N)	BaseCache)BaseSerializerc                       e Zd ZdZ e       Z	 	 	 	 	 ddej                  dej                  e	   dej                  e	   de
dej                  e	   dej                  f fdZd	ej                  fd
Zd	ej                  fdZde	d	ej                  fdZde	d	efdZ	 	 dde	dej                  dej                  e
   dej                  e   d	ej                  f
dZdde	dej                  dej                  e
   d	ej                  fdZ	 ddej&                  e	ej                  f   dej                  e
   d	ej(                  ej                     fdZde	d	ej(                  ej                     fdZde	d	ej&                  e	ej                  f   fdZdde	dej                  dej                  e
   d	ej                  fdZde	d	efdZde	d	ej(                  ej                     fdZd	efdZ xZS ) 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.

    clientdb
collectiondefault_timeout
key_prefixkwargsc                    t         |   |       	 dd l}|t        |t              rj                  |      }||   |   | _	        | j                  j                         }|j                         D 	
ch c]  }	|	d   D ]  }
|
d   	  }}	}
d|vr| j                  j                  dd       |xs d| _        || _        y # t        $ r t	        j
                  d       Y w xY wc c}
}	w )	Nr   zno pymongo module found)hostkeyidT)unique )super__init__pymongoImportErrorloggingwarning
isinstancestrMongoClientr   index_informationvaluescreate_indexr   r	   )selfr   r   r	   r
   r   r   r   
index_infovaluesubkeyall_keys	__class__s               C/var/www/html/venv/lib/python3.12/site-packages/cachelib/mongodb.pyr   zMongoDbCache.__init__   s     	)	7 >Z4((f(5FRj,[[224
#-#4#4#6
U5\
;AF1I

 
 xKK$$T$$7$*$  	7OO56	7
s   C 4C%C"!C"returnc                     t         j                   j                         j                  t         j                  j                        S )z<Return a tz-aware UTC datetime representing the current time)tzinfo)datetimeutcnowreplacetimezoneutcr   s    r%   _utcnowzMongoDbCache._utcnow6   s1      '')119J9J9N9N1OO    c                 `    | j                   j                  dd| j                         ii      }|S )N
expirationz$lte)r   delete_manyr/   )r   ress     r%   _expire_recordszMongoDbCache._expire_records:   s+    kk%%|fdlln5M&NO
r0   r   c                     | j                          | j                  j                  d| j                  |z   i      }d}|r| j                  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)r5   r   find_oner   
serializerloads)r   r   recordr!   s       r%   getzMongoDbCache.get>   sW     	%%tT__s-B&CDOO))&-8Er0   c                     | j                   j                  d| j                  |z   i      }t        |j                  dkD        }|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   
delete_oner   booldeleted_count)r   r   r4   deleteds       r%   deletezMongoDbCache.deleteL   s@     kk$$dDOOc,A%BCs((1,-r0   r!   timeout	overwritec                 f   | j                  |      }| j                         }|s| j                  |      ry| j                  j	                  |      }| j
                  |z   |d}|dkD  r|t        j                  |      z   |d<   | j                  j                  d| j
                  |z   id|id       y)	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   r7   r   secondsr2   r   $setT)
_normalize_timeoutr/   hasr9   dumpsr   r)   	timedeltar   
update_one)r   r   r!   rC   rD   nowdumpr;   s           r%   _setzMongoDbCache._setX   s    & ))'2lln xx}$$U+#-d;Q;#&););G)L#LF< doo&;<vv>NPTUr0   c                 L    | j                          | j                  |||d      S )NTrC   rD   r5   rQ   r   r   r!   rC   s       r%   setzMongoDbCache.set|   s%    yyeWyEEr0   mappingc           	         | j                          ddlm} g }| j                         }| j	                  |      }|j                         D ]~  \  }}| j                  j                  |      }| j                  |z   |d}	|dkD  r|t        j                  |      z   |	d<   |j                   |d| j                  |z   id|	id	              | j                  j                  |      }
t        |j                               }|
j                   d
   t#        |      k7  rW| j                  j%                  dd|D cg c]  }| j                  |z    c}ii      }g }|D ]  }|j                  |d           |S c c}w )Nr   )	UpdateOnerF   rG   r2   r   rI   T)upsert	nUpserted$in)r5   r   rY   r/   rJ   itemsr9   rL   r   r)   rM   appendr   
bulk_writelistkeysbulk_api_resultlenfind)r   rW   rC   rY   
operationsrO   r   r7   rP   r;   resultra   queryitems                 r%   set_manyzMongoDbCache.set_many   se    	%
lln))'2 		HC??((-D OOc1$?F{'*X-?-?-P'P|$43!67&&9IRVW		 ''
3GLLN#!!+.#d);KK$$F## 5FGHE D (DJ'(  Gs   5E3ra   c                 x     | j                   | }g }|D ]#  }|j                  |j                  |d              % |S N)get_dictr^   r<   )r   ra   resultsr   r   s        r%   get_manyzMongoDbCache.get_many   sB    $--& 	2CMM'++c401	2r0   c           	      V   | j                          | j                  j                  dd|D cg c]  }| j                  |z    c}ii      }t        j                  |d       }|D ]>  }| j                  j                  |d         }|||d   t        | j                        d  <   @ |S c c}w )Nr   r\   r7   )	r5   r   rd   r   dictfromkeysr9   r:   rc   )r   ra   r   rg   rm   rh   r!   s          r%   rl   zMongoDbCache.get_dict   s      ETBcDOOc1BCD
 --d+ 	@DOO))$u+6E:?GDJs4??3567	@  Cs   B&c                 L    | j                          | j                  |||d      S )NFrS   rT   rU   s       r%   addzMongoDbCache.add   s%    yyeWyFFr0   c                 L    | j                          | j                  |      }|d uS rk   )r5   r<   )r   r   r;   s      r%   rK   zMongoDbCache.has   s&    #T!!r0   c                    | j                          t        |      }dd|D cg c]  }| j                  |z    c}ii}| j                  j	                  |      }|j
                  t        |      k7  rU| j                  j                  |      D cg c]  }|d   t        | j                        d   }}|D cg c]	  }||vs| }}|S c c}w c c}w c c}w )Nr   r\   )r5   r`   r   r   r3   r@   rc   rd   )r   ra   r4   r   filterrf   rh   existing_keyss           r%   r3   zMongoDbCache.delete_many   s    4j$ G33!6 GHI((03t9,?C{{?O?OPV?W7;T
3t/12M  %)FDD,E4FCF
 !H Gs   C"C5	C?Cc                 8    | j                   j                          yNT)r   dropr.   s    r%   clearzMongoDbCache.clear   s    r0   )Nzcache-dbzcache-collectioni,  Nry   rk   )__name__
__module____qualname____doc__r   r9   _tAnyOptionalr   intr   r/   r5   r<   r?   rB   rQ   rV   DictListri   rn   rl   rs   rK   r3   r{   __classcell__)r$   s   @r%   r   r   	   sh     !J )'9"'+%% KK% KK$	%
 % KK$% &&%6P P s rvv 
# 
$ 
  %)'+"" vv" S!	"
 ;;t$" 
"HFs F266 FBKK4D FPRPVPV F
 JNwwsBFF{+68kk#6F	>c bggbffo 	c 	bggc266k&: 	Gs G266 GBKK4D GPRPVPV G"s "t "
  t r0   r   )	r)   r   typingr   cachelib.baser   cachelib.serializersr   r    r0   r%   <module>r      s"       # /A9 Ar0   