Skip to content

feat(core): implement DiskCache using sled - #414

Merged
codeZe-us merged 1 commit into
Toolbox-Lab:mainfrom
JuliobaCR:feat/403-disk-cache-sled
Aug 23, 2026
Merged

feat(core): implement DiskCache using sled#414
codeZe-us merged 1 commit into
Toolbox-Lab:mainfrom
JuliobaCR:feat/403-disk-cache-sled

Conversation

@JuliobaCR

Copy link
Copy Markdown
Contributor

Description

Adds DiskCache, a persistent, disk-backed cache implementing the CacheProvider trait (#412) using sled, a high-performance embedded database. This allows large or long-lived data to be stored on the local filesystem and survive application restarts, without a custom eviction/LRU scheme.

How It Was Done

  • crates/core/src/cache/disk.rs: DiskCache struct wrapping an Arc<sled::Db>, with new(path) and default_location() constructors (the latter reuses the same directories::ProjectDirs convention as CacheStore).
  • Implements CacheProvider::{get, put, remove, clear}: values are serialized/deserialized with bincode before hitting the sled tree; keys stay &str, consistent with the trait.
  • Relies on sled's internal lock-free concurrency guarantees for safe concurrent access — no external mutex needed. Blocking sled calls are offloaded via tokio::task::spawn_blocking so the async executor is never blocked.
  • Serialization/deserialization failures map to the typed GratError::CacheSerializationError / CacheDeserializationError variants added in feat(core): implement CacheProvider trait #412; lower-level backend I/O failures (open/get/insert/remove/clear, task join errors) reuse the existing GratError::CacheError(String) variant, consistent with CacheStore.
  • Added sled = "0.34" as a workspace dependency, following the same { workspace = true } pattern as redb.
  • Added a flush() method (via sled's async flush_async) so callers needing durability guarantees (e.g. before a restart) can await it explicitly, since sled lazily batches writes.

Issues Encountered (If Any)

  • bincode is not self-describing, so deserializing into the wrong type doesn't reliably error unless the stored bytes are too short for the target type's layout — the type-mismatch test exploits that instead of relying on bincode detecting the mismatch directly.

Related Issue

Closes #403

How It Was Tested

cargo test -p grat-core --lib cache::disk — 11/11 passing: put/get roundtrip, put overwrites existing entry, cache miss returns Ok(None), remove (present and missing key), clear, persistence across a Db close/reopen cycle (via explicit flush()), concurrent put/get from 20 tokio tasks, and a typed deserialization-error case.

cargo clippy -p grat-core --lib --tests -- -D warnings and cargo fmt --all -- --check are clean for the new code.

Screenshots / Video (If Applicable)

N/A — no UI impact.

Adds DiskCache, a CacheProvider backed by sled, a persistent, embedded,
lock-free B+tree store. Values are serialized with bincode before being
written to the tree, so entries survive process restarts. Blocking sled
calls are offloaded to tokio::task::spawn_blocking to keep the async
executor unblocked.

Closes Toolbox-Lab#403
@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: f717c2af-fd47-40f8-b52a-0a554a6a28cd


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 22:07

@codeZe-us codeZe-us left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pr reviewed

@codeZe-us
codeZe-us merged commit e0a2f37 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 DiskCache using Sled

2 participants