Crates.io | tinykv |
lib.rs | tinykv |
version | 0.4.0 |
created_at | 2025-07-04 21:49:35.011935+00 |
updated_at | 2025-09-02 11:47:13.424747+00 |
description | A simple, file-backed, human-readable key-value store with TTL support |
homepage | https://github.com/hsnyildiz/tinykv |
repository | https://github.com/hsnyildiz/tinykv |
max_upload_size | |
id | 1738536 |
size | 71,278 |
A minimal JSON-based key-value store for Rust with TTL support and multi-platform compatibility.
TinyKV provides a simple persistent key-value store that works across different Rust environments - from standard desktop applications to embedded systems and WebAssembly. Data is stored in human-readable JSON format with optional automatic expiration.
Add to your Cargo.toml
:
# Default configuration (std + serde)
tinykv = "0.4"
# For embedded systems (no_std + nanoserde)
tinykv = { version = "0.4", default-features = false, features = ["nanoserde"] }
# Minimal configuration (no_std only)
tinykv = { version = "0.4", default-features = false }
# WebAssembly support
tinykv = { version = "0.4", features = ["wasm", "nanoserde"] }
use tinykv::TinyKV;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create or open a store with namespace
let mut store = TinyKV::open("data.json")?
.with_auto_save()
.with_namespace("app1");
// Store some data (automatically prefixed with "app1:")
store.set("username", "alice")?;
store.set("count", 42)?;
// Store with expiration (1 hour)
store.set_with_ttl("session_token", "abc123", 3600)?;
// Retrieve data
let username: Option<String> = store.get("username")?;
let count: Option<i32> = store.get("count")?;
// List keys (returns ["username", "count", "session_token"])
let keys = store.keys();
println!("User: {:?}, Count: {:?}", username, count);
Ok(())
}
#![no_std]
extern crate alloc;
use tinykv::TinyKV;
fn main() -> Result<(), tinykv::TinyKVError> {
let mut store = TinyKV::new();
store.set("device_id", "ESP32_001")?;
store.set("config", "production")?;
// Serialize for external storage
let serialized = store.to_data()?;
// Later, restore from serialized data
let restored_store = TinyKV::from_data(&serialized)?;
Ok(())
}
import { TinyKVWasm } from 'tinykv';
// Use browser localStorage
const store = TinyKVWasm.openLocalStorage('myapp');
store.set('theme', 'dark');
store.setWithTtl('session', 'abc123', 3600); // 1 hour
// Prefix operations
const userKeys = store.listKeys('user:'); // ['user:123', 'user:456']
const deleted = store.clearPrefix('temp:'); // returns count
Note: For optimal performance, serve WASM files with Content-Type: application/wasm
. TinyKV will automatically fallback to slower instantiation if the MIME type is incorrect.
TinyKV stores data in a structured JSON format:
{
"username": {
"value": "alice",
"expires_at": null
},
"session_token": {
"value": "abc123",
"expires_at": 1725300000
}
}
std
(default): Enables file I/O, TTL, and standard library featuresserde
(default): Uses serde for serialization (maximum compatibility)nanoserde
: Uses nanoserde for faster compilation and smaller binarieswasm
: Enables WebAssembly support with localStorage backendTinyKV::open(path)
- Open or create file-based storeTinyKV::new()
- Create in-memory storeset(key, value)
- Store a valueset_with_ttl(key, value, seconds)
- Store with expirationget(key)
- Retrieve a valueremove(key)
- Delete a keycontains_key(key)
- Check if key existskeys()
- List all keyslist_keys(prefix)
- List keys with prefixclear()
- Remove all entriesclear_prefix(prefix)
- Remove entries with prefixsave()
- Manually save to diskwith_auto_save()
- Enable automatic savingwith_backup(enabled)
- Enable/disable backup fileswith_namespace(prefix)
- Set key namespace prefixpurge_expired()
- Remove expired entriesPlatform | File I/O | TTL | Auto-save | Serialization |
---|---|---|---|---|
std | ✓ | ✓ | ✓ | serde/nanoserde |
no_std | ✗ | ✗ | ✗ | nanoserde/manual |
WASM | localStorage | ✓ | ✓ | nanoserde |
Ideal for:
Not recommended for:
Full API documentation is available at docs.rs/tinykv.
setWithTtl
now accepts number
instead of bigint
with_namespace(prefix)
methodlist_keys(prefix)
and clear_prefix(prefix)
listKeys()
and clearPrefix()
methodsexports
field and sideEffects: false
MIT License