Giter VIP home page Giter VIP logo

redis-raw-rs's Introduction

redis-raw-rs is a minimal Redis client library implementation. It exposes a general purpose interface to Redis.

The crate is called redis_raw and you can depend on it via cargo:

[dependencies]
redis_raw = "*"

If you want to use the git version:

[dependencies]
redis_raw = { version = "*", git = "[email protected]:aminroosta/redis-raw-rs.git" }

Basic Operation

redis_raw exposes two API levels: a low- and a lower-level part!
The low-level part does not expose all the functionality of redis and might take some liberties in how it speaks the protocol. The lower-level part of the API allows you to express any request on the redis level. You can fluently switch between both API levels at any point.

Connection Handling

For connecting to redis you can use tokio::net::TcpStream which can be converted to (or from) RedisConnection.

use redis_raw::RedisConnection;
use tokio::net::TcpStream;

async fn do_something()
 -> std::result::Result<(), Box<dyn std::error::Error>> {

   let stream = TcpStream::connect("127.0.0.1:6379").await?;
   let mut con: RedisConnection = stream.into();

    /* do something here */

    Ok(())
}
# fn main() {}

Executing Lower-Level Commands

To execute lower-level commands you can use the write() and read() functions which allow you to make redis requests and parse redis (RESP) responses. These functions correspond to the underlying socket's read and write operations.

The read() function parses the RESP response as redis_raw::Value.
Value Represents a redis RESP protcol response.

use redis_raw::{RedisConnection, RedisResult, Value }

fn do_something(con: &mut RedisConnection) -> RedisResult<Value> {
   con.write("set key vvv\r\n").await?
   con.read().await
}

Executing Low-Level Commands

The low-level interface is similar. The command() function does a write() and a read() and converts the Value into requested type.

use redis_raw::{RedisConnection, RedisResult, Value }

fn do_something(con: &mut RedisConnection) -> RedisResult<String> {
   con.command::<()>("set key value\r\n".to_owned()).await?;
   con.command::<i64>("append key !!!\r\n".to_owned()).await?;
   con.command::<String>("get key\r\n".to_owned()).await
}

Here is another example, to find out the correct result type see redis docs.

use redis_raw::{RedisConnection, RedisResult, Value }

fn do_something(con: &mut RedisConnection) -> RedisResult<Vec<String>> {
   for i in 1..10 {
       con.command::<i64>(format!("zadd myset {} {}\r\n", i, i*i)).await?;
   }
   con.command::<Vec<String>>("zrange myset 0 -1\r\n".to_owned()).await
}

The following return types are supported: (), i64, String, Vec<i64>, and Vec<String>

redis-raw-rs's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.