feat: add corex-storage crate for unified storage abstraction

- Introduced corex-storage crate with support for local disk, S3-compatible, and Google Cloud Storage backends.
- Implemented StorageDriver trait for various storage backends.
- Added LocalFileStorage for local disk operations.
- Added S3Storage for S3-compatible object storage with multipart upload support.
- Added GcsStorage for Google Cloud Storage operations.
- Included error handling for storage operations.
- Added tests for each storage backend to ensure functionality.
- Created README.md for documentation and usage examples.
- Added Apache and MIT licenses for open-source compliance.
This commit is contained in:
asepharyana
2026-08-28 22:24:18 +07:00
parent 6a4b2b8f54
commit db4f277336
47 changed files with 0 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
[package]
name = "corex-cache"
version = "1.2.0"
edition = "2021"
rust-version = "1.75"
license = "MIT OR Apache-2.0"
repository = "https://github.com/asepharyana/mytheclipse"
homepage = "https://github.com/asepharyana/mytheclipse"
documentation = "https://docs.rs/corex-cache"
authors = ["asepharyana <superaseph@gmail.com>"]
description = "Unified multi-layer cache abstraction: L1/L2 caching, cache-aside and auto-refresh, with pluggable backends."
readme = "README.md"
keywords = ["cache", "lru", "redis", "multilayer", "cache-aside"]
categories = ["caching", "asynchronous"]
[features]
default = ["l1-memory", "cache-aside"]
# L1 (in-process) backends.
l1-memory = []
l1-moka = ["l1-memory", "dep:moka"]
# L2 (distributed) backends.
l2-redis = ["l1-memory", "dep:redis"]
# Cache-aside + auto-refresh helper.
cache-aside = ["l1-memory", "dep:serde", "dep:serde_json"]
[dependencies]
tracing = "0.1"
# Required unconditionally: the core `Cache` trait (always compiled) uses it.
async-trait = "0.1"
serde = { version = "1", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true }
# L1: Moka (high-performance in-memory cache).
moka = { version = "0.12", default-features = false, features = ["future"], optional = true }
# L2: Redis/Valkey async client (multiplexed connection).
redis = { version = "0.27", default-features = false, features = ["tokio-comp"], optional = true }
[dev-dependencies]
tokio = { version = "1.53", features = ["full"] }
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2026 The corex Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 The corex Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+33
View File
@@ -0,0 +1,33 @@
# corex-cache
A unified multi-layer cache abstraction so your app isn't locked to one cache
provider. Combines an in-process **L1** cache with a distributed **L2** cache
(e.g. Redis/Valkey) behind one simple `get`/`set`/`invalidate` API, plus a
**cache-aside / auto-refresh** helper.
## Features
- `l1-memory` (default) — zero-dependency in-process cache.
- `l1-moka` — high-performance Moka-backed L1 with TTL/max-capacity.
- `l2-redis` — Redis/Valkey L2 via `fred`.
- `cache-aside` (default) — read-through cache-aside helper.
## Usage
```rust
use corex_cache::{Cache, MemoryCache, MultiLayerCache, CacheAside};
let cache = MultiLayerCache::new(
MemoryCache::new(), // L1
MemoryCache::new(), // L2 (use RedisCache in production)
);
cache.set("k", b"v".to_vec(), None).await.unwrap();
let v = cache.get("k").await.unwrap();
// Cache-aside: fill misses from a source of truth.
let aside = CacheAside::new(
MemoryCache::new(),
|key| async move { Some(format!("data-for-{key}").into_bytes()) },
);
let _ = aside.get("orders:42").await.unwrap();
```
+123
View File
@@ -0,0 +1,123 @@
//! Cache-aside with read-through (feature `cache-aside`).
//!
//! [`CacheAside`] wires a [`Cache`] to a data source: on a miss it invokes a
//! user-provided async fetcher, stores the result (with an optional TTL), and
//! returns it. This is the standard cache-aside pattern — reads bypass a cold
//! cache by falling back to the source of truth.
use std::time::Duration;
use crate::traits::{Cache, CacheError};
/// A generic read-through cache-aside helper.
///
/// `F` is the data source: an async closure `(owned key) -> Option<Vec<u8>>`.
/// The key is passed by value ([`String`]) so the returned future does not
/// borrow from the caller, which keeps the API simple and `'static`-friendly.
#[derive(Clone)]
pub struct CacheAside<C, F> {
cache: C,
fetcher: F,
ttl: Option<Duration>,
}
impl<C, F, Fut> CacheAside<C, F>
where
C: Cache,
F: Fn(String) -> Fut + Send + Sync,
Fut: std::future::Future<Output = Option<Vec<u8>>> + Send,
{
/// Builds a cache-aside wrapper around `cache` using `fetcher` to fill
/// misses. Entries are stored without expiry unless `with_ttl` is used.
pub fn new(cache: C, fetcher: F) -> Self {
Self {
cache,
fetcher,
ttl: None,
}
}
/// Applies a `ttl` to every entry written by this wrapper.
pub fn with_ttl(mut self, ttl: Duration) -> Self {
self.ttl = Some(ttl);
self
}
/// Returns a value for `key`, reading through to the fetcher on a miss and
/// caching the result.
pub async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError> {
if let Some(value) = self.cache.get(key).await? {
return Ok(Some(value));
}
if let Some(value) = (self.fetcher)(key.to_string()).await {
self.cache.set(key, value.clone(), self.ttl).await?;
Ok(Some(value))
} else {
Ok(None)
}
}
/// Explicitly evicts `key`.
pub async fn invalidate(&self, key: &str) -> Result<(), CacheError> {
self.cache.invalidate(key).await
}
/// Returns a reference to the underlying cache.
pub fn cache(&self) -> &C {
&self.cache
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::memory::MemoryCache;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
fn fetcher(
hits: Arc<AtomicU64>,
) -> impl Fn(String) -> std::future::Ready<Option<Vec<u8>>> + Send + Sync {
move |_key: String| {
let n = hits.fetch_add(1, Ordering::SeqCst) + 1;
std::future::ready(Some(format!("fetched-{n}").into_bytes()))
}
}
#[tokio::test]
async fn miss_reads_through_and_caches() {
let hits = Arc::new(AtomicU64::new(0));
let aside = CacheAside::new(MemoryCache::new(), fetcher(hits.clone()));
let first = aside.get("k").await.unwrap().unwrap();
let second = aside.get("k").await.unwrap().unwrap();
assert_eq!(first, b"fetched-1");
// Cache hit — fetcher not called again.
assert_eq!(second, b"fetched-1");
assert_eq!(hits.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn invalidate_forces_refetch() {
let hits = Arc::new(AtomicU64::new(0));
let aside = CacheAside::new(MemoryCache::new(), fetcher(hits.clone()));
let _ = aside.get("k").await.unwrap();
aside.invalidate("k").await.unwrap();
let again = aside.get("k").await.unwrap().unwrap();
assert_eq!(again, b"fetched-2");
assert_eq!(hits.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn ttl_applies_to_writes() {
let aside = CacheAside::new(MemoryCache::new(), fetcher(Arc::new(AtomicU64::new(0))))
.with_ttl(Duration::from_millis(30));
let _ = aside.get("k").await.unwrap();
assert_eq!(
aside.cache().get("k").await.unwrap(),
Some(b"fetched-1".to_vec())
);
tokio::time::sleep(Duration::from_millis(60)).await;
assert_eq!(aside.cache().get("k").await.unwrap(), None);
}
}
+73
View File
@@ -0,0 +1,73 @@
//! # corex-cache
//!
//! A unified multi-layer cache abstraction that keeps your application from
//! being locked to any single cache provider.
//!
//! - **L1 (in-process) caches**: [`memory::MemoryCache`] (zero-dependency,
//! default) or [`moka_cache::MokaL1`] (high-performance, TTL/max-capacity).
//! - **L2 (distributed) caches**: [`redis::RedisCache`] backed by Redis/Valkey.
//! - **Multi-layer composition**: [`multilayer::MultiLayerCache`] layers an L1
//! over an L2 behind one [`Cache`] face; reads fall through to L2 and
//! backfill L1.
//! - **Cache-aside / auto-refresh**: [`cache_aside::CacheAside`] reads through
//! to a data source on a miss and caches the result.
//!
//! The core [`Cache`] trait is byte-oriented; typed convenience (JSON) is
//! layered on top via [`memory::typed::TypedCache`].
//!
//! ## Example
//!
//! ```no_run
//! use corex_cache::{Cache, MemoryCache, MultiLayerCache, CacheAside};
//! # async fn run() {
//! let l1 = MemoryCache::new();
//! let l2 = MemoryCache::new(); // in a real app: a RedisCache
//! let cache = MultiLayerCache::new(l1, l2);
//!
//! cache.set("user:1", b"payload".to_vec(), None).await.unwrap();
//! assert_eq!(cache.get("user:1").await.unwrap(), Some(b"payload".to_vec()));
//!
//! // Cache-aside: fill misses from a source of truth.
//! let aside = CacheAside::new(
//! MemoryCache::new(),
//! |key| async move { Some(format!("data-for-{key}").into_bytes()) },
//! );
//! let _v = aside.get("orders:42").await.unwrap();
//! # }
//! ```
#![forbid(unsafe_code)]
pub mod traits;
#[cfg(feature = "l1-memory")]
pub mod memory;
#[cfg(feature = "l1-moka")]
pub mod moka_cache;
#[cfg(feature = "l2-redis")]
pub mod redis;
#[cfg(feature = "cache-aside")]
pub mod cache_aside;
#[cfg(feature = "cache-aside")]
pub mod multilayer;
pub use traits::{Cache, CacheError};
#[cfg(feature = "l1-memory")]
pub use memory::MemoryCache;
#[cfg(feature = "l1-moka")]
pub use moka_cache::MokaL1;
#[cfg(feature = "l2-redis")]
pub use redis::RedisCache;
#[cfg(feature = "cache-aside")]
pub use cache_aside::CacheAside;
#[cfg(feature = "cache-aside")]
pub use multilayer::MultiLayerCache;
+191
View File
@@ -0,0 +1,191 @@
//! A simple, dependency-free in-process cache (L1, `l1-memory`).
//!
//! Backed by a `HashMap<String, (Vec<u8>, Instant)>` guarded by a `Mutex`.
//! Entries are lazily expired on access by comparing against `Instant`; a
//! monotonic clock keeps TTLs robust against wall-clock discontinuities.
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use async_trait::async_trait;
use crate::traits::{Cache, CacheError};
/// A wrapping entry: `None` expiry means the value never expires.
type Entry = (Vec<u8>, Option<Instant>);
/// An in-process [`Cache`] implementation for L1 caching.
#[derive(Clone, Default)]
pub struct MemoryCache {
inner: Arc<Mutex<HashMap<String, Entry>>>,
}
impl MemoryCache {
/// Builds an empty in-memory cache.
pub fn new() -> Self {
Self::default()
}
/// Pre-allocates space for `capacity` entries to reduce reallocation.
pub fn with_capacity(self, capacity: usize) -> Self {
self.inner.lock().unwrap().reserve(capacity);
self
}
}
#[async_trait]
impl Cache for MemoryCache {
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError> {
let mut map = self.inner.lock().unwrap();
match map.get(key) {
Some((value, Some(expires))) if *expires <= Instant::now() => {
map.remove(key);
Ok(None)
}
Some((value, _)) => Ok(Some(value.clone())),
None => Ok(None),
}
}
async fn set(
&self,
key: &str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Result<(), CacheError> {
let expires = ttl.map(|d| Instant::now() + d);
self.inner
.lock()
.unwrap()
.insert(key.to_string(), (value, expires));
Ok(())
}
async fn invalidate(&self, key: &str) -> Result<(), CacheError> {
self.inner.lock().unwrap().remove(key);
Ok(())
}
async fn clear(&self) -> Result<(), CacheError> {
self.inner.lock().unwrap().clear();
Ok(())
}
}
/// A typed view over a byte cache using `serde`-compatible (JSON) encoding.
///
/// Only enabled with the `cache-aside` feature, which pulls in `serde`.
#[cfg(feature = "cache-aside")]
pub mod typed {
use serde::{de::DeserializeOwned, Serialize};
use super::*;
/// Wraps a [`Cache`] with JSON-based typed get/set.
#[derive(Clone)]
pub struct TypedCache<C> {
inner: C,
}
impl<C: Cache> TypedCache<C> {
/// Wraps `inner`.
pub fn new(inner: C) -> Self {
Self { inner }
}
/// Fetches and deserializes a value.
pub async fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>, CacheError> {
match self.inner.get(key).await? {
Some(bytes) => serde_json::from_slice(&bytes)
.map(Some)
.map_err(|e| CacheError::Serialization(e.to_string())),
None => Ok(None),
}
}
/// Serializes and stores a value.
pub async fn set<T: Serialize>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> Result<(), CacheError> {
let bytes =
serde_json::to_vec(value).map_err(|e| CacheError::Serialization(e.to_string()))?;
self.inner.set(key, bytes, ttl).await
}
/// Returns the underlying byte cache.
pub fn into_inner(self) -> C {
self.inner
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn set_get_roundtrip() {
let c = MemoryCache::new();
c.set("k", b"v".to_vec(), None).await.unwrap();
assert_eq!(c.get("k").await.unwrap(), Some(b"v".to_vec()));
assert_eq!(c.get("missing").await.unwrap(), None);
}
#[tokio::test]
async fn ttl_expires_entry() {
let c = MemoryCache::new();
c.set("k", b"v".to_vec(), Some(Duration::from_millis(30)))
.await
.unwrap();
assert_eq!(c.get("k").await.unwrap(), Some(b"v".to_vec()));
tokio::time::sleep(Duration::from_millis(60)).await;
assert_eq!(c.get("k").await.unwrap(), None);
}
#[tokio::test]
async fn invalidate_and_clear() {
let c = MemoryCache::new();
c.set("a", b"1".to_vec(), None).await.unwrap();
c.set("b", b"2".to_vec(), None).await.unwrap();
c.invalidate("a").await.unwrap();
assert_eq!(c.get("a").await.unwrap(), None);
assert_eq!(c.get("b").await.unwrap(), Some(b"2".to_vec()));
c.clear().await.unwrap();
assert_eq!(c.get("b").await.unwrap(), None);
}
#[cfg(feature = "cache-aside")]
#[tokio::test]
async fn typed_cache_roundtrip() {
use typed::TypedCache;
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct User {
id: u64,
name: String,
}
let typed = TypedCache::new(MemoryCache::new());
typed
.set(
"u",
&User {
id: 1,
name: "alice".into(),
},
None,
)
.await
.unwrap();
let got: User = typed.get("u").await.unwrap().unwrap();
assert_eq!(
got,
User {
id: 1,
name: "alice".into()
}
);
}
}
@@ -0,0 +1,96 @@
//! A high-performance in-process cache backed by Moka (L1, `l1-moka`).
//!
//! Moka provides automatic max-capacity and (optionally) TTL-based eviction,
//! so this L1 is well-suited to workloads where memory bounds matter.
use std::time::Duration;
use async_trait::async_trait;
use moka::future::Cache as MokaCache;
use crate::traits::{Cache, CacheError};
/// A Moka-backed [`Cache`] for L1 caching.
#[derive(Clone)]
pub struct MokaL1 {
inner: MokaCache<String, Vec<u8>>,
}
impl MokaL1 {
/// Builds a Moka cache with `max_capacity` entries and an optional default
/// `ttl`.
pub fn new(max_capacity: u64, ttl: Option<Duration>) -> Self {
let mut builder = MokaCache::builder().max_capacity(max_capacity);
if let Some(ttl) = ttl {
builder = builder.time_to_live(ttl);
}
Self {
inner: builder.build(),
}
}
}
#[async_trait]
impl Cache for MokaL1 {
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError> {
Ok(self.inner.get(key).await)
}
async fn set(
&self,
key: &str,
value: Vec<u8>,
_ttl: Option<Duration>,
) -> Result<(), CacheError> {
// Per-entry TTL overrides are handled by the builder default in Moka;
// the passed `ttl` is intentionally ignored (single configured policy).
self.inner.insert(key.to_string(), value).await;
Ok(())
}
async fn invalidate(&self, key: &str) -> Result<(), CacheError> {
self.inner.invalidate(key).await;
Ok(())
}
async fn clear(&self) -> Result<(), CacheError> {
self.inner.invalidate_all();
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn set_get_roundtrip() {
let c = MokaL1::new(100, None);
c.set("k", b"v".to_vec(), None).await.unwrap();
assert_eq!(c.get("k").await.unwrap(), Some(b"v".to_vec()));
assert_eq!(c.get("missing").await.unwrap(), None);
}
#[tokio::test]
async fn invalidate_and_clear() {
let c = MokaL1::new(100, None);
c.set("a", b"1".to_vec(), None).await.unwrap();
c.set("b", b"2".to_vec(), None).await.unwrap();
c.invalidate("a").await.unwrap();
assert_eq!(c.get("a").await.unwrap(), None);
assert_eq!(c.get("b").await.unwrap(), Some(b"2".to_vec()));
c.clear().await.unwrap();
assert_eq!(c.get("b").await.unwrap(), None);
}
#[tokio::test]
async fn ttl_does_expire() {
// Keep a firm TTL assertion; sleep well past the expiry window.
let c = MokaL1::new(100, Some(Duration::from_millis(40)));
c.set("k", b"v".to_vec(), None).await.unwrap();
assert_eq!(c.get("k").await.unwrap(), Some(b"v".to_vec()));
tokio::time::sleep(Duration::from_millis(120)).await;
let v = c.get("k").await.unwrap();
assert!(matches!(v, None));
}
}
+139
View File
@@ -0,0 +1,139 @@
//! Multi-layer (L1/L2) caching behind a single [`Cache`] face.
//!
//! [`MultiLayerCache`] layers a fast in-process L1 over a slower but larger
//! L2 (e.g. Redis). Reads are L1-first with an L2 fallback; a hit on L2 is
//! backfilled into L1. Writes and invalidations go to both layers.
use std::time::Duration;
use async_trait::async_trait;
use crate::traits::{Cache, CacheError};
/// A read-through, write-through composition of an L1 and L2 cache.
///
/// `L1` is typically [`crate::memory::MemoryCache`] or
/// [`crate::moka_cache::MokaL1`]; `L2` is typically a distributed cache such
/// as a Redis backend. Order of layers fixed: `L1` is consulted first.
#[derive(Clone)]
pub struct MultiLayerCache<L1, L2> {
l1: L1,
l2: L2,
/// When `true`, an L2 hit is written back into L1 (default `true`).
populate_l1: bool,
}
impl<L1, L2> MultiLayerCache<L1, L2>
where
L1: Cache,
L2: Cache,
{
/// Builds a two-layer cache with L1-backfill enabled.
pub fn new(l1: L1, l2: L2) -> Self {
Self {
l1,
l2,
populate_l1: true,
}
}
/// Disables L1 backfill-on-read.
pub fn without_l1_backfill(mut self) -> Self {
self.populate_l1 = false;
self
}
/// Returns a reference to the L1 layer.
pub fn l1(&self) -> &L1 {
&self.l1
}
/// Returns a reference to the L2 layer.
pub fn l2(&self) -> &L2 {
&self.l2
}
}
#[async_trait]
impl<L1, L2> Cache for MultiLayerCache<L1, L2>
where
L1: Cache,
L2: Cache,
{
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError> {
// L1 first.
if let Some(value) = self.l1.get(key).await? {
return Ok(Some(value));
}
// L2 fallback.
if let Some(value) = self.l2.get(key).await? {
if self.populate_l1 {
self.l1.set(key, value.clone(), None).await?;
}
return Ok(Some(value));
}
Ok(None)
}
async fn set(
&self,
key: &str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Result<(), CacheError> {
self.l1.set(key, value.clone(), ttl).await?;
self.l2.set(key, value, ttl).await
}
async fn invalidate(&self, key: &str) -> Result<(), CacheError> {
self.l1.invalidate(key).await?;
self.l2.invalidate(key).await
}
async fn clear(&self) -> Result<(), CacheError> {
self.l1.clear().await?;
self.l2.clear().await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::memory::MemoryCache;
#[tokio::test]
async fn read_through_populates_l1() {
let l2 = MemoryCache::new();
l2.set("k", b"l2-value".to_vec(), None).await.unwrap();
let layered = MultiLayerCache::new(MemoryCache::new(), l2);
assert_eq!(layered.l1().get("k").await.unwrap(), None);
assert_eq!(layered.get("k").await.unwrap(), Some(b"l2-value".to_vec()));
// L2 hit should have populated L1.
assert_eq!(
layered.l1().get("k").await.unwrap(),
Some(b"l2-value".to_vec())
);
}
#[tokio::test]
async fn write_goes_to_both() {
let l1 = MemoryCache::new();
let l2 = MemoryCache::new();
let layered = MultiLayerCache::new(l1, l2.clone());
layered.set("k", b"v".to_vec(), None).await.unwrap();
assert_eq!(layered.l1().get("k").await.unwrap(), Some(b"v".to_vec()));
assert_eq!(l2.get("k").await.unwrap(), Some(b"v".to_vec()));
}
#[tokio::test]
async fn invalidate_clears_both() {
let l1 = MemoryCache::new();
let l2 = MemoryCache::new();
let layered = MultiLayerCache::new(l1, l2);
layered.set("k", b"v".to_vec(), None).await.unwrap();
layered.invalidate("k").await.unwrap();
assert_eq!(layered.l1().get("k").await.unwrap(), None);
assert_eq!(layered.l2().get("k").await.unwrap(), None);
}
}
+124
View File
@@ -0,0 +1,124 @@
//! A distributed (L2) cache backed by Redis / Valkey (feature `l2-redis`).
//!
//! Wraps a `redis` async connection (multiplexed). Values are stored as raw
//! Redis strings with an optional TTL (`SETEX` when a TTL is given). The
//! caller provides the connection; this type only issues cache commands.
use std::time::Duration;
use async_trait::async_trait;
use redis::aio::MultiplexedConnection;
use redis::{AsyncCommands, RedisError};
use crate::traits::{Cache, CacheError};
/// Map a Redis error onto a [`CacheError`].
fn map_err(e: RedisError) -> CacheError {
CacheError::Io(e.to_string())
}
/// An L2 cache backed by a `redis` [`MultiplexedConnection`].
///
/// The connection is supplied by the caller; it is cheaply cloned (the
/// multiplexed connection is `Arc`-backed internally), so one pool can drive
/// both cache operations and other Redis usage.
#[derive(Clone)]
pub struct RedisCache {
conn: MultiplexedConnection,
/// Optional namespace prefix prepended to every key.
prefix: String,
}
impl RedisCache {
/// Wraps an existing connection.
pub fn new(conn: MultiplexedConnection) -> Self {
Self::with_prefix(conn, String::new())
}
/// Wraps a connection and adds a namespace prefix to every key.
pub fn with_prefix(conn: MultiplexedConnection, prefix: String) -> Self {
Self { conn, prefix }
}
fn key(&self, key: &str) -> String {
if self.prefix.is_empty() {
key.to_string()
} else {
format!("{}{}", self.prefix, key)
}
}
}
#[async_trait]
impl Cache for RedisCache {
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError> {
// `get::<_, Option<Vec<u8>>>` returns `None` for a missing key.
let mut c = self.conn.clone();
let k = self.key(key);
let result: Result<Option<Vec<u8>>, RedisError> = c.get(&k).await;
result.map_err(map_err)
}
async fn set(
&self,
key: &str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Result<(), CacheError> {
let mut c = self.conn.clone();
let k = self.key(key);
match ttl {
Some(ttl) => {
let secs = ttl.as_secs().max(1);
let result: Result<(), RedisError> = c.set_ex(&k, value, secs).await;
result.map_err(map_err)
}
None => {
let result: Result<(), RedisError> = c.set(&k, value).await;
result.map_err(map_err)
}
}
}
async fn invalidate(&self, key: &str) -> Result<(), CacheError> {
let mut c = self.conn.clone();
let k = self.key(key);
let result: Result<u64, RedisError> = c.del(&k).await;
result.map(|_| ()).map_err(map_err)
}
async fn clear(&self) -> Result<(), CacheError> {
// Deliberately does nothing: `FLUSHALL`/`FLUSHDB` are dangerous on a
// shared instance. Consumers should scope keys under a prefix and call
// `invalidate` for the keys they own.
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Integration test requiring a live Redis at `REDIS_URL`
/// (e.g. `redis://127.0.0.1:6379`). Run with:
/// `REDIS_URL=redis://127.0.0.1:6379 cargo test -p corex-cache --features l2-redis -- --ignored` .
#[tokio::test]
#[ignore = "requires a live Redis instance (REDIS_URL)"]
async fn set_get_roundtrip_live() {
let url = std::env::var("REDIS_URL").expect("set REDIS_URL");
let client = redis::Client::open(url).expect("valid redis url");
let conn = client
.get_multiplexed_tokio_connection()
.await
.expect("connect");
let cache = RedisCache::with_prefix(conn, "corex_cache_test:".to_string());
cache
.set("k", b"v".to_vec(), Some(Duration::from_secs(3600)))
.await
.unwrap();
assert_eq!(cache.get("k").await.unwrap(), Some(b"v".to_vec()));
cache.invalidate("k").await.unwrap();
assert_eq!(cache.get("k").await.unwrap(), None);
}
}
+77
View File
@@ -0,0 +1,77 @@
//! The core [`Cache`] and [`KeyEncoder`] traits.
use std::borrow::Cow;
use std::time::Duration;
use async_trait::async_trait;
/// Errors returned by cache operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CacheError {
/// The backend could not be reached (e.g. Redis connection lost).
Io(String),
/// A value could not be serialized / deserialized.
Serialization(String),
/// A key could not be encoded for the backend.
Key(String),
}
impl std::fmt::Display for CacheError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(s) => write!(f, "cache io: {s}"),
Self::Serialization(s) => write!(f, "cache serialization: {s}"),
Self::Key(s) => write!(f, "cache key: {s}"),
}
}
}
impl std::error::Error for CacheError {}
/// A generic byte-oriented cache.
///
/// Real caches operate on bytes or strings; typed convenience is layered on
/// top (see [`crate::memory::typed::TypedCache`], behind `cache-aside`).
/// Implementors control the value format.
#[async_trait]
pub trait Cache: Send + Sync {
/// Fetches a value by key. `None` indicates a miss.
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>;
/// Stores a value under `key`, optionally expiring after `ttl`.
async fn set(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>)
-> Result<(), CacheError>;
/// Removes a key.
async fn invalidate(&self, key: &str) -> Result<(), CacheError>;
/// Removes all entries.
async fn clear(&self) -> Result<(), CacheError>;
}
/// Keys given to the byte-oriented [`Cache`] are `&str`, but concrete backends
/// may need richer keys. [`KeyEncoder`] turns typed keys into canonical strings.
pub trait KeyEncoder {
/// The "shape" of a key, e.g. `"user:{id}:profile"`.
fn encode<C: Into<Cow<'static, str>>, R: std::fmt::Display>(parts: (C, R)) -> String;
}
/// A blanket implementation that formats `{collection}:{id}`.
pub struct DefaultKeyEncoder;
impl KeyEncoder for DefaultKeyEncoder {
fn encode<C: Into<Cow<'static, str>>, R: std::fmt::Display>(parts: (C, R)) -> String {
format!("{}:{}", parts.0.into(), parts.1)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_key_encoder_formats() {
assert_eq!(DefaultKeyEncoder::encode(("user", 42)), "user:42");
assert_eq!(
DefaultKeyEncoder::encode(("session", "abc-123")),
"session:abc-123"
);
}
}