Search

Lib.rs

› Database interfaces
#key-value-store #key-value-cache #cache #async #store-storage

keyv

Simple key-value storage with support for multiple backends

by Christian Llontop and 2 contributors

  • Install
  • API reference
  • GitHub repo (chrisllontop)

3 unstable releases

0.3.0 Dec 1, 2025
0.2.1 Apr 18, 2024
0.1.0 Apr 16, 2024
0.0.2-alpha.1 Dec 27, 2023

#2059 in Database interfaces

MIT license

64KB
945 lines

Keyv Rust

Simple key-value storage with support for multiple backends.

Crates.io GitHub Actions Workflow Status GitHub Repo stars Crates.io Total Downloads

Simple key-value storage with support for multiple backends Keyv provides a consistent interface for key-value storage across multiple backends via storage adapters. It supports TTL based expiry, making it suitable as a cache or a persistent key-value store.

Usage

Instalation

cargo add keyv

Store Adapters

Keyv supports multiple store adapters, you can enable them by specifying the feature flag.

  • full: Enables all available adapters.
  • redis: Redis store adapter.
  • postgres: PostgreSQL store adapter.
  • mysql: MySQL store adapter.
  • mongodb: MongoDB store adapter.
  • sqlite: SQLite store adapter.
cargo add keyv --features <store>

Initialization

By default, everything is stored in memory, you can optionally also install a storage adapter.

  • Inmemory (default)

    let keyv = Keyv::default();
    
  • Store Adapters examples

    • Postgres
    • Redis
    • Mongodb
    • Sqlite
    • MySQL

Interacting with Store

use keyv::Keyv;

#[tokio::main]
async fn main() {
    let keyv = Keyv::default();
    keyv.set("number", 42).await.unwrap();
    keyv.set("number", 10).await.unwrap();
    keyv.set("array", vec!["hola", "test"]).await.unwrap();
    keyv.set("string", "life long").await.unwrap();

    match keyv.get("number").await.unwrap() {
        Some(number) => {
            let number: i32 = serde_json::from_value(number).unwrap();
            assert_eq!(number, 10);
        }
        None => assert!(false),
    }

    match keyv.get("string").await.unwrap() {
        Some(string) => {
            let string: String = serde_json::from_value(string).unwrap();
            assert_eq!(string, "life long");
        }
        None => assert!(false),
    }

    match keyv.get("array").await.unwrap() {
        Some(array) => {
            let array: Vec<String> = serde_json::from_value(array).unwrap();
            assert_eq!(array, vec!["hola".to_string(), "test".to_string()])
        }
        None => assert!(false),
    }

    match keyv.remove_many(&["number", "string"]).await {
        Ok(_) => {}
        Err(_) => assert!(false),
    }
}

Dependencies

~3–21MB
~237K SLoC

  • async-trait
  • log
  • serde
  • serde_json
  • thiserror 2.0
  • tokio+full
  • mongo? mongodb 3.4
  • optional redis 0.32.7
  • mysql? postgres? sqlite? sqlx
  • dev cargo-tarpaulin 0.33

Other feature

  • full
See also: lru-cache, wasefire-store, ember-protocol, lsm-tree, cacache, epoch-db, disklru, bazuka, mdl, sine_cache, databeam

Lib.rs is an unofficial list of Rust/Cargo crates, created by kornelski. It contains data from multiple sources, including heuristics, and manually curated data. Content of this page is not necessarily endorsed by the authors of the crate. This site is not affiliated with nor endorsed by the Rust Project. If something is missing or incorrect, please file a bug.