tinykv 0.1.0

A simple, file-backed, human-readable key-value store with TTL support
Documentation

tinykv

๐Ÿชถ A minimal file-backed key-value store for Rust.

tinykv is a lightweight, serde-powered key-value storage engine with optional TTL (time-to-live) support, atomic saving, and human-readable persistence via JSON.

Perfect for config storage, CLI apps, prototyping, testing, and anywhere you need a simple persistent store without setting up a database.


๐Ÿ† Why tinykv?

Feature tinykv sled pickledb HashMap + JSON
Human-readable โœ… โŒ โœ… โœ…
TTL Support โœ… โŒ โŒ โŒ
Actively maintained โœ… โš ๏ธ โŒ N/A
Learning curve โœ… Simple โŒ Complex โœ… Simple โŒ Manual
Serde integration โœ… โŒ โœ… โŒ

โœจ Features

  • โœ… JSON file storage (human-readable)
  • โœ… serde-based serialization
  • โœ… Optional TTL (Time-to-Live) per entry
  • โœ… Auto-save on every write (optional)
  • โœ… Backup file creation (optional)
  • โœ… Atomic write safety (.tmp + rename)
  • โœ… Drop-safe saving
  • โœ… Fully tested with tempfile isolation

๐ŸŽฏ Use Cases

  • CLI Tools: Store user preferences and config
  • Desktop Apps: Save application state and settings
  • Games: Simple save game systems
  • Prototyping: Quick persistent storage without DB setup
  • IoT/Embedded: Lightweight device configuration
  • Testing: Mock persistent storage in tests

๐Ÿš€ Quick Start

# Cargo.toml
[dependencies]
tinykv = "0.1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
use tinykv::TinyKV;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut kv = TinyKV::open("store.json")?
        .with_auto_save()
        .with_backup(true);

    kv.set("username", "hasan")?;
    kv.set_with_ttl("token", "abc123", 10)?; // expires in 10 seconds

    if let Some(username) = kv.get::<String>("username")? {
        println!("Welcome, {username}");
    }

    kv.save()?; // not needed with auto_save, but explicit is okay
    Ok(())
}

๐Ÿ“ฆ Example File Output

{
  "username": {
    "value": "hasan",
    "expires_at": null
  },
  "token": {
    "value": "abc123",
    "expires_at": 1721234567
  }
}

๐Ÿงช Running Tests

cargo test

Tests use tempfile to avoid polluting your working directory.

๐Ÿ“ Planned (Optional) Features

  • purge_on_load() builder option
  • Pluggable serialization formats (YAML, Bincode) via feature flags

๐Ÿ“š Full API Documentation

๐Ÿ“œ License

This project is licensed under the MIT License.

MIT License

Copyright (c) 2025 Hasan YILDIZ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      
copies of the Software, and to permit persons to whom the Software is          
furnished to do so, subject to the following conditions:                       

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.                                

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.