Use redis as backend

These docs provide a detailed examples for using redis as backend.

We can run this example via:

cargo run --example redis --features services-redis

Example

Via Environment

All config could be passed via environment:

  • OPENDAL_REDIS_ENDPOINT endpoint to redis services, required.
  • OPENDAL_REDIS_ROOT root dir of this ftp services, default to /
  • OPENDAL_REDIS_USERNAME
  • OPENDAL_REDIS_PASSWORD
  • OPENDAL_REDIS_DB
use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;

#[tokio::main]
async fn main() -> Result<()> {
    // Init Operator from env.
    let op = Operator::from_env(Scheme::Redis)?;
}

Via Builder

//! example for initiating a Redis backend

use std::env;

use anyhow::Result;
use log::info;
use opendal::services::redis;
use opendal::services::redis::Builder;
use opendal::Operator;

#[tokio::main]
async fn main() -> Result<()> {
    if env::var("RUST_LOG").is_err() {
        env::set_var("RUST_LOG", "debug");
    }
    env_logger::init();
    println!(
        r#"
        OpenDAL redis example.

        Available Environment Variables:

        - OPENDAL_REDIS_ENDPOINT: network address of redis server, default is "tcp://127.0.0.1:6379"
        - OPENDAL_REDIS_ROOT: working directory of opendal, default is "/"
        - OPENDAL_REDIS_USERNAME: username for redis, default is no username
        - OPENDAL_REDIS_PASSWORD: password to log in. default is no password
        - OPENDAL_REDIS_DB: Redis db to use, default is 0.
        "#
    );

    // Create redis backend builder
    let mut builder: Builder = redis::Builder::default();

    // Set the root, all operations will happen under this directory, or prefix, more accurately.
    //
    // NOTE: the root must be absolute path
    builder.root(&env::var("OPENDAL_REDIS_ROOT").unwrap_or_else(|_| "/".to_string()));

    // Set the endpoint, the address of remote redis server.
    builder.endpoint(
        &env::var("OPENDAL_REDIS_ENDPOINT").unwrap_or_else(|_| "tcp://127.0.0.1:6379".to_string()),
    );

    // Set the username
    builder.username(&env::var("OPENDAL_REDIS_USERNAME").unwrap_or_else(|_| "".to_string()));

    // Set the password
    builder.password(&env::var("OPENDAL_REDIS_PASSWORD").unwrap_or_else(|_| "".to_string()));

    // Set the db
    builder.db(env::var("OPENDAL_REDIS_DB")
        .map(|s| s.parse::<i64>().unwrap_or_default())
        .unwrap_or_else(|_| 0));

    // `Accessor` provides the low level APIs, we will use `Operator` normally.
    let op: Operator = Operator::new(builder.build()?);

    let path = uuid::Uuid::new_v4().to_string();

    // Create an object handle to start operation on object.
    info!("try to write file: {}", &path);
    op.object(&path).write("Hello, world!").await?;
    info!("write file successful!");

    info!("try to read file: {}", &path);
    let content = op.object(&path).read().await?;
    info!(
        "read file successful, content: {}",
        String::from_utf8_lossy(&content)
    );

    info!("try to get file metadata: {}", &path);
    let meta = op.object(&path).metadata().await?;
    info!(
        "get file metadata successful, size: {}B",
        meta.content_length()
    );

    info!("try to delete file: {}", &path);
    op.object(&path).delete().await?;
    info!("delete file successful");

    Ok(())
}