
    h                     J    S SK r S SKrS SKrS SKJr  S SKJr   " S S\5      rg)    N)	BaseCache)BaseSerializerc                     ^  \ rS rSrSr\" 5       r     SS\R                  S\R                  \
   S\R                  \
   S\S\R                  \
   S\R                  4U 4S	 jjjrS
\R                  4S jrS
\R                  4S jrS\
S
\R                  4S jrS\
S
\4S jr  S S\
S\R                  S\R                  \   S\R                  \   S
\R                  4
S jjrS!S\
S\R                  S\R                  \   S
\R                  4S jjr S!S\R(                  \
\R                  4   S\R                  \   S
\R*                  \R                     4S jjrS\
S
\R*                  \R                     4S jrS\
S
\R(                  \
\R                  4   4S jrS!S\
S\R                  S\R                  \   S
\R                  4S jjrS\
S
\4S jrS\
S
\R*                  \R                     4S jrS
\4S jrSrU =r$ )"MongoDbCache	   a  
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U ]  U5         SS KnUb  [        U[        5      (       a  WR                  US9nX   U   U l	        U R                  R                  5       nUR                  5        V	V
s1 s H  oS     H  oS   iM	     M     nn	n
SU;  a  U R                  R                  SSS9  U=(       d    SU l        X0l        g ! [         a    [        R
                  " S5         Nf = fs  sn
n	f )	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               eC:\Users\ROHAN GUPTA\OneDrive\Desktop\mathbuddy-assessment\venv\Lib\site-packages\cachelib/mongodb.pyr   MongoDbCache.__init__   s     	)	7 >Z44((f(5Fj,[[224
#-#4#4#6
#6%5\61I\I#6 	 
 xKK$$T$$7$*$  	7OO56	7
s   C
 6C0
 C-,C-returnc                     [         R                   R                  5       R                  [         R                  R                  S9$ )z<Return a tz-aware UTC datetime representing the current time)tzinfo)datetimeutcnowreplacetimezoneutcr    s    r&   _utcnowMongoDbCache._utcnow6   s1      '')119J9J9N9N1OO    c                 `    U R                   R                  SSU R                  5       005      nU$ )N
expirationz$lte)r   delete_manyr1   )r    ress     r&   _expire_recordsMongoDbCache._expire_records:   s+    kk%%|fdlln5M&NO
r3   r   c                     U R                  5         U R                  R                  SU R                  U-   05      nSnU(       a  U R                  R                  US   5      nU$ )zq
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)r8   r   find_oner   
serializerloads)r    r   recordr"   s       r&   getMongoDbCache.get>   sW     	%%tT__s-B&CDOO))&-8Er3   c                     U R                   R                  SU R                  U-   05      n[        UR                  S:  5      nU$ )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   r7   deleteds       r&   deleteMongoDbCache.deleteL   s@     kk$$dDOOc,A%BCs((1,-r3   r"   timeout	overwritec                 x   U R                  U5      nU R                  5       nU(       d  U R                  U5      (       a  gU R                  R	                  U5      nU R
                  U-   US.nUS:  a  U[        R                  " US9-   US'   U R                  R                  SU R
                  U-   0SU0S5        g)	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   r;   r   secondsr5   r   $setT)
_normalize_timeoutr1   hasr=   dumpsr   r+   	timedeltar   
update_one)r    r   r"   rI   rJ   nowdumpr?   s           r&   _setMongoDbCache._setX   s    & ))'2lln xx}}$$U+#-d;Q;#&););G)L#LF< doo&;<vv>NPTUr3   c                 D    U R                  5         U R                  XUSS9$ )NTrI   rJ   r8   rW   r    r   r"   rI   s       r&   setMongoDbCache.set|   s#    yyWyEEr3   mappingc           	         U R                  5         SSKJn  / nU R                  5       nU R	                  U5      nUR                  5        Hz  u  pgU R                  R                  U5      nU R                  U-   US.n	US:  a  U[        R                  " US9-   U	S'   UR                  U" SU R                  U-   0SU	0SS	95        M|     U R                  R                  U5      n
[        UR                  5       5      nU
R                   S
   [#        U5      :w  a[  U R                  R%                  SSU Vs/ s H  o`R                  U-   PM     sn005      n/ nU H  nUR                  US   5        M     U$ s  snf )Nr   )	UpdateOnerL   rM   r5   r   rO   T)upsert	nUpserted$in)r8   r   ra   r1   rP   itemsr=   rR   r   r+   rS   appendr   
bulk_writelistkeysbulk_api_resultlenfind)r    r_   rI   ra   
operationsrU   r   r;   rV   r?   resultri   queryitems                 r&   set_manyMongoDbCache.set_many   s[    	%
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   3E5ri   c                 |    U R                   " U6 n/ nU H$  nUR                  UR                  US 5      5        M&     U$ N)get_dictrf   r@   )r    ri   resultsr   r   s        r&   get_manyMongoDbCache.get_many   s;    --&CMM'++c401 r3   c           	      \   U R                  5         U R                  R                  SSU Vs/ s H  o R                  U-   PM     sn005      n[        R                  US 5      nU H>  nU R                  R                  US   5      nXdUS   [        U R                  5      S  '   M@     U$ s  snf )Nr   rd   r;   )	r8   r   rl   r   dictfromkeysr=   r>   rk   )r    ri   r   ro   rv   rp   r"   s          r&   ru   MongoDbCache.get_dict   s      ETBTcOOc1TBCD
 --d+DOO))$u+6E:?DJs4??3567   Cs   B)c                 D    U R                  5         U R                  XUSS9$ )NFrZ   r[   r\   s       r&   addMongoDbCache.add   s#    yyWyFFr3   c                 L    U R                  5         U R                  U5      nUS L$ rt   )r8   r@   )r    r   r?   s      r&   rQ   MongoDbCache.has   s&    #T!!r3   c                    U R                  5         [        U5      nSSU Vs/ s H  o0R                  U-   PM     sn00nU R                  R	                  U5      nUR
                  [        U5      :w  a\  U R                  R                  U5       Vs/ s H  ofS   [        U R                  5      S  PM     nnU Vs/ s H  ofU;  d  M
  UPM     nnU$ s  snf s  snf s  snf )Nr   rd   )r8   rh   r   r   r6   rE   rk   rl   )r    ri   r7   r   filterrn   rp   existing_keyss           r&   r6   MongoDbCache.delete_many   s    4j$ G$33!6$ GHI((03t9,?C{{?O?OPV?W?WtT
3t/12?W   %)FDD,E4DCF
 !H Gs   C$C:	CCc                 8    U R                   R                  5         gNT)r   dropr0   s    r&   clearMongoDbCache.clear   s    r3   )r   r
   r   )Nzcache-dbzcache-collectioni,  Nr   rt   )__name__
__module____qualname____firstlineno____doc__r   r=   _tAnyOptionalr   intr   r1   r8   r@   rD   rG   rW   r]   DictListrq   rw   ru   r~   rQ   r6   r   __static_attributes____classcell__)r%   s   @r&   r   r   	   sr     !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  r3   r   )	r+   r   typingr   cachelib.baser   cachelib.serializersr   r    r3   r&   <module>r      s"       # /A9 Ar3   