Skip to content

General key value storage for actix-web with switchable backends(redis, sled, hashmaps)

License

Notifications You must be signed in to change notification settings

tristan-f-r/actix-storage

 
 

Repository files navigation

Actix-storage



Note The main branch includes many breaking changes, switch to v0.3 branch for the released version.


Actix storage is a simple wrapper around some key-value storages to provide basic operations without knowing the backend in advance.

Install

Actix-storage is meant to be used alongside one the implementer crates, ex:

# Cargo.toml
[dependencies]
actix-storage = "0.3.0"
actix-storage-hashmap = "0.3.0"

Usage

After you picked an implementer:

use actix_storage::{Storage, Format};
use actix_storage_hashmap::HashMapBackend;
use actix_web::{App, HttpServer};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
   // Intialize the implementer according to its docs
   let store = HashMapBackend::start_default();

   // Give it to the Storage struct
   let storage = Storage::build().store(store).finish();

   // Or if it doesn't support expiring functionality
   // it will give errors if those methods are called
   let storage = Storage::build().store(store).no_expiry().finish();

   // It is also possible to feed a seprate expiry,
   // as long as it works on the same storage backend
   let storage = Storage::build().store(store).expiry(expiry).finish();


   // Store it in you application state with actix_web::App.app_data
   let server = HttpServer::new(move || {
      App::new()
            .app_data(storage.clone())
   });
   server.bind("localhost:5000")?.run().await
}

And later in your handlers

async fn index(storage: Storage) -> Result<String, Error>{
   storage.set("key", b"value").await;
   let val = storage.get(b"key").await?.unwrap_or_default();

   Ok(std::str::from_utf8(&val)
      .map_err(|err| error::ErrorInternalServerError("Storage error"))?.to_string())
}

Implementations

actix-storage-hashmap docs.rs docs

actix-storage-sled docs.rs docs

actix-storage-redis docs.rs docs

Why?

It can be usefull when:

  1. You don't know which key-value database you'll need later.
  2. You can't afford the long time compilation of some dbs while developing.
    • hashmap store compiles pretty fast
  3. You're writing an actix-web extension library and need to support multiple storage backends.

Why not?

If you really care about every drop of your application performance then actix-storage may not be for you, as it uses dynamic dispatching internally.

Examples

There are bunch of examples in the examples folder, very basic ones thought, but it will give you the idea.

License

This project is licensed under either of

at your option.

About

General key value storage for actix-web with switchable backends(redis, sled, hashmaps)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%