2 个版本

1.0.0-alpha.22023年5月26日
1.0.0-alpha2023年4月30日

337数据库实现

Download history 2/week @ 2024-03-14 21/week @ 2024-03-28 22/week @ 2024-04-04

69 每月下载
用于 eight-serve

BSD-3-Clause

71KB
1.5K SLoC

安装

要将此包添加为依赖项,只需运行

cargo add eight

Eight 库支持嵌入式和客户端使用。您可以通过启用 client 功能来使用官方客户端实现 eight-serve

嵌入式使用

Eight 目前提供了两种默认存储实现。以下是一个文件系统存储的示例

use eight::{embedded::{self, messaging::{Request, Response}, server::Server, storage::filesystem}};
use std::collections::HashMap;

#[tokio::main(flavor = "current_thread")]
async fn main() -> embedded::Result<()> {
    // create filesystem storage
    let storage = filesystem::Storage::from_path("/path/to/store");

    // create new server from storage
    let server = Server::new(storage);

    // start listener in another task
    server.start().await;

    // send a request to server and wait for response
    let response = server.call(Request::Set("pipi".to_string(), "hello world".to_string())).await?;
    assert_eq!(response, Response::Ok);

    // query language usage
    let mut env = HashMap::<String, String>::new();
    env.insert("user".to_string(), "pipi".to_string());

    let results = server.query("
        get $user;
        delete $user;
    ", env).await?;

    assert_eq!(results.len(), 2);
    assert_eq!(results[0], Response::Text("hello world".to_string()));
    assert_eq!(results[1], Response::Ok);

    // clear database before existing
    server.call(Request::Flush).await?;

    Ok(())
}

客户端使用

HTTP 客户端

use eight::client::{self, http, messaging};

#[tokio::main(flavor = "current_thread")]
async fn main() -> client::Result<()> {
    // crate new stateless client
    let client = http::Client::new("http://127.0.0.1:8080");

    // prepare request
    let request = messaging::QueryBuilder::new()
        .add_query("set $key $value;")
        .add_query("get $key;")
        .bind("key", "bob")
        .bind("value", "some value")
        .set_random_id()
        .collect();

    // send request
    let response = client.execute(request).await?;
    assert_eq!(response.results.len(), 2);

    Ok(())
}

WebSocket 客户端

use eight::client::{self, websocket, messaging};

#[tokio::main(flavor = "current_thread")]
async fn main() -> client::Result<()> {
    // connect to websocket
    let client = websocket::Client::connect("ws://127.0.0.1:8080").await?;

    // start message broker
    client.start().await;

    // prepare request
    let request = messaging::QueryBuilder::new()
        .add_query("set $key $value;")
        .add_query("get $key;")
        .bind("key", "bob")
        .bind("value", "some value")
        .set_random_id()
        .collect();

    // send request and wait for response
    let response = client.call(request).await?;
    assert_eq!(response.results.len(), 2);

    Ok(())
}

文档

您可以在 docs.rs 上找到文档。

依赖项

~3–18MB
~211K SLoC