Cookies Psst! Do you accept cookies?

We use cookies to enhance and personalise your experience.
Please accept our cookies. Checkout our Cookie Policy for more information.

Introducing "keyv": A Key-Value Storage for Rust

Today, I'm happy to announce that I've released keyv, a Rust library designed to simplify key-value storage and increase its flexibility.

What is keyv?

keyv is a simple key-value storage library that supports multiple backends. It offers a consistent interface for key-value storage across these backends. With support for TTL-based expiry, it's versatile enough to act as either a cache or a persistent key-value store, adapting to your needs.

Where Could You Use keyv?

Imagine you're working on a high-traffic web service that needs to cache user sessions. keyv can swiftly partner with a Redis backend to give you that in-memory speed. Or, let's say you're building an application that requires durable storage. Here, keyv with PostgreSQL would be your go-to.

Here are some scenarios where keyv shines:

  • Caching frequently accessed data with in-memory databases like Redis.
  • Persisting user settings or session data in SQL databases such as SQLite or PostgreSQL.
  • Building a quick prototype where you need a straightforward storage solution without the overhead of complex configurations.

Getting Started with keyv

Setting up keyv is a breeze. You can start by adding it to your Cargo.toml:

[dependencies]
keyv = "0.2.1"

or by running:

cargo add keyv

Store Adapters

keyv embraces versatility with multiple store adapters, allowing seamless integration with various storage backends. Activate them with a feature flag:

  • full: All-in-one feature for enabling every available adapter.
  • Redis: In-memory storage for speedy cache.
  • Postgres: Robust and structured data storage.
  • MySQL: Reliable storage within the MySQL ecosystem.
  • MongoDB: Schema flexibility with a NoSQL approach.
  • SQLite: Simple setup, ideal for development.

Add keyv with your chosen adapter:

cargo add keyv --features "sqlite" # Example for SQLite

Example Uses of keyv

keyv provides a straightforward way to manage key-value data. Here’s how you can use it in your Rust projects.

InMemory (default)

use keyv::Keyv;

#[tokio::main]
async fn main() {
    // Initialize `keyv` with default in-memory storage
    let keyv = Keyv::default();

    // Setting a value
    keyv.set("my_key", "my_value").await.unwrap();

    // Getting a value
    if let Some(value) = keyv.get::<String>("my_key").await.unwrap() {
        println!("Retrieved value: {}", value);
    } else {
        println!("Value not found");
    }

    // Removing a value
    keyv.remove("my_key").await.unwrap();
}

Using with Store adapters

Adjust keyv to use different store adapters like Redis.

#[cfg(feature = "redis")]
use keyv::{adapter::redis::RedisStoreBuilder, Keyv};

#[cfg(feature = "redis")]
#[tokio::main]
async fn main() {
    // Initialize Redis storage with default TTL
    let store = RedisStoreBuilder::new()
        .uri("redis://localhost:6379")
        .default_ttl(3600) // 1 hour TTL
        .build()
        .await
        .unwrap();

    // Create `keyv` instance with Redis store
    let keyv = Keyv::try_new(store).await.unwrap();

    // Set and retrieve a value
    keyv.set("my_key", "my_value").await.unwrap();
    let value: String = keyv
        .get("my_key")
        .await
        .unwrap()
        .expect("Key not found");

    println!("Retrieved value: {}", value);
}

#[cfg(not(feature = "redis"))]
fn main() {
    println!("This example requires the 'redis' feature. Please enable it.");
}

Easy to set up and super straightforward to use, give it a whirl in your next project. If you run into any issues or have suggestions, feel free to reach out on GitHub.

Happy coding! I hope keyv makes your key-value data storage a bit easier!

Last Stories

What's your thoughts?

Please Register or Login to your account to be able to submit your comment.