Skip to content

feat(core): implement CacheProvider trait - #412

Merged
codeZe-us merged 1 commit into
Toolbox-Lab:mainfrom
JuliobaCR:feat/402-cache-provider-trait
Aug 23, 2026
Merged

feat(core): implement CacheProvider trait#412
codeZe-us merged 1 commit into
Toolbox-Lab:mainfrom
JuliobaCR:feat/402-cache-provider-trait

Conversation

@JuliobaCR

Copy link
Copy Markdown
Contributor

Description

Adds CacheProvider, the shared async contract (get/put/remove/clear) that cache backends implement, so the rest of the codebase can depend on "a cache" without caring which storage mechanism backs it. This is the foundational trait that the planned memory, disk, and Wasm-specific caches (#403, #404, #405) will implement.

How It Was Done

  • crates/core/src/cache/provider.rs: CacheProvider trait, generic over the value type (Serialize / DeserializeOwned + Send / Sync); keys stay &str, consistent with the existing CacheStore.
  • Methods return impl Future<..> + Send (RPITIT) rather than async fn. A plain async fn in a trait doesn't guarantee the returned future is Send, and backends that offload I/O via tokio::spawn / spawn_blocking need that guarantee — this also avoids the boxing overhead of #[async_trait].
  • crates/core/src/error.rs: extends GratError with dedicated CacheMiss, CacheCapacityExceeded, CacheSerializationError, and CacheDeserializationError variants, so backends can surface typed errors instead of ad-hoc strings. The existing CacheError(String) variant is left as-is since CacheStore already depends on it.
  • Adds a hand-rolled in-memory conformance test double (no new dependency) implementing CacheProvider, plus a trait-level test suite any future backend's own tests can mirror.

Issues Encountered (If Any)

  • get's V bound needed an explicit Send (not just DeserializeOwned) for the returned future to actually be Send — the value type is part of the future's Output, so it's included in the auto-trait computation even though it's only read after the await point.
  • A pre-existing cargo build --workspace --all-targets --all-features failure in grat-wasm (rlib/dylib mismatch for wasmparser/rustls/protobuf) reproduces identically on a clean main checkout, unrelated to this change.

Related Issue

Closes #402

How It Was Tested

cargo test -p grat-core --lib cache:: — 9/9 passing (7 new + 2 existing CacheStore tests): get-after-put, put-overwrites, miss returns Ok(None) (not an error), remove, remove-of-missing-key, clear, and over-capacity returns a typed error.

cargo fmt --all -- --check and cargo clippy -p grat-core --lib --tests -- -D warnings are clean for the new code (pre-existing clippy findings elsewhere in the crate are untouched by this PR).

Screenshots / Video (If Applicable)

N/A — trait-only change, no UI impact.

Adds the shared async contract (get/put/remove/clear) that memory, disk,
and Wasm-specific cache backends (Toolbox-Lab#403/Toolbox-Lab#404/Toolbox-Lab#405) will implement, so the
rest of the codebase can depend on "a cache" without caring which storage
mechanism backs it.

- Generic over the value type (Serialize/DeserializeOwned + Send/Sync);
  keys stay &str, consistent with the existing CacheStore.
- Methods return impl Future<..> + Send (RPITIT) rather than async fn,
  since a plain async fn in a trait doesn't guarantee a Send future -
  needed for backends that offload I/O via tokio::spawn/spawn_blocking.
- Extends GratError with dedicated CacheMiss, CacheCapacityExceeded,
  CacheSerializationError, and CacheDeserializationError variants so
  backends can surface typed errors instead of ad-hoc strings.
- Adds a hand-rolled in-memory conformance test double covering the
  trait's contract: get-after-put, overwrite, miss returns Ok(None)
  (not an error), remove, remove-of-missing-key, clear, and capacity
  overflow.

Closes Toolbox-Lab#402
@coderabbitai

coderabbitai Bot commented Aug 23, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 22ed857c-6ed0-43b6-a4ff-691f46d74e3b


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codeZe-us
codeZe-us self-requested a review August 23, 2026 19:11
@codeZe-us

Copy link
Copy Markdown
Contributor

PR reviewed

@codeZe-us
codeZe-us merged commit a7426bf into Toolbox-Lab:main Aug 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement CacheProvider Trait

2 participants