#command #client #iris #in-memory #client-connect #expression

bin+lib iris_client

Rust 中类似内存数据库的命令

3 个版本

0.1.2 2024 年 4 月 20 日
0.1.1 2024 年 4 月 14 日
0.1.0 2024 年 4 月 14 日

#1251数据库接口

Download history 174/week @ 2024-04-12 142/week @ 2024-04-19 8/week @ 2024-04-26 1/week @ 2024-05-17

97 每月下载量

自定义许可

20KB
177

iris_client

与 iris 交互的 crate。一个内存数据库

安装

[dependencies]
iris_client = "0.1.1"

示例

use iris_client::{connect, Expression, DeleteExpression};

#[tokio::main]
async fn main() -> Result<(), String> {
    let mut client = connect("127.0.0.1:3000").await?;

    // Sets an item in the database
    let user_id = client.set("user:joe", "foo bar").await?; // Returns the id so "user:joe"

    // Gets the value based on the id
    let user_value = client.get(user_id).await?; // Returns "foo bar"

    // List items in the database based on count. -1 means get all of them
    let list_count = client.list(Expression::Number(-1)).await?; // Returns Vec<Item>

    // List items in the database based on range. You can also do (3..-1) to get the items from 3 up to the length of the items
    let list_expr = client.list(Expression::Range(0..3)).await?; // Returns Vec<Item>

    // Just returns how many items currently in the database
    let count = client.count(Expression::Number(-1)).await?; // Returns u32

    // Deletes an item in the database based on id
    let deleted_user_id = client.delete(DeleteExpression::ID("user:joe")).await?; // Returns Vec<Item>

    // Deletes an item in the database based on count. (This deletes every item from 0 to 2)
    let deleted_user_count = client.delete(DeleteExpression::Number(2)).await?; // Returns Vec<Item>

    // Deletes an item in the database based on range.
    let deleted_user_expr = client.delete(DeleteExpression::Range(0..2)).await?; // Returns Vec<Item>

    // If you want you can also send commands raw
    let raw = client.delete("GET user:joe").await?; // Returns ServerResponse

    Ok(())
}

管道

您还可以使用管道执行命令。上一个命令的返回值将附加到当前命令中

use iris_client::connect;

#[tokio::main]
async fn main() -> Result<(), String> {
    let mut client = connect("127.0.0.1:3000").await?;

    // NOTE: I will rewrite this api to make it better. It sucks rn
    let pipe_commands = client.pipe()
        .pipe()
        .set("someid", "foo bar") // Returns an id
        .get("") // The id will be appended in here
        .await?; // Returns ServerResponse for now.

    // This shows how you can pipe commands in raw. Tbh this makes sense more than the current pipe api
    let pipe_raw = client.raw("SET someid this is data ~> GET").await?; // Returns ServerResponse

    Ok(())
}

贡献

欢迎为 iris 贡献!如果您有改进、新功能或错误修复的想法,请随时在 iris 上提出问题或提交拉取请求

依赖关系

~4–12MB
~110K SLoC