

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

# type Cache
<a name="retrieving-secrets_cache-go_cache"></a>

适用于从 Secrets Manager 请求的密钥的内存中缓存。您使用 [GetSecretString](#retrieving-secrets_cache-go_cache_operations_GetCachedSecret) 或 [GetSecretBinary](#retrieving-secrets_cache-go_cache_operations_GetSecretBinary) 从缓存中检索密钥。

下面的示例演示了如何配置缓存设置。

```
// Create a custom secretsmanager client
client := getCustomClient()

// Create a custom CacheConfig struct 
config := secretcache. CacheConfig{
    MaxCacheSize:  secretcache.DefaultMaxCacheSize + 10,
    VersionStage:  secretcache.DefaultVersionStage,
    CacheItemTTL:  secretcache.DefaultCacheItemTTL,
}
	
// Instantiate the cache 
cache, _ := secretcache.New(
    func( c *secretcache.Cache) {  c. CacheConfig = config },
    func( c *secretcache.Cache) {  c. Client = client },
)
```

有关包括示例在内的更多信息，请参阅 [使用 Go 和客户端缓存获取 Secrets Manager 密钥值](retrieving-secrets_cache-go.md)。

## 方法
<a name="retrieving-secrets_cache-go_cache_operations"></a>

### New
<a name="retrieving-secrets_cache-go_cache_operations_New"></a>

`func New(optFns ...func(*Cache)) (*Cache, error)`

New 使用功能选项构造密钥缓存，否则将使用默认值。从新会话初始化 SecretsManager 客户端。初始化 CacheConfig 为默认值。使用默认最大大小初始化 LRU 缓存。

### GetSecretString
<a name="retrieving-secrets_cache-go_cache_operations_GetCachedSecret"></a>

`func (c *Cache) GetSecretString(secretId string) (string, error)`

GetSecretString 从缓存中获取给定密钥 ID 的秘密字符串值。返回密钥字符串，如果操作失败则返回错误。

### GetSecretStringWithStage
<a name="retrieving-secrets_cache-go_cache_operations_GetSecretStringWithStage"></a>

`func (c *Cache) GetSecretStringWithStage(secretId string, versionStage string) (string, error)`

GetSecretStringWithStage 从缓存中获取给定密钥 ID 和[版本阶段](whats-in-a-secret.md#term_version)的秘密字符串值。返回密钥字符串，如果操作失败则返回错误。

### GetSecretBinary
<a name="retrieving-secrets_cache-go_cache_operations_GetSecretBinary"></a>

`func (c *Cache) GetSecretBinary(secretId string) ([]byte, error) {`

GetSecretBinary 从缓存中获取给定密钥 ID 的秘密二进制值。返回密钥二进制值，如果操作失败则返回错误。

### GetSecretBinaryWithStage
<a name="retrieving-secrets_cache-go_cache_operations_GetSecretBinaryWithStage"></a>

`func (c *Cache) GetSecretBinaryWithStage(secretId string, versionStage string) ([]byte, error)`

GetSecretBinaryWithStage 从缓存中获取给定密钥 ID 和[版本阶段](whats-in-a-secret.md#term_version)的秘密二进制值。返回密钥二进制值，如果操作失败则返回错误。