aws_smithy_runtime/client/identity/
cache.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use aws_smithy_runtime_api::client::identity::{
    IdentityFuture, ResolveCachedIdentity, ResolveIdentity, SharedIdentityCache,
    SharedIdentityResolver,
};
use aws_smithy_runtime_api::shared::IntoShared;
use aws_smithy_types::config_bag::ConfigBag;

mod lazy;
use aws_smithy_runtime_api::client::runtime_components::RuntimeComponents;
pub use lazy::LazyCacheBuilder;

/// Identity cache configuration.
///
/// # Examples
///
/// Disabling identity caching:
/// ```no_run
/// use aws_smithy_runtime::client::identity::IdentityCache;
///
/// # /*
/// let config = some_service::Config::builder()
///     .identity_cache(
/// # */
/// # drop(
///         IdentityCache::no_cache()
/// # );
/// # /*
///     )
///     // ...
///     .build();
/// let client = some_service::Client::new(config);
/// # */
/// ```
///
/// Customizing lazy caching:
/// ```no_run
/// use aws_smithy_runtime::client::identity::IdentityCache;
/// use std::time::Duration;
///
/// # /*
/// let config = some_service::Config::builder()
///     .identity_cache(
/// # */
/// # drop(
///         IdentityCache::lazy()
///             // change the load timeout to 10 seconds
///             .load_timeout(Duration::from_secs(10))
///             .build()
/// # );
/// # /*
///     )
///     // ...
///     .build();
/// let client = some_service::Client::new(config);
/// # */
/// ```
#[non_exhaustive]
pub struct IdentityCache;

impl IdentityCache {
    /// Create an identity cache that does not cache any resolved identities.
    pub fn no_cache() -> SharedIdentityCache {
        NoCache.into_shared()
    }

    /// Configure a lazy identity cache.
    ///
    /// Identities are lazy loaded and then cached when a request is made.
    pub fn lazy() -> LazyCacheBuilder {
        LazyCacheBuilder::new()
    }
}

#[derive(Clone, Debug)]
struct NoCache;

impl ResolveCachedIdentity for NoCache {
    fn resolve_cached_identity<'a>(
        &'a self,
        resolver: SharedIdentityResolver,
        runtime_components: &'a RuntimeComponents,
        config_bag: &'a ConfigBag,
    ) -> IdentityFuture<'a> {
        IdentityFuture::new(async move {
            resolver
                .resolve_identity(runtime_components, config_bag)
                .await
        })
    }
}