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
use super::{Cache, CacheRecord, CacheRepo, CacheRepoError, IntoCache};

/// A connection to a cache repo.
#[derive(Clone, Default)]
pub struct CacheConnection<Repo> {
    /// The underlying repository.
    pub repo: Repo,
}

impl<Repo> CacheConnection<Repo>
where
    Repo: CacheRepo,
{
    /// Create a new connection with a [`CacheRepo`]
    pub fn new(repo: Repo) -> Self {
        Self { repo }
    }

    /// View a cache record by URI
    pub async fn view(
        &self,
        uri: impl AsRef<str> + Send,
    ) -> Result<Cache<CacheRecord>, CacheRepoError> {
        self.repo.get_memo(uri).await
    }

    /// Insert a cache record
    pub async fn insert<CacheCandidate>(
        &self,
        record: CacheCandidate,
    ) -> Result<CacheRecord, CacheRepoError>
    where
        CacheCandidate: IntoCache + Send,
    {
        self.repo.set_memo(record).await
    }
}