aws_smithy_runtime/client/identity/
cache.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use aws_smithy_runtime_api::client::identity::{
7    IdentityFuture, ResolveCachedIdentity, ResolveIdentity, SharedIdentityCache,
8    SharedIdentityResolver,
9};
10use aws_smithy_runtime_api::shared::IntoShared;
11use aws_smithy_types::config_bag::ConfigBag;
12
13mod lazy;
14use aws_smithy_runtime_api::client::runtime_components::RuntimeComponents;
15pub use lazy::LazyCacheBuilder;
16
17/// Identity cache configuration.
18///
19/// # Examples
20///
21/// Disabling identity caching:
22/// ```no_run
23/// use aws_smithy_runtime::client::identity::IdentityCache;
24///
25/// # /*
26/// let config = some_service::Config::builder()
27///     .identity_cache(
28/// # */
29/// # drop(
30///         IdentityCache::no_cache()
31/// # );
32/// # /*
33///     )
34///     // ...
35///     .build();
36/// let client = some_service::Client::new(config);
37/// # */
38/// ```
39///
40/// Customizing lazy caching:
41/// ```no_run
42/// use aws_smithy_runtime::client::identity::IdentityCache;
43/// use std::time::Duration;
44///
45/// # /*
46/// let config = some_service::Config::builder()
47///     .identity_cache(
48/// # */
49/// # drop(
50///         IdentityCache::lazy()
51///             // change the load timeout to 10 seconds
52///             .load_timeout(Duration::from_secs(10))
53///             .build()
54/// # );
55/// # /*
56///     )
57///     // ...
58///     .build();
59/// let client = some_service::Client::new(config);
60/// # */
61/// ```
62#[non_exhaustive]
63pub struct IdentityCache;
64
65impl IdentityCache {
66    /// Create an identity cache that does not cache any resolved identities.
67    pub fn no_cache() -> SharedIdentityCache {
68        NoCache.into_shared()
69    }
70
71    /// Configure a lazy identity cache.
72    ///
73    /// Identities are lazy loaded and then cached when a request is made.
74    pub fn lazy() -> LazyCacheBuilder {
75        LazyCacheBuilder::new()
76    }
77}
78
79#[derive(Clone, Debug)]
80struct NoCache;
81
82impl ResolveCachedIdentity for NoCache {
83    fn resolve_cached_identity<'a>(
84        &'a self,
85        resolver: SharedIdentityResolver,
86        runtime_components: &'a RuntimeComponents,
87        config_bag: &'a ConfigBag,
88    ) -> IdentityFuture<'a> {
89        IdentityFuture::new(async move {
90            resolver
91                .resolve_identity(runtime_components, config_bag)
92                .await
93        })
94    }
95}