

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Python 和客户端缓存获取 Secrets Manager 密钥值
<a name="retrieving-secrets_cache-python"></a>

在检索密钥时，您可以使用 Secrets Manager 基于 Python 的缓存组件来缓存密钥，以备将来使用。检索已缓存密钥比从 Secrets Manager 中检索密钥的速度要快。由于调用 Secrets Manager 需要付费 APIs，因此使用缓存可以降低成本。有关检索密钥的所有方法，请参阅 [获取密钥](retrieving-secrets.md)。

缓存策略为“最近最少使用 (LRU)”，因此当缓存必须丢弃某个密钥时，它会丢弃最近使用最少的密钥。原定设置下，缓存会每小时刷新一次秘密。您可以配置在缓存中[刷新密钥的频率](retrieving-secrets_cache-ref-secretcacheconfig.md)，也可以[挂钩到密钥检索中](retrieving-secrets_cache-ref-secretcachehook.md)以添加更多功能。

一旦释放缓存引用，缓存便不会进行强制垃圾回收。缓存实施不包括缓存失效。缓存实现侧重于缓存本身，而不是侧重加强安全性或以安全性为重点。如果您需要额外的安全性（例如加密缓存中的项目），请使用提供的接口和抽象方法。

要使用该组件，您必须满足以下条件：
+ Python 3.6 或更高版本。
+ botocore 1.12 或更高版本。请参阅[适用于 Python](https://www.amazonaws.cn/sdk-for-python/) 和 [Botocore 的Amazon SDK](https://botocore.amazonaws.com/v1/documentation/api/latest/index.html)。
+ setuptools\$1scm 3.2 或更高版本。见 [https://pypi。 org/project/setuptools-scm/](https://pypi.org/project/setuptools-scm/)。

要下载源代码，请参阅上的 S [ecrets Manager 基于 Python 的缓存客户端组件](https://github.com/aws/aws-secretsmanager-caching-python )。 GitHub

要安装组件，请使用以下命令。

```
$ pip install aws-secretsmanager-caching
```

**所需权限：**
+ `secretsmanager:DescribeSecret`
+ `secretsmanager:GetSecretValue`

有关更多信息，请参阅 [权限参考](auth-and-access.md#reference_iam-permissions)。

**Topics**
+ [SecretCache](retrieving-secrets_cache-ref-secretcache.md)
+ [SecretCacheConfig](retrieving-secrets_cache-ref-secretcacheconfig.md)
+ [SecretCacheHook](retrieving-secrets_cache-ref-secretcachehook.md)
+ [@InjectSecretString](retrieving-secrets_cache-decor-string.md)
+ [@InjectKeywordedSecretString](retrieving-secrets_cache-decor-keyword.md)

**Example 检索密钥**  
以下示例说明如何获取名为的密钥的机密值*mysecret*。  

```
import botocore 
import botocore.session 
from aws_secretsmanager_caching import SecretCache, SecretCacheConfig 

client = botocore.session.get_session().create_client('secretsmanager')
cache_config = SecretCacheConfig()
cache = SecretCache( config = cache_config, client = client)

secret = cache.get_secret_string('mysecret')
```