§
    X˜¶f@!  ã                   óR   — d dl Z d dlZd dlmZ d dlmZ dZdZ G d„ de¦  «        Z	dS )é    N)Ú	BaseCache)ÚDynamoDbSerializerÚ
created_atÚresponsec                   ó:  ‡ — e Zd ZdZ e¦   «         Z	 	 	 	 	 ddej        e         de	d	ej        e         d
ej        e         dej        e         dej
        fˆ fd„Zdej
        fd„Zddedej        e         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dej
        dej        e	         dej
        fd„Zdedefd„Zdefd„Zˆ xZS )!ÚDynamoDbCachea(  
    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.

    úpython-cacheé,  Ú	cache_keyÚexpiration_timeNÚ
table_nameÚdefault_timeoutÚ	key_fieldÚexpiration_time_fieldÚ
key_prefixÚkwargsc                 óì  •— t          ¦   «                              |¦  «         	 dd l}n"# t          $ r}t	          d¦  «        |‚d }~ww xY w|| _        || _        || _        |pd| _         |j	        di |¤Ž| _
        |j        j        j        | _        	 | j
                             |¦  «        | _        | j                             ¦   «          d S # t$          $ r� | j
                             |ddœg||ddœgd	¬
¦  «        }	|	                     ¦   «           |j        di |¤Ž}
|
                     |d|dœ¬¦  «         | j
                             |¦  «        | _        | j                             ¦   «          Y d S w xY w)Nr   zno boto3 module foundÚ ÚdynamodbÚS)ÚAttributeNameÚAttributeTypeÚHASH)r   ÚKeyTypeÚPAY_PER_REQUEST)ÚAttributeDefinitionsÚ	TableNameÚ	KeySchemaÚBillingModeT)ÚEnabledr   )r   ÚTimeToLiveSpecification)r   )Ú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              €úE/var/www/piapp/venv/lib/python3.11/site-packages/cachelib/dynamodb.pyr#   zDynamoDbCache.__init__)   s×  ø€ õ 	‰Œ×Ò˜Ñ)Ô)Ð)ð	AØˆLˆLˆLˆLøÝð 	Að 	Að 	AÝÐ6Ñ7Ô7¸SÐ@øøøøð	Aøøøð &ˆÔØ#ˆŒØ&;ˆÔ#Ø$Ð*¨ˆŒØ%�u”~Ð;Ð;°FÐ;Ð;ˆŒØ”^Ô.Ô3ˆŒ
ð	Øœ,×,Ò,¨ZÑ8Ô8ˆDŒKØŒK×ÒÑÔÐÐÐøåð 	ð 	ð 	Ø”L×-Ò-à&/À#ÐFÐFð&ð %à&/¸FÐCÐCðð .ð .ñ 	ô 	ˆEð ×#Ò#Ñ%Ô%Ð%Ø!�U”\Ð7Ð7°Ð7Ð7ˆFØ×&Ò&Ø$à#Ø%:ð)ð )ð 'ñ ô ð ð œ,×,Ò,¨ZÑ8Ô8ˆDŒKØŒK×ÒÑÔÐÐÐÐð+	øøøs'   ¤) ©
A³AÁAÂ8C ÃB#E3Å2E3Úreturnc                 ó„   — t           j                              ¦   «                              t           j        j        ¬¦  «        S )z<Return a tz-aware UTC datetime representing the current time)Útzinfo)ÚdatetimeÚutcnowÚreplaceÚtimezoneÚutc)r7   s    r<   Ú_utcnowzDynamoDbCache._utcnow[   s.   € åÔ ×'Ò'Ñ)Ô)×1Ò1½Ô9JÔ9NÐ1ÑOÔOÐOó    ÚkeyÚ
attributesc                 ó¨  — i }|rD| j         |vrt          |¦  «        | j         gz   }t          d                     |¦  «        ¬¦  «        } | j        j        dd| j        |ii|¤Ž}|                     d¦  «        }|rWt          |  	                    ¦   «          
                    ¦   «         ¦  «        }|                     | j         |dz   ¦  «        |k    r|S dS )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ÚIteméd   N© )r)   ÚlistÚdictÚjoinr0   Úget_itemr(   ÚgetÚintrE   Ú	timestamp)r7   rG   rH   r   r   Ú
cache_itemÚnows          r<   Ú	_get_itemzDynamoDbCache._get_item_   sß   € ð ˆØð 	EØÔ*°*Ð<Ð<Ý! *Ñ-Ô-°Ô1LÐ0MÑM�
Ý¨s¯xªx¸
Ñ/CÔ/CÐDÑDÔDˆFà'�4”;Ô'ÐMÐM¨T¬_¸cÐ,BÐMÀfÐMÐMˆØ—\’\ &Ñ)Ô)ˆ
àð 	"Ý�d—l’l‘n”n×.Ò.Ñ0Ô0Ñ1Ô1ˆCØ�~Š~˜dÔ9¸3À¹9ÑEÔEÈÒKÐKØ!Ð!àˆtrF   c                 ó–   — |                       | j        |z   ¦  «        }|r)|t                   }| j                             |¦  «        }|S 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
        N)rY   r   ÚRESPONSE_FIELDÚ
serializerÚloads)r7   rG   rW   r   Úvalues        r<   rT   zDynamoDbCache.get}   sM   € ð —^’^ D¤O°cÑ$9Ñ:Ô:ˆ
Øð 	Ø!¥.Ô1ˆHØ”O×)Ò)¨(Ñ3Ô3ˆEØˆLØˆtrF   c                 óü   — 	 | j                              | j        | j        |z   i|                      | j        ¦  «                             ¦   «         ¬¦  «         dS # | j        j        j        j	        j
        $ r Y dS w xY w)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
        )rL   ÚConditionExpressionTF)r0   Údelete_itemr(   r   r.   Úexistsr+   Úmetar5   Ú
exceptionsÚConditionalCheckFailedException©r7   rG   s     r<   ÚdeletezDynamoDbCache.delete‹   s‹   € ð	ØŒK×#Ò#Ø”_ d¤o¸Ñ&;Ð<Ø$(§J¢J¨t¬Ñ$?Ô$?×$FÒ$FÑ$HÔ$Hð $ñ ô ð ð �4øØŒ|Ô Ô'Ô2ÔRð 	ð 	ð 	Ø�5�5ð	øøøs   ‚AA ÁA;Á:A;Tr^   ÚtimeoutÚ	overwritec                 óÒ  — |                       |¦  «        }|                      ¦   «         }i }|s‰|                      | j        ¦  «                             ¦   «         |                      | j        ¦  «                             t          |                     ¦   «         ¦  «        ¦  «        z  }t          |¬¦  «        }	 | j
                             |¦  «        }| j        |t          |                     ¦   «         t          |i}	|dk    rA|t          j        |¬¦  «        z   }
t          |
                     ¦   «         ¦  «        |	| j        <    | j        j        dd|	i|¤Ž dS # t&          $ r Y dS w xY w)	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.
        )r`   r   )ÚsecondsrM   TFNrO   )Ú_normalize_timeoutrE   r.   r(   Ú
not_existsr)   ÚlterU   rV   rQ   r\   ÚdumpsÚCREATED_AT_FIELDÚ	isoformatr[   r@   Ú	timedeltar0   Úput_itemr2   )r7   rG   r^   rh   ri   rX   r   ÚcondÚdumpÚitemr   s              r<   Ú_setzDynamoDbCache._setœ   sa  € ð& ×)Ò)¨'Ñ2Ô2ˆØ�lŠl‰nŒnˆàˆØð 	4ð —:’:˜dœoÑ.Ô.×9Ò9Ñ;Ô;¸d¿jºjØÔ+ñ?ô ?çŠc•#�c—m’m‘o”oÑ&Ô&Ñ'Ô'ñ(ˆDõ ¨dÐ3Ñ3Ô3ˆFð	Ø”?×(Ò(¨Ñ/Ô/ˆDà” Ý  #§-¢-¡/¤/Ý ðˆDð
 ˜Š{ˆ{Ø"%­Ô(:À7Ð(KÑ(KÔ(KÑ"K�Ý47¸×8QÒ8QÑ8SÔ8SÑ4TÔ4T��TÔ0Ñ1Ø ˆDŒKÔ Ð5Ð5 dÐ5¨fÐ5Ð5Ð5Ø�4øÝð 	ð 	ð 	Ø�5�5ð	øøøs   Â8BE Å
E&Å%E&c                 óD   — |                       | j        |z   ||d¬¦  «        S )NT©rh   ri   ©rw   r   ©r7   rG   r^   rh   s       r<   ÚsetzDynamoDbCache.setË   s$   € Ø�yŠy˜œ¨3Ñ.°¸wÐRVˆyÑWÔWÐWrF   c                 óD   — |                       | j        |z   ||d¬¦  «        S )NFry   rz   r{   s       r<   ÚaddzDynamoDbCache.addÎ   s$   € Ø�yŠy˜œ¨3Ñ.°¸wÐRWˆyÑXÔXÐXrF   c                 óN   — |                       | j        |z   | j        g¦  «        d uS ©N)rY   r   r)   rf   s     r<   ÚhaszDynamoDbCache.hasÑ   s-   € à�NŠN˜4œ?¨SÑ0°4Ô3NÐ2OÑPÔPØðð	
rF   c                 ó@  — | j         j        j                             d¦  «        }|                     | j        | j        ¬¦  «        }| j                             ¦   «         5 }|D ]#}|d         D ]}| 	                    |¬¦  «         ŒŒ$	 d d d ¦  «         n# 1 swxY w Y   dS )NÚscan)r   rK   ÚItems)rL   T)
r+   rc   r5   Úget_paginatorÚpaginater'   r(   r0   Úbatch_writerra   )r7   Ú	paginatorÚpagesÚbatchÚpagerv   s         r<   ÚclearzDynamoDbCache.clear×   sö   € Ø”LÔ%Ô,×:Ò:¸6ÑBÔBˆ	Ø×"Ò"ØÔ&¸T¼_ð #ñ 
ô 
ˆð Œ[×%Ò%Ñ'Ô'ð 	0¨5Øð 0ð 0�Ø  œMð 0ð 0�DØ×%Ò%¨$Ð%Ñ/Ô/Ð/Ð/ð0ð0ð	0ð 	0ð 	0ñ 	0ô 	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	0øøøð 	0ð 	0ð 	0ð 	0ð
 ˆts   Á'BÂBÂB)r	   r
   r   r   Nr€   )NT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r\   Ú_tÚOptionalÚstrrU   ÚAnyr#   rE   rP   rY   rT   Úboolrg   rw   r|   r~   r�   rŒ   Ú__classcell__)r;   s   @r<   r   r      sœ  ø€ € € € € ðð ð6 $Ð#Ñ%Ô%€Jð (6Ø"Ø&1Ø2CØ'+ð0ð 0à”K Ô$ð0ð ð0ð ”;˜sÔ#ð	0ð
  "œ{¨3Ô/ð0ð ”K Ô$ð0ð ”&ð0ð 0ð 0ð 0ð 0ð 0ðdP˜œð Pð Pð Pð Pðð ˜Sð ¨b¬k¸$Ô.?ð È2Ì6ð ð ð ð ð<�sð ˜rœvð ð ð ð ð˜#ð  $ð ð ð ð ð* %)Ø'+ð-ð -àð-ð Œvð-ð ”˜SÔ!ð	-ð
 ”;˜tÔ$ð-ð 
Œð-ð -ð -ð -ð^Xð X�sð X 2¤6ð X°B´KÀÔ4Dð XÐPRÔPVð Xð Xð Xð XðYð Y�sð Y 2¤6ð Y°B´KÀÔ4Dð YÐPRÔPVð Yð Yð Yð Yð
�sð 
˜tð 
ð 
ð 
ð 
ð�tð ð ð ð ð ð ð ð rF   r   )
r@   Útypingr‘   Úcachelib.baser   Úcachelib.serializersr   rp   r[   r   rO   rF   r<   ú<module>rš      s‡   ðØ €€€Ø Ð Ð Ð à #Ð #Ð #Ð #Ð #Ð #Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3àÐ Ø€ðWð Wð Wð Wð W�Iñ Wô Wð Wð Wð WrF   