9 个版本 (1 个稳定版)
1.0.0 | 2023 年 10 月 13 日 |
---|---|
0.2.5 | 2023 年 10 月 7 日 |
0.1.3 | 2023 年 10 月 5 日 |
#2323 在 数据库接口
83 次每月下载
75KB
1.5K SLoC
Quick-KV
现代软件的可靠键值存储
功能
- 基于二进制的数据存储
- 支持 Serde 数据类型
- 线程安全
链接
安装
cargo add quick-kv
示例
use quick_kv::prelude::*;
fn main() -> anyhow::Result<()>
{
let mut client = QuickClient::<String>::new(None);
client.get("star this repo")?;
Ok(())
}
CLI (Beta)
Quick-KV 随附用于与数据库交互的 REPL。
要安装 CLI,请运行以下命令
cargo install quick-kv
这与 cargo add
命令不同,因为它全局安装 CLI,允许您将其作为可执行文件使用。
lib.rs
:
QuickKV 是一个简单、快速、易于使用的键值存储。
功能
- 简单 API
- 内置日志记录
- I/O 缓存
- CLI 工具(Beta)
有用的链接
示例
use std::io;
use quick_kv::prelude::*;
use rand::Rng;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
struct GuessingGame
{
tries: u32,
incorrect_guesses: Vec<u32>,
}
fn main() -> anyhow::Result<()>
{
let mut client = QuickClient::<GuessingGame>::new(ClientConfig::default());
// First, we create a new game.
let game = GuessingGame {
tries: 0,
incorrect_guesses: vec![],
};
// Set the game in the client.
client.set("game", game.clone())?;
println!("Welcome to the Number Guessing Game!");
println!("You have 5 attempts to guess a number between 1 and 50.");
let secret_number = rand::thread_rng().gen_range(1..50);
loop {
let game_state = client.get("game")?.unwrap();
let remaining_attempts = 5 - game_state.tries;
if remaining_attempts == 0 {
println!("Sorry, you have lost the game!");
println!("The secret number was {}.", secret_number);
client.delete("game")?;
break;
}
println!("Attempts left: {}", remaining_attempts);
println!("Please input your guess:");
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Failed to read line");
if guess.trim().to_lowercase() == "quit" {
println!("Thanks for playing!");
break;
}
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Please enter a valid number!");
continue;
}
};
if guess < 1 || guess > 50 {
println!("The secret number is between 1 and 50.");
continue;
}
if guess == secret_number {
println!("Congratulations! You guessed the correct number!");
client.delete("game")?;
break;
} else if guess < secret_number {
println!("Sorry, your guess was too low. Try a higher number!");
} else {
println!("Sorry, your guess was too high. Try a lower number!");
}
let updated_game = GuessingGame {
tries: game_state.tries + 1,
incorrect_guesses: game_state.incorrect_guesses.clone(),
};
client.update("game", updated_game, None)?;
}
Ok(())
}
更多示例可以在 examples 目录中找到。
依赖项
~5–16MB
~161K SLoC