ó 9(Zc@sÓdZddlmZmZddlZddlmZddlZddlZddl Z ddl m Z ddl m Z dd lmZmZd ZejeƒZejd eƒd e fd „ƒYƒZdS(slLocal, in-memory, LRU, cryptographic materials cache for use with caching cryptographic materials providers.iÿÿÿÿ(tdequet OrderedDictN(tRLocki(tCryptoMaterialsCacheEntry(tCryptoMaterialsCachei(t CacheKeyErrortNotSupportedErrori thashtLocalCryptoMaterialsCachecBs§eZdZejdejjejƒƒZ d„Z d„Z d„Z d„Z d„Zd„Zdd„Zd „Zd „Zd „Zd „Zd „Zd„ZRS(sÅLocal, in-memory, LRU, cache for use with caching cryptographic materials providers. .. versionadded:: 1.3.0 :param int capacity: Maximum number of entries to retain in cache at once t validatorcCsO|jdkrtdƒ‚ntƒ|_tƒ|_tƒ|_t|_ dS(s-Prepares initial values not handled by attrs.is8LocalCryptoMaterialsCache capacity cannot be less than 1N( tcapacityt ValueErrorRt _cache_lockRt_cacheRt _lre_dequetTruet_init_completed(tself((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyt__attrs_post_init__)s    cCsCt|dƒr*|dkr*tdƒ‚ntt|ƒj||ƒS(s>Disable setting of capacity after __attrs_post_init__ has run.RR sCcapacity may not be modified on LocalCryptoMaterialsCache instances(thasattrRtsuperRt __setattr__(Rtnametvalue((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyR8sc Cs®|jŸy|jjƒ}Wntk r1dSX|ƒ}|dkrKdS|j sa|jƒr”|jƒy|j|j =Wnt k rnXdS|jj |ƒWdQXdS(sXChecks the least recently evaluated entry and evicts it from the cache if it is expired.N( R Rtpopt IndexErrortNonetvalidt is_too_oldt invalidateR t cache_keytKeyErrort appendleft(Rt entry_reft actual_entry((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyt_try_to_evict_one_entry>s       cCs%xttƒD]}|jƒq WdS(sJTries to evict a set number of the least recently evaluated cache entries.N(tranget_OPPORTUNISTIC_EVICTION_ROUNDSR#(Rt_((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyt_try_to_evict_some_entriesXscCsRxKt|jƒ|jkrM|jjdtƒ\}}|jƒ|jƒqWdS(sGPrunes internal cache until internal cache is within the defined limit.tlastN(tlenR R tpopitemtFalseRR#(RR&R((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyt_prune]s cCsM|j>tj|ƒ}||j|j<|jj|ƒ|jƒWdQXdS(sµAdds a value to the cache data and control structures. :param value: Value to add to cache :type value: aws_encryption_sdk.caches.CryptoMaterialsCacheEntry N(R tweakreftrefR RRR R,(RRt reference((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyt_add_value_to_cachees  cCsStd|d|d|ƒ}|j|ƒ|j|jƒ|j|ƒWdQX|S(sSAdds encryption materials to the cache. :param bytes cache_key: Identifier for entries in cache :param encryption_materials: Encryption materials to add to cache :type encryption_materials: aws_encryption_sdk.materials_managers.EncryptionMaterials :param int plaintext_length: Length of plaintext associated with this request to the cache :param entry_hints: Metadata to associate with entry (optional) :type entry_hints: aws_encryption_sdk.caches.CryptoCacheEntryHints :rtype: aws_encryption_sdk.caches.CryptoMaterialsCacheEntry RRthintsN(Rt$_update_with_message_bytes_encryptedR R'R0(RRtencryption_materialstplaintext_lengtht entry_hintstentry((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pytput_encryption_materialsqs     cCs@td|d|ƒ}|j|jƒ|j|ƒWdQX|S(s\Adds decryption materials to the cache :param bytes cache_key: Identifier for entries in cache :param decryption_materials: Decryption materials to add to cache :type decryption_materials: aws_encryption_sdk.materials_managers.DecryptionMaterials :rtype: aws_encryption_sdk.caches.CryptoMaterialsCacheEntry RRN(RR R'R0(RRtdecryption_materialsR6((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pytput_decryption_materials‡s   c Csa|jRz?y|jƒ|j|j=Wntk rGtdƒ‚nXWd|jƒXWdQXdS(sÙRemoves a value from the cache. :param value: Value to add to cache :type value: aws_encryption_sdk.caches.CryptoMaterialsCacheEntry :raises CacheKeyError: if value not found in cache sKey not found in cacheN(R RR RRRR'(RR((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pytremove˜s   c Csn|j_y|j|}Wntk r:tdƒ‚nX|js`|j|ƒtdƒ‚n|SWdQXdS(s(Locates exactly one available cache entry for the specified cache_key. :param bytes cache_key: Cache ID for which to locate cache entries :rtype: aws_encryption_sdk.caches.CryptoMaterialsCacheEntry :raises CacheKeyError: if no values found in cache for cache_key sKey not found in cacheN(R R RRRR:(RRt cache_entry((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyt_get_single_entry¬s    cCsDtjd|ƒ|j%|j|ƒ}|j|ƒ|SWdQXdS(sòLocates exactly one available encryption materials cache entry for the specified cache_key, incrementing the entry's usage stats prior to returning it to the caller. :param bytes cache_key: Cache ID for which to locate cache entries :param int plaintext_length: Length of plaintext associated with this request to the cache :rtype: aws_encryption_sdk.caches.CryptoMaterialsCacheEntry :raises CacheKeyError: if no values found in cache for cache_key s>Looking in cache for encryption materials to encrypt %d bytes.N(t_LOGGERtdebugR R<R2(RRR4R6((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pytget_encryption_materials¿s   cCs!|j|j|ƒSWdQXdS(s=Locates exactly one available decryption materials cache entry for the specified cache_key. :param bytes cache_key: Cache ID for which to locate cache entries :rtype: aws_encryption_sdk.caches.CryptoMaterialsCacheEntry :raises CacheKeyError: if no values found in cache for cache_key N(R R<(RR((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pytget_decryption_materialsÎs cCs,|jtƒ|_tƒ|_WdQXdS(sClears the cache.N(R RR RR(R((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pytclearØs  N(t__name__t __module__t__doc__tattrtibt validatorst instance_oftsixt integer_typesR RRR#R'R,R0RR7R9R:R<R?R@RA(((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyRs!           (RDt collectionsRRtloggingt threadingRR-RERItRtbaseRt exceptionsRRR%t getLoggerRBR=tsR+R(((sK/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/caches/local.pyt s