shardmap
shardmap is the embedded Rust map/cache crate for shard-kv. It gives
applications a cloneable, sharded in-process handle with byte-oriented keys and
values, TTL support, memory-limit eviction, lock helpers, prepared-key lookups,
and native semantic-cache APIs.
Use shardmap when you want an embedded Rust cache. Use the repository's
shardcache server package when you need a TCP service.
Install
[]
= "0.1.0"
Quick Start
use ShardMap;
let cache = new;
cache.insert_slice;
let value = cache.get_owned.unwrap;
assert_eq!;
ShardMap is a cheap cloneable handle. Clones share the same underlying
sharded store and can be moved into worker threads.
Feature Overview
| Area | What it gives you | Example |
|---|---|---|
| Point-key map | Insert, get, mutate, remove, and entry-style access for byte keys. | basic_map.rs |
| TTL cache | Relative TTL writes and memory-limit eviction. | ttl_and_locks.rs |
| Prepared keys | Route metadata for repeated hot-key lookups. | prepared_keys_threads.rs |
| Entry API | Occupied/vacant mutation without a separate lookup. | entry_api.rs |
| Route inspection | See which shard owns a key before sending work to a worker. | route_inspection.rs |
| Lock helpers | Process-local token locks built on SET key token NX PX ttl semantics. |
ttl_and_locks.rs |
| Configuration | Capacity hints, memory budgets, eviction policy, routing, and lock policy. | configured_cache.rs |
| Semantic cache | Store embeddings with cached values and search by cosine similarity. | semantic_cache.rs |
| Semantic TTL | Combine semantic reuse with freshness windows. | semantic_ttl.rs |
| Governance metadata | Attach application-owned authorization context to semantic hits. | semantic_cache.rs |
| Mini app | A small feature-flag cache combining TTL, prepared keys, and locks. | mini_feature_flags.rs |
Run any example with:
Point-Key Map Operations
Use the default ShardMap for a 64-stripe shared embedded map, or
ShardMapWithShards<N> when you want to choose the stripe count at compile
time.
use ShardMap;
let cache = with_capacity;
cache.insert_slice;
assert!;
if let Some = cache.get_mut
assert_eq!;
assert!;
Use get_owned when you want refcounted bytes after the shard read lock has
been released. Use get/get_ref when a short borrowed guard is enough.
TTL, Eviction, And Cache Configuration
TTL values are relative milliseconds. A None TTL means the value does not
expire because of time.
use ShardMap;
let cache = new;
cache.insert_slice_with_ttl;
assert!;
CacheOptions configures the shared-handle cache. Memory limits are enforced
inside each stripe, using the selected eviction policy.
use ;
use EvictionPolicy;
let cache = with_options;
assert_eq!;
EvictionPolicy::Lru and EvictionPolicy::Lfu are available in the default
crate. EvictionPolicy::Prefix is available with the prefix-eviction
feature for prefix-group cache workloads.
Prepared Keys And Concurrency
For repeated hot lookups, prepare the key once and reuse the route metadata.
use ShardMap;
let cache = new;
cache.insert_slice;
let prepared = cache.prepare_key;
let value = cache.get_prepared_owned.unwrap;
assert_eq!;
Cloned handles share the same storage, so applications can move a clone into each worker thread and keep using normal map operations.
Entry API And Routing
Use entry when the update naturally depends on whether the key is already
present.
use Bytes;
use ShardMap;
let cache = new;
let value = cache.entry
.or_insert;
assert_eq!;
Use route_key when your application already partitions work by shard and
wants to send a key to its owning worker.
use ShardMapWithShards;
let cache = new;
let route = cache.route_key;
assert!;
Lock Helpers
The lock helpers are useful for process-local coordination in embedded mode. They acquire only when the key is absent or expired, release only when the stored token matches, and renew by extending the TTL for the matching token.
use ShardMap;
let cache = new;
assert!;
assert!;
assert!;
assert!;
Use the server surface when multiple processes or machines need to coordinate through one lock table.
Semantic Cache
Semantic cache entries attach a normalized embedding to the same point-key value. Lookups search live semantic entries and return the best match at or above the requested score.
use ShardMap;
let cache = new;
cache.insert_semantic_slice?;
cache.insert_semantic_slice?;
let matched = cache.semantic_search?.unwrap;
assert_eq!;
assert_eq!;
# Ok::
Plain writes to a key clear its semantic embedding, so semantic hits cannot
return a value whose embedding describes an older payload. Repeated exact
semantic queries use an internal query-result cache; call
disable_semantic_query_cache when benchmarking the cold vector path.
Governance Metadata
Cross-user semantic cache entries can carry opaque governance metadata. Entries
written through the default semantic APIs return None; applications that need
cross-user authorization can opt into the governance API layer and pass a
predicate that must approve the metadata before the cached value is released.
use ShardMap;
let cache = new;
cache.insert_semantic_slice_with_governance?;
let matched = cache
.semantic_search_with_governance_filter?
.unwrap;
assert_eq!;
assert_eq!;
# Ok::
The intended data model is:
| Field | Example | Purpose |
|---|---|---|
key |
semantic:tenant/acme/faq/refund-policy |
Stable cache identity for the answer. |
value |
cached response bytes | The answer that may be reused. |
embedding |
normalized prompt embedding | Semantic lookup vector. |
governance |
{tenant, policy_version, allowed_groups, source_docs} |
Opaque authorization context owned by the application. |
ttl |
Some(300_000) |
Optional freshness bound for the cached answer. |
The cache does not parse governance bytes. Callers can encode tenant, group, source document, policy version, retention tier, region, or audit context in whatever format they already use, then decide whether a semantically close candidate may release its cached value.
Optional Server, Protocol, And Persistence Internals
The crate also contains the storage internals used by the shardcache server:
command parsing, RESP/SCNP protocol code, persistence, replication, and server
transport modules. Those surfaces are feature-gated so embedded users do not
compile server code by default.
Most applications should start with ShardMap. Use lower-level modules only
when you are building a custom server, embedding the protocol layer, or wiring
storage into a specialized runtime.
API Shape
ShardMap: default embedded map/cache handle.ShardCache: cache-flavored alias forShardMap.ShardMapWithShards<N>: embedded handle with an explicit stripe count.CacheOptions: embedded capacity, memory, routing, and lock options.get_ownedandget_prepared_owned: return refcounted bytes after releasing the shard read lock.entry,get_mut,try_insert_slice, and lock helpers: DashMap-style mutation and coordination APIs.insert_semantic_sliceandsemantic_search: native semantic-cache APIs.semantic_search_with_governance_filter: semantic cache lookup with request-specific authorization.
Features
| Feature | Default | Purpose |
|---|---|---|
sharded |
Yes | Embedded sharded map/cache API. |
redis |
No | Redis/Valkey object and command behavior for shared internals. |
server |
No | TCP server internals used by the source-only shardcache package. |
redis-server |
No | Server internals plus Redis/Valkey compatibility. |
telemetry |
No | Embedded operational metrics. |
monoio |
No | Linux-only server transport internals. |
prefix-eviction |
No | Enables EvictionPolicy::Prefix for prefix-group memory-limit eviction. |
License
Licensed under Apache-2.0.