databeam/cache/
mod.rs

1#![allow(async_fn_in_trait)]
2use serde::{de::DeserializeOwned, Serialize};
3
4pub const EXPIRE_AT: i64 = 3_600_000;
5
6#[allow(type_alias_bounds)]
7pub type TimedObject<T: Serialize + DeserializeOwned> = (i64, T);
8
9#[cfg(feature = "redis")]
10pub mod redis;
11
12#[cfg(feature = "moka")]
13pub mod moka;
14
15#[cfg(feature = "oysters")]
16pub mod oysters;
17
18/// A simple cache "database".
19pub trait Cache {
20    type Item;
21    type Client;
22
23    /// Create a new [`Cache`].
24    async fn new() -> Self;
25    /// Get a connection to the cache.
26    async fn get_con(&self) -> Self::Client;
27
28    /// Get a cache object by its identifier
29    ///
30    /// # Arguments
31    /// * `id` - `String` of the object's id
32    async fn get(&self, id: Self::Item) -> Option<String>;
33    /// Set a cache object by its identifier and content
34    ///
35    /// # Arguments
36    /// * `id` - `String` of the object's id
37    /// * `content` - `String` of the object's content
38    async fn set(&self, id: Self::Item, content: Self::Item) -> bool;
39    /// Update a cache object by its identifier and content
40    ///
41    /// # Arguments
42    /// * `id` - `String` of the object's id
43    /// * `content` - `String` of the object's content
44    async fn update(&self, id: Self::Item, content: Self::Item) -> bool;
45    /// Remove a cache object by its identifier
46    ///
47    /// # Arguments
48    /// * `id` - `String` of the object's id
49    async fn remove(&self, id: Self::Item) -> bool;
50    /// Remove a cache object by its identifier('s start)
51    ///
52    /// # Arguments
53    /// * `id` - `String` of the object's id('s start)
54    async fn remove_starting_with(&self, id: Self::Item) -> bool;
55    /// Increment a cache object by its identifier
56    ///
57    /// # Arguments
58    /// * `id` - `String` of the object's id
59    async fn incr(&self, id: Self::Item) -> bool;
60    /// Decrement a cache object by its identifier
61    ///
62    /// # Arguments
63    /// * `id` - `String` of the object's id
64    async fn decr(&self, id: Self::Item) -> bool;
65
66    /// Get a cache object by its identifier
67    ///
68    /// # Arguments
69    /// * `id` - `String` of the object's id
70    async fn get_timed<T: Serialize + DeserializeOwned>(
71        &self,
72        id: Self::Item,
73    ) -> Option<TimedObject<T>>;
74    /// Set a cache object by its identifier and content
75    ///
76    /// # Arguments
77    /// * `id` - `String` of the object's id
78    /// * `content` - `String` of the object's content
79    async fn set_timed<T: Serialize + DeserializeOwned>(&self, id: Self::Item, content: T) -> bool;
80}