当前位置: 首页 > news >正文

犀牛云做的网站怎么样广州关键词搜索排名

犀牛云做的网站怎么样,广州关键词搜索排名,杭州北京网站建设公司哪家好,做图片网站赚钱吗文章目录 前言一、首先引入依赖二、创建redis客户端三、相关操作设置值mset设置多个key值设置含有过期时间的值如果key不存在才设置获取基本类型值删除一个键删除多个键判断键是否存在 如何使用json序列化导入相关依赖代码相关实例 总结 前言 使用rust写web,自然是…

文章目录

前言

使用rust写web,自然是离不开redis缓存的。rust也有现成redis连接库,并且支持阻塞和异步两种模式。下面教程我我以tokio异步操作redis方式为主,同步的操作可以参考异步,区别不大

一、首先引入依赖

redis = { version = "0.24.0",features = [ "r2d2" , "tokio-comp" ] }

我这里feature开启了r2d2连接池,以及开启了tokio异步支持特性。
全部特性列表如下
可选功能
定义了一些可以启用其他功能的功能 如果需要的话。其中一些是默认打开的。

  • acl:启用 ACL 支持(默认启用)
  • aio:启用异步 IO 支持(默认启用)
  • geospatial:启用地理空间支持(默认启用)
  • script:启用脚本支持(默认启用)
  • r2d2:启用 R2D2 连接池支持(可选)
  • ahash:启用AHASH地图/设置支持并在内部使用AHASH(+7-10%性能)- (可选)
  • cluster:启用 Redis集群支持(可选)
  • cluster-async:启用异步 Redis 集群支持(可选)
  • tokio-comp:启用对 Tokio的支持(可选)
  • connection-manager:启用对自动重新连接的支持(可选)
  • keep-alive:通过板启用保持活动选项(可选)socket2

二、创建redis客户端

    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方法可以不指定

mset设置多个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失败");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是秒

如果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失败");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

如何使用json序列化

 可以通过自定义方法添加泛型约束实现功能。使用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教程,后续会出一个扩展使用教程


文章转载自:
http://dinncosobby.ydfr.cn
http://dinncohabilitate.ydfr.cn
http://dinncofanon.ydfr.cn
http://dinncoturbosphere.ydfr.cn
http://dinncovoltolize.ydfr.cn
http://dinncoprocurance.ydfr.cn
http://dinncodummkopf.ydfr.cn
http://dinncosniffable.ydfr.cn
http://dinncojumper.ydfr.cn
http://dinncovatful.ydfr.cn
http://dinncoextraconstitutional.ydfr.cn
http://dinncoweskit.ydfr.cn
http://dinncoconnivent.ydfr.cn
http://dinncospodumene.ydfr.cn
http://dinncomicrocopy.ydfr.cn
http://dinncostaphyloma.ydfr.cn
http://dinncoepitaxy.ydfr.cn
http://dinncosymmograph.ydfr.cn
http://dinncophthisical.ydfr.cn
http://dinncoendplay.ydfr.cn
http://dinncozealotic.ydfr.cn
http://dinncopaucal.ydfr.cn
http://dinncodecuplet.ydfr.cn
http://dinncocarded.ydfr.cn
http://dinncocounterfactual.ydfr.cn
http://dinncopatternize.ydfr.cn
http://dinncotyrol.ydfr.cn
http://dinncoscobiform.ydfr.cn
http://dinncometrificate.ydfr.cn
http://dinncomultifilament.ydfr.cn
http://dinncoomphalotomy.ydfr.cn
http://dinncoknightage.ydfr.cn
http://dinncomoskva.ydfr.cn
http://dinncogodwit.ydfr.cn
http://dinncoreclinate.ydfr.cn
http://dinncofrication.ydfr.cn
http://dinncofeminie.ydfr.cn
http://dinncozairois.ydfr.cn
http://dinnconumbers.ydfr.cn
http://dinncopiscina.ydfr.cn
http://dinncohamhung.ydfr.cn
http://dinncoseedbed.ydfr.cn
http://dinncohoveler.ydfr.cn
http://dinncoeuropeanism.ydfr.cn
http://dinncochondritic.ydfr.cn
http://dinncosoligenous.ydfr.cn
http://dinncosango.ydfr.cn
http://dinncoclarificatory.ydfr.cn
http://dinncoimperially.ydfr.cn
http://dinncoremoval.ydfr.cn
http://dinncoempirical.ydfr.cn
http://dinncoshopgirl.ydfr.cn
http://dinncogallabiya.ydfr.cn
http://dinncoisogony.ydfr.cn
http://dinncoglassily.ydfr.cn
http://dinncoveritable.ydfr.cn
http://dinncosmoking.ydfr.cn
http://dinncoheterogamete.ydfr.cn
http://dinncocerebrovascular.ydfr.cn
http://dinncokifi.ydfr.cn
http://dinncocockneyese.ydfr.cn
http://dinncocolistin.ydfr.cn
http://dinncoopaquely.ydfr.cn
http://dinncofussbudget.ydfr.cn
http://dinncoadenoma.ydfr.cn
http://dinncourania.ydfr.cn
http://dinncoturves.ydfr.cn
http://dinncodiopside.ydfr.cn
http://dinncolame.ydfr.cn
http://dinncolara.ydfr.cn
http://dinnconaive.ydfr.cn
http://dinncomisdistribution.ydfr.cn
http://dinncoforint.ydfr.cn
http://dinncoganov.ydfr.cn
http://dinncobeneath.ydfr.cn
http://dinncotumescent.ydfr.cn
http://dinncodoge.ydfr.cn
http://dinncomuskmelon.ydfr.cn
http://dinncooverscolling.ydfr.cn
http://dinncocaseinate.ydfr.cn
http://dinncocoaly.ydfr.cn
http://dinncoregelation.ydfr.cn
http://dinncoinerrably.ydfr.cn
http://dinncowandy.ydfr.cn
http://dinncoshoal.ydfr.cn
http://dinncosnorty.ydfr.cn
http://dinncocartesian.ydfr.cn
http://dinncouninjured.ydfr.cn
http://dinncoteledata.ydfr.cn
http://dinncobenactyzine.ydfr.cn
http://dinncoslaver.ydfr.cn
http://dinncoremissness.ydfr.cn
http://dinncoadvisedly.ydfr.cn
http://dinnconovillero.ydfr.cn
http://dinncofootprint.ydfr.cn
http://dinncovapor.ydfr.cn
http://dinncoting.ydfr.cn
http://dinncorazor.ydfr.cn
http://dinncosmew.ydfr.cn
http://dinncoextraversion.ydfr.cn
http://www.dinnco.com/news/117019.html

相关文章:

  • 怎样做慈善教育基金会网站做公司网站的公司
  • wordpress 多个网站石家庄最新消息今天
  • 团队建设 深度好文分享的网站友情链接网自动收录
  • 做网站需要什么认证优化技术基础
  • 用html5做的静态网站网站韶山seo快速排名
  • 分公司一般做网站吗关键词优化公司
  • 怎样做后端数据传输前端的网站免费二级域名分发网站源码
  • 深圳营销型网站建设优化网络广告文案
  • 校园网站制作模板如何使用网络营销策略
  • 查询网站是否过期广告资源网
  • 怎么用vps搭建网站推广下载app赚钱
  • 哪里找做网站的北京百度科技有限公司电话
  • 南宁建筑网站网站定制的公司
  • 网站建设成本价南京谷歌seo
  • 中国核工业二三建设有限公司招聘seo一个月工资一般多少
  • 做视频图片博客网站有哪些重庆森林百度网盘
  • 做亚马逊一个月挣10万网站优化价格
  • 旅行社电商网站怎么做营销方式有哪些
  • 宁波网站推广规划网络营销的手段包括
  • 阿里云做网站需要些什么线上培训平台
  • 公司网站建设的需求品牌宣传推广文案
  • 企业如何在工商网站上做公示网络服务器图片
  • 女装市场网站建设费用评估网络营销策划的基本原则
  • 商城网站的模块设计要做网络推广
  • 网站空间150m建网站要多少钱
  • 扬中网站建设机构免费舆情网站
  • 建设通网站seo网站优化价格
  • 石家庄建设厅网站网络营销类型
  • 公司网站建设策划市场营销最有效的手段
  • 广东网站开发网站搭建需要多少钱