使用rust写web,自然是离不开redis缓存的。rust也有现成redis连接库,并且支持阻塞和异步两种模式。下面教程我我以tokio异步操作redis方式为主,同步的操作可以参考异步,区别不大
redis = { version = "0.24.0",features = [ "r2d2" , "tokio-comp" ] }
我这里feature开启了r2d2连接池,以及开启了tokio异步支持特性。
全部特性列表如下
可选功能
定义了一些可以启用其他功能的功能 如果需要的话。其中一些是默认打开的。
let client = redis::Client::open("redis://127.0.0.1:6379/").unwrap();
//获取连接
let con = client.get_async_connection().await.expect("连接redis失败");
此处url的格式为redis://[][:@][:port][/]
db是redis数据库索引。可根据业务自行修改
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
con.set::<&str,u32,()>("my_key", 12).await.expect("操作失败");
}
此处<&str,u32,()>前两个泛型指定key,value。第三个泛型是实现了FromRedisValue Trait的返回,set方法可以不指定
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
con.mset::<&str,u32,()>(&[("my_key1", 100), ("my_key2", 200)]).await.expect("操作失败");
}
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
//设置60秒过期,第三个参数是过期时间
con.pset_ex::<&str,u32,()>("my_key", 12,60*1000).await.expect("操作失败");
}
pset_ex是毫秒单位,set_ex是秒
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
con.set_nx::<&str,u32,()>("key3",30).await.expect("操作失败");
}
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
let value : u32 = con.get("my_key").await.expect("获取值失败");
println!("my_key = {}", value);
}
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
con.del::<&str, ()>("key3") .await.expect("删除redis key失败");
}
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
// 多个键删除
let keys_to_delete = vec!["my_key1", "my_key2"];
let result = con.del::<&Vec<&str>, i32>(&keys_to_delete).await;
match result {
Ok(count) => println!("Deleted {} keys", count),
Err(e) => println!("Error: {}", e),
}
}
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
// 判断键是否存在
let exists : bool = con.exists("my_key").await.expect("执行redis命令失败");
println!("exists: {}", exists);
}
更多详细的接口文档可以看官网AsyncCommands接口https://docs.rs/redis/0.24.0/redis/trait.AsyncCommands.html
可以通过自定义方法添加泛型约束实现功能。使用serde_json库来进行序列化
[dependencies]
tokio = { version = "1.35.1", features = ["full"] }
redis = { version = "0.24.0",features = [ "r2d2" , "tokio-comp","json" ] }
serde_json = "1.0.111"
serde = { version = "1.0.195", features = ["derive"] }
use redis::{aio, AsyncCommands, RedisError, RedisResult, ToRedisArgs};
use serde::{Deserialize, Serialize};
use serde::de::DeserializeOwned;
#[derive(Debug, Clone,Deserialize, Serialize)]
struct User{
name: String,
age: u8,
email: String,
}
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379/10").unwrap();
let mut con = client.get_async_connection().await.expect("连接redis失败");
// 判断键是否存在
let user = User{
name: "zhangsan".to_string(),
age: 18,
email: "11111111".to_string(),
};
set_json(&mut con,"user1",&user).await.expect("设置失败");
let user :User = get_json(&mut con, "user2").await.expect("获取失败,没有找到User");
println!("user:{:?}",user);
}
//设置json格式对象
pub async fn set_json<K,T>(con: &mut aio::Connection, key: K, obj : &T) -> RedisResult<String>
where K: ToRedisArgs + Send + Sync, T: Serialize{
let json = serde_json::to_string(obj);
match json {
Ok(obj) => {
con.set::<K,String, String>(key,obj).await
},
Err(e) => {return Err(RedisError::from(e));}
}
}
//获得json格式对象
pub async fn get_json<K, T>(con: &mut aio::Connection, key: K) -> RedisResult<T>
where K: ToRedisArgs + Send + Sync, T: DeserializeOwned{
let json : String = con.get(key).await?;
let result = serde_json::from_str(&json);
return match result {
Ok(obj) => { Ok(obj) },
Err(e) => { Err(RedisError::from(e)) }
}
}
通过这两个方法泛型约束据即可实现相关功能
以上就是今天要讲的内容,本文介绍了rust语言tokio异步使用redis教程,后续会出一个扩展使用教程