TinyKV
A minimal JSON-based key-value store for Rust with TTL support and multi-platform compatibility.
Overview
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.
Features
- JSON file-based persistence with atomic writes
- TTL (time-to-live) expiration for keys
- Auto-save functionality
- Backup support with .bak files
- Multi-platform: std, no_std, and WebAssembly
- Flexible serialization: serde or nanoserde
- Thread-safe operations
Installation
Add to your Cargo.toml:
# Default configuration (std + serde)
= "0.4"
# For embedded systems (no_std + nanoserde)
= { = "0.4", = false, = ["nanoserde"] }
# Minimal configuration (no_std only)
= { = "0.4", = false }
# WebAssembly support
= { = "0.4", = ["wasm", "nanoserde"] }
Quick Start
Basic Usage
use TinyKV;
Embedded Systems (no_std)
extern crate alloc;
use TinyKV;
WebAssembly
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.
Data Format
TinyKV stores data in a structured JSON format:
Feature Flags
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 backend
API Reference
Core Methods
TinyKV::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 disk
Configuration
with_auto_save()- Enable automatic savingwith_backup(enabled)- Enable/disable backup fileswith_namespace(prefix)- Set key namespace prefixpurge_expired()- Remove expired entries
Platform Compatibility
| Platform | File I/O | TTL | Auto-save | Serialization |
|---|---|---|---|---|
| std | ✓ | ✓ | ✓ | serde/nanoserde |
| no_std | ✗ | ✗ | ✗ | nanoserde/manual |
| WASM | localStorage | ✓ | ✓ | nanoserde |
Use Cases
Ideal for:
- Application configuration files
- Game save data
- CLI tool settings
- Test data persistence
- Embedded device configuration
- Browser-based applications
- Rapid prototyping
Not recommended for:
- High-performance databases
- Complex relational queries
- Concurrent multi-user access
- Large datasets (>100MB)
Documentation
Full API documentation is available at docs.rs/tinykv.
Changelog
Version 0.4.0
- BREAKING: WASM
setWithTtlnow acceptsnumberinstead ofbigint - Added namespace support with
with_namespace(prefix)method - Added prefix operations:
list_keys(prefix)andclear_prefix(prefix) - Enhanced WASM bindings with
listKeys()andclearPrefix()methods - Modernized package.json with
exportsfield andsideEffects: false - Improved WASM performance documentation
Version 0.3.0
- Enhanced no_std support
- Updated documentation and examples
- Improved JSON output formatting
Version 0.2.0
- Added nanoserde support for minimal binary size
- Improved compilation speed
- Enhanced embedded systems compatibility
Version 0.1.0
- Initial release
- Core key-value operations with TTL
- File-based JSON persistence
- Auto-save and backup support
License
MIT License