
    h@!                     J    S SK r S SKrS SKJr  S SKJr  SrSr " S S\5      r	g)    N)	BaseCache)DynamoDbSerializer
created_atresponsec                     ^  \ rS rSrSr\" 5       r     SS\R                  \	   S\
S\R                  \	   S\R                  \	   S\R                  \	   S\R                  4U 4S	 jjjrS
\R                  4S jrSS\	S\R                  \   S
\R                  4S j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\	S\R                  S\R                  \
   S
\R                  4S jjrS\	S
\4S jrS
\4S jrSrU =r$ )DynamoDbCache   a  
Implementation of cachelib.BaseCache that uses an AWS DynamoDb table
as the backend.

Your server process will require dynamodb:GetItem and dynamodb:PutItem
IAM permissions on the cache table.

Limitations: DynamoDB table items are limited to 400 KB in size.  Since
this class stores cached items in a table, the max size of a cache entry
will be slightly less than 400 KB, since the cache key and expiration
time fields are also part of the item.

:param table_name: The name of the DynamoDB table to use
:param default_timeout: Set the timeout in seconds after which cache entries
                        expire
:param key_field: The name of the hash_key attribute in the DynamoDb
                  table. This must be a string attribute.
:param expiration_time_field: The name of the table attribute to store the
                              expiration time in.  This will be an int
                              attribute. The timestamp will be stored as
                              seconds past the epoch.  If you configure
                              this as the TTL field, then DynamoDB will
                              automatically delete expired entries.
:param key_prefix: A prefix that should be added to all keys.


table_namedefault_timeout	key_fieldexpiration_time_field
key_prefixkwargsc                   > [         TU ]  U5         SS KnXl        X0l        X@l        U=(       d    SU l        UR                  " S0 UD6U l
        UR                  R                  R                  U l         U R                  R                  U5      U l        U R                   R#                  5         g ! [         a  n[	        S5      UeS nAff = f! [$         a    U R                  R'                  USS./UUSS./SS	9n	U	R)                  5         UR*                  " S0 UD6n
U
R-                  US
US.S9  U R                  R                  U5      U l        U R                   R#                  5          g f = f)Nr   zno boto3 module found S)AttributeNameAttributeTypeHASH)r   KeyTypePAY_PER_REQUEST)AttributeDefinitions	TableName	KeySchemaBillingModeT)Enabledr   )r   TimeToLiveSpecification)dynamodb)super__init__boto3ImportErrorRuntimeError_table_name
_key_field_expiration_time_fieldr   resource_dynamor   
conditionsAttr_attrTable_tableload	Exceptioncreate_tablewait_until_existsclientupdate_time_to_live)selfr
   r   r   r   r   r   r!   errtabledynamo	__class__s              fC:\Users\ROHAN GUPTA\OneDrive\Desktop\mathbuddy-assessment\venv\Lib\site-packages\cachelib/dynamodb.pyr    DynamoDbCache.__init__)   sf    	)	A &#&;#$*~~;F;^^..33
	,,,,Z8DKKK  	A67S@	A  	LL--&/#F& %&/FC . . 	E ##%\\77F&&$#%:) '  ,,,,Z8DKKK+	s*   B0 5:C 0
C:CCBE.-E.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)r4   s    r9   _utcnowDynamoDbCache._utcnow[   s1      '')119J9J9N9N1OO    key
attributesc                    0 nU(       aA  U R                   U;  a  [        U5      U R                   /-   n[        SR                  U5      S9nU R                  R
                  " SSU R                  U00UD6nUR                  S5      nU(       aL  [        U R                  5       R                  5       5      nUR                  U R                   US-   5      U:  a  U$ g)a  
Get an item from the cache table, optionally limiting the returned
attributes.

:param key: The cache key of the item to fetch

:param attributes: An optional list of attributes to fetch.  If not
                   given, all attributes are fetched.  The
                   expiration_time field will always be added to the
                   list of fetched attributes.
:return: The table item for key if it exists and is not expired, else
         None
,)ProjectionExpressionKeyItemd   N )r&   listdictjoinr-   get_itemr%   getintrC   	timestamp)r4   rF   rG   r   r   
cache_itemnows          r9   	_get_itemDynamoDbCache._get_item_   s     ***<!*-1L1L0MM
sxx
/CDF;;''MT__c,BMfM\\&)
dlln..01C~~d9939EK!!rE   c                     U R                  U R                  U-   5      nU(       a&  U[           nU R                  R	                  U5      nU$ g)zq
Get a cache item

:param key: The cache key of the item to fetch
:return: cache value if not expired, else None
N)rX   r   RESPONSE_FIELD
serializerloads)r4   rF   rV   r   values        r9   rS   DynamoDbCache.get}   sD     ^^DOOc$9:
!.1HOO))(3ELrE   c                 6    U R                   R                  U R                  U R                  U-   0U R	                  U R                  5      R                  5       S9  g! U R                  R                  R                  R                  R                   a     gf = f)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
)rK   ConditionExpressionTF)r-   delete_itemr%   r   r+   existsr(   metar2   
exceptionsConditionalCheckFailedExceptionr4   rF   s     r9   deleteDynamoDbCache.delete   s    	KK##__doo&;<$(JJt$?$F$F$H $  ||  ''22RR 		s   AA 8BBr^   timeout	overwritec                    U R                  U5      nU R                  5       n0 nU(       dt  U R                  U R                  5      R	                  5       U R                  U R
                  5      R                  [        UR                  5       5      5      -  n[        US9n U R                  R                  U5      nU R                  U[        UR                  5       [        U0n	US:  a<  U[        R                   " US9-   n
[        U
R                  5       5      XR
                  '   U R"                  R$                  " SSU	0UD6  g! [&         a     gf = f)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.
)ra   r   )secondsrL   TFrN   )_normalize_timeoutrC   r+   r%   
not_existsr&   lterT   rU   rP   r\   dumpsCREATED_AT_FIELD	isoformatr[   r>   	timedeltar-   put_itemr/   )r4   rF   r^   rj   rk   rW   r   conddumpitemexpiration_times              r9   _setDynamoDbCache._set   s    & ))'2lln ::doo.99;djj++?c#cmmo&'(D d3F	??((/D #--/D
 {"%(:(:7(K"K478Q8Q8S4T001KK  5d5f5 		s    B#E 
EEc                 >    U R                  U R                  U-   X#SS9$ )NTrj   rk   rz   r   r4   rF   r^   rj   s       r9   setDynamoDbCache.set   s!    yy3.RVyWWrE   c                 >    U R                  U R                  U-   X#SS9$ )NFr}   r~   r   s       r9   addDynamoDbCache.add   s!    yy3.RWyXXrE   c                 Z    U R                  U R                  U-   U R                  /5      S L$ N)rX   r   r&   rg   s     r9   hasDynamoDbCache.has   s/    NN4??S043N3N2OP	
rE   c                 Z   U R                   R                  R                  R                  S5      nUR	                  U R
                  U R                  S9nU R                  R                  5        nU H  nUS    H  nUR                  US9  M     M      S S S 5        g! , (       d  f       g= f)Nscan)r   rJ   Items)rK   T)
r(   rd   r2   get_paginatorpaginater$   r%   r-   batch_writerrb   )r4   	paginatorpagesbatchpagerx   s         r9   clearDynamoDbCache.clear   s    LL%%,,::6B	""&&T__ # 
 [[%%'5 MD%%$%/ *  (
  ('
 s   .%B
B*)r+   r(   r&   r%   r-   r$   r   )zpython-cachei,  	cache_keyry   Nr   )NT)__name__
__module____qualname____firstlineno____doc__r   r\   _tOptionalstrrT   Anyr    rC   rO   rX   rS   boolrh   rz   r   r   r   r   __static_attributes____classcell__)r8   s   @r9   r   r      s   6 $%J (6"&12C'+0KK$0 0 ;;s#	0
  "{{3/0 KK$0 &&0 0dP PS bkk$.? 266 <s rvv # $ * %)'+-- vv- S!	-
 ;;t$- 
-^Xs X266 XBKK4D XPRPVPV XYs Y266 YBKK4D YPRPVPV Y
s 
t 
t  rE   r   )
r>   typingr   cachelib.baser   cachelib.serializersr   rr   r[   r   rN   rE   r9   <module>r      s*      # 3 WI WrE   