广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >在Rust web服务中使用Redis的方法
  • 787
分享到

在Rust web服务中使用Redis的方法

2024-04-02 19:04:59 787人浏览 八月长安
摘要

Redis一直是网络生态系统的重要组成部分,它经常用作缓存、消息代理或简单地用作数据存储。 在这篇文章中,我们将演示如何在一个Rust WEB应用程序中使用Redis。 我们将探索两

Redis一直是网络生态系统的重要组成部分,它经常用作缓存、消息代理或简单地用作数据存储。

在这篇文章中,我们将演示如何在一个Rust WEB应用程序中使用Redis。

我们将探索两种种使用Redis的方法:

  • 使用同步连接池

  • 使用异步连接池

对于同步池,我们使用基于r2d2库的r2d2-redis。我们在异步解决方案中使用mobc,还有许多其他异步连接池,如deadpool和bb8,它们都以类似的方式工作。

话不多说,让我们开始吧!

新建一个项目

carGo new rust-redis-web-example

在Cargo.toml中加入依赖:

[dependencies]</code>
<code>tokio = { version = "1.19", features = ["full"] }</code>
<code>warp = "0.3.2"</code>
<code>redis = "0.21"</code>
<code>r2d2_redis = "0.14"</code>
<code>mobc-redis = "0.7"</code>
<code>mobc = "0.7"</code>
<code>thiserror = "1.0"

首先,让我们设置一些共享类型,在main.rs中:

type WebResult= std::result::Result;</code>
<code>type Result= std::result::Result;</code>
<code>const REDIS_CON_STRING: &str = "redis://127.0.0.1/";

定义这两个Result类型是为了节省一些输入,并表示内部Errors (Result)和外部Errors (WebResult)。

接下来,定义这个内部error类型并为其实现Reject,以便它可以处理从程序返回的Http错误。

#[derive(Error, Debug)]</code>
<code>pub enum Error {</code>
<code>    #[error("mobc error: {0}")]</code>
<code>    MobcError(#[from] MobcError),</code>
<code>    #[error("r2d2 error: {0}")]</code>
<code>    R2D2Error(#[from] R2D2Error),</code>
<code>}</code>
<code>#[derive(Error, Debug)]</code>
<code>pub enum MobcError {</code>
<code>    #[error("could not get redis connection from pool : {0}")]</code>
<code>    RedisPoolError(mobc::Error),</code>
<code>    #[error("error parsing string from redis result: {0}")]</code>
<code>    RedisTypeError(mobc_redis::redis::RedisError),</code>
<code>    #[error("error executing redis command: {0}")]</code>
<code>    RedisCMDError(mobc_redis::redis::RedisError),</code>
<code>    #[error("error creating Redis client: {0}")]</code>
<code>    RedisClientError(mobc_redis::redis::RedisError),</code>
<code>}</code>
<code>#[derive(Error, Debug)]</code>
<code>pub enum R2D2Error {</code>
<code>    #[error("could not get redis connection from pool : {0}")]</code>
<code>    RedisPoolError(r2d2_redis::r2d2::Error),</code>
<code>    #[error("error parsing string from redis result: {0}")]</code>
<code>    RedisTypeError(r2d2_redis::redis::RedisError),</code>
<code>    #[error("error executing redis command: {0}")]</code>
<code>    RedisCMDError(r2d2_redis::redis::RedisError),</code>
<code>    #[error("error creating Redis client: {0}")]</code>
<code>    RedisClientError(r2d2_redis::redis::RedisError),</code>
<code>}</code>
<code>impl warp::reject::Reject for Error {}

上面定义了通用的错误类型和我们将实现的每一种使用Redis方法的错误类型。错误本身只是处理连接、池的创建和命令执行等 错误。

使用r2d2(同步)

r2d2 crate是第一个被广泛使用的连接池,它现在仍然被广泛使用。Redis的连接池是r2d2-redis crate。

在src目录下创建r2d2_pool.rs文件,因为我们现在使用的是连接池,所以这个池的创建也需要在r2d2模块中处理。

use crate::{R2D2Error::*, Result, REDIS_CON_STRING};</code>
<code>use r2d2_redis::redis::{Commands, FromRedisValue};</code>
<code>use r2d2_redis::{r2d2, RedisConnectionManager};</code>
<code>use std::time::Duration;</code>
<code>pub type R2D2Pool = r2d2::Pool;</code>
<code>pub type R2D2Con = r2d2::PooledConnection;</code>
<code>const CACHE_POOL_MAX_OPEN: u32 = 16;</code>
<code>const CACHE_POOL_MIN_IDLE: u32 = 8;</code>
<code>const CACHE_POOL_TIMEOUT_SECONDS: u64 = 1;</code>
<code>const CACHE_POOL_EXPIRE_SECONDS: u64 = 60;</code>
<code>pub fn connect() -> Result> {</code>
<code>    let manager = RedisConnectionManager::new(REDIS_CON_STRING).map_err(RedisClientError)?;</code>
<code>    r2d2::Pool::builder()</code>
<code>        .max_size(CACHE_POOL_MAX_OPEN)</code>
<code>        .max_lifetime(Some(Duration::from_secs(CACHE_POOL_EXPIRE_SECONDS)))</code>
<code>        .min_idle(Some(CACHE_POOL_MIN_IDLE))</code>
<code>        .build(manager)</code>
<code>        .map_err(|e| RedisPoolError(e).into())</code>
<code>}

定义一些常量来配置池,如打开和空闲连接,连接超时和连接的生命周期,池本身是使用RedisConnectionManager创建的,传递给它的参数是redis连接字符串

不要太担心配置值,大多数连接池都有一些缺省值,这些缺省值将适用于基本应用程序。

我们需要一种方法来获得连接池,然后向Redis设置和获取值。

pub fn get_con(pool: &R2D2Pool) -> Result{</code>
<code>    pool.get_timeout(Duration::from_secs(CACHE_POOL_TIMEOUT_SECONDS))</code>
<code>        .map_err(|e| {</code>
<code>            eprintln!("error connecting to redis: {}", e);</code>
<code>            RedisPoolError(e).into()</code>
<code>        })</code>
<code>}</code>
<code>pub fn set_str(pool: &R2D2Pool, key: &str, value: &str, ttl_seconds: usize) -> Result<()> {</code>
<code>    let mut con = get_con(&pool)?;</code>
<code>    con.set(key, value).map_err(RedisCMDError)?;</code>
<code>    if ttl_seconds > 0 {</code>
<code>        con.expire(key, ttl_seconds).map_err(RedisCMDError)?;</code>
<code>    }</code>
<code>    Ok(())</code>
<code>}</code>
<code>pub fn get_str(pool: &R2D2Pool, key: &str) -> Result{</code>
<code>    let mut con = get_con(&pool)?;</code>
<code>    let value = con.get(key).map_err(RedisCMDError)?;</code>
<code>    FromRedisValue::from_redis_value(&value).map_err(|e| RedisTypeError(e).into())</code>
<code>}

我们尝试从池中获取连接,并配置超时时间。在set_str和get_str中,每次调用这些函数时都会调用get_con。

使用mobc(异步)

在src目录下创建r2d2_pool.rs文件,让我们定义配置并创建连接池。

use crate::{MobcError::*, Result, REDIS_CON_STRING};</code>
<code>use mobc::{Connection, Pool};</code>
<code>use mobc_redis::redis::{AsyncCommands, FromRedisValue};</code>
<code>use mobc_redis::{redis, RedisConnectionManager};</code>
<code>use std::time::Duration;</code>
<code>pub type MobcPool = Pool;</code>
<code>pub type MobcCon = Connection;</code>
<code>const CACHE_POOL_MAX_OPEN: u64 = 16;</code>
<code>const CACHE_POOL_MAX_IDLE: u64 = 8;</code>
<code>const CACHE_POOL_TIMEOUT_SECONDS: u64 = 1;</code>
<code>const CACHE_POOL_EXPIRE_SECONDS: u64 = 60;</code>
<code>pub async fn connect() -> Result{</code>
<code>    let client = redis::Client::open(REDIS_CON_STRING).map_err(RedisClientError)?;</code>
<code>    let manager = RedisConnectionManager::new(client);</code>
<code>    Ok(Pool::builder()</code>
<code>        .get_timeout(Some(Duration::from_secs(CACHE_POOL_TIMEOUT_SECONDS)))</code>
<code>        .max_open(CACHE_POOL_MAX_OPEN)</code>
<code>        .max_idle(CACHE_POOL_MAX_IDLE)</code>
<code>        .max_lifetime(Some(Duration::from_secs(CACHE_POOL_EXPIRE_SECONDS)))</code>
<code>        .build(manager))</code>
<code>}

这和r2d2非常相似,这不是巧合;许多连接池库都从r2d2出色的api中获得了灵感。

async fn get_con(pool: &MobcPool) -> Result{</code>
<code>    pool.get().await.map_err(|e| {</code>
<code>        eprintln!("error connecting to redis: {}", e);</code>
<code>        RedisPoolError(e).into()</code>
<code>    })</code>
<code>}</code>
<code>pub async fn set_str(pool: &MobcPool, key: &str, value: &str, ttl_seconds: usize) -> Result<()> {</code>
<code>    let mut con = get_con(&pool).await?;</code>
<code>    con.set(key, value).await.map_err(RedisCMDError)?;</code>
<code>    if ttl_seconds > 0 {</code>
<code>        con.expire(key, ttl_seconds).await.map_err(RedisCMDError)?;</code>
<code>    }</code>
<code>    Ok(())</code>
<code>}</code>
<code>pub async fn get_str(pool: &MobcPool, key: &str) -> Result{</code>
<code>    let mut con = get_con(&pool).await?;</code>
<code>    let value = con.get(key).await.map_err(RedisCMDError)?;</code>
<code>    FromRedisValue::from_redis_value(&value).map_err(|e| RedisTypeError(e).into())</code>
<code>}

现在看起来应该很熟悉了,传入池并在开始时获取连接,但这一次采用异步方式,使用async和await。

下一步就是把它们结合到一个warp web应用中,修改main.rs:

use std::convert::Infallible;</code>
<code>use mobc_pool::MobcPool;</code>
<code>use r2d2_pool::R2D2Pool;</code>
<code>use thiserror::Error;</code>
<code>use warp::{Rejection, Filter, Reply};</code>
<code>mod r2d2_pool;</code>
<code>mod mobc_pool;</code>
<code>type WebResult= std::result::Result;</code>
<code>type Result= std::result::Result;</code>
<code>const REDIS_CON_STRING: &str = "redis://127.0.0.1/";</code>
<code>#[tokio::main]</code>
<code>async fn main() {</code>
<code>    let mobc_pool = mobc_pool::connect().await.expect("can create mobc pool");</code>
<code>    let r2d2_pool = r2d2_pool::connect().expect("can create r2d2 pool");</code>
<code>    let mobc_route = warp::path!("mobc")</code>
<code>        .and(with_mobc_pool(mobc_pool.clone()))</code>
<code>        .and_then(mobc_handler);</code>
<code>    let r2d2_route = warp::path!("r2d2")</code>
<code>        .and(with_r2d2_pool(r2d2_pool.clone()))</code>
<code>        .and_then(r2d2_handler);</code>
<code>    let routes = mobc_route.or(r2d2_route);</code>
<code>    warp::serve(routes).run(([0, 0, 0, 0], 8080)).await;</code>
<code>}</code>
<code>fn with_mobc_pool(</code>
<code>    pool: MobcPool,</code>
<code>) -> impl Filter+ Clone {</code>
<code>    warp::any().map(move || pool.clone())</code>
<code>}</code>
<code>fn with_r2d2_pool(</code>
<code>    pool: R2D2Pool,</code>
<code>) -> impl Filter+ Clone {</code>
<code>    warp::any().map(move || pool.clone())</code>
<code>}</code>
<code>async fn mobc_handler(pool: MobcPool) -> WebResult{</code>
<code>    mobc_pool::set_str(&pool, "mobc_hello", "mobc_world", 60)</code>
<code>        .await</code>
<code>        .map_err(|e| warp::reject::custom(e))?;</code>
<code>    let value = mobc_pool::get_str(&pool, "mobc_hello")</code>
<code>        .await</code>
<code>        .map_err(|e| warp::reject::custom(e))?;</code>
<code>    Ok(value)</code>
<code>}</code>
<code>async fn r2d2_handler(pool: R2D2Pool) -> WebResult{</code>
<code>    r2d2_pool::set_str(&pool, "r2d2_hello", "r2d2_world", 60)</code>
<code>        .map_err(|e| warp::reject::custom(e))?;</code>
<code>    let value = r2d2_pool::get_str(&pool, "r2d2_hello").map_err(|e| warp::reject::custom(e))?;</code>
<code>    Ok(value)</code>
<code>}

Docker启动一个本地Redis实例:

docker run -p 6379:6379 redis

接下来,运行 cargo run。

使用curl测试

curl http://localhost:8080/r2d2</code>
<code>curl http://localhost:8080/mobc

到此这篇关于在Rust web服务中使用Redis的文章就介绍到这了,更多相关Rust 使用Redis内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 在Rust web服务中使用Redis的方法

本文链接: https://www.lsjlt.com/news/166475.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 在Rust web服务中使用Redis的方法
    Redis一直是网络生态系统的重要组成部分,它经常用作缓存、消息代理或简单地用作数据存储。 在这篇文章中,我们将演示如何在一个Rust web应用程序中使用Redis。 我们将探索两...
    99+
    2022-11-13
  • 在Rust web服务中使用Redis的方法
    Redis一直是网络生态系统的重要组成部分,它经常用作缓存、消息代理或简单地用作数据存储。 在这篇文章中,我们将演示如何在一个Rust web应用程序中使用Redis。 我们将探索两种种使用Redis的方法: 使用同步连...
    99+
    2022-08-31
  • redis中django-redis的使用方法
    本篇文章主要探讨redis中django-redis的使用方法。有一定的参考价值,有需要的朋友可以参考一下,跟随小编一起来看使用方法吧。自定义连接池这种方式跟普通py文件操作redis一样,代码如下view...
    99+
    2022-10-18
  • 在Node.js应用中使用Redis的方法简介
    在开始本文之前请确保安装好 Redis 和 Node.js 以及 Node.js 的 Redis 扩展 —— node_redis 首先创建一个新文件夹并新建文本文件 app.js 文件内容如下: ...
    99+
    2022-06-04
    简介 方法 Node
  • spring中redis的使用方法
    这篇文章将为大家详细讲解有关spring中redis的使用方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。spring中redis怎么用在Spring中使用RedisJ...
    99+
    2022-10-18
  • Redis在项目中的使用方法有哪些
    本篇内容介绍了“Redis在项目中的使用方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!springboot中redis相关配置po...
    99+
    2023-06-22
  • Redis中的Redis集群和PHP的使用方法
    Redis是一款强大的内存键值对存储数据库。与常规的RDBMS(关系型数据库管理系统)相比,它具有更高的性能和更好的伸缩性。Redis的优点之一是它可以作为分布式系统的核心技术。在这篇文章中,我们将探讨Redis集群的概念以及如何在PHP中...
    99+
    2023-05-15
    PHP 使用方法 Redis集群
  • springboot整合使用云服务器上的Redis方法
    目录一、前提条件二、整合步骤三、可能遇到的坑一、前提条件 修改Redis.conf配置文件 1、protected-mode yes(默认的) 修改成 protected-mode no,解除保护模式 2、注释掉绑定i...
    99+
    2022-09-30
  • Redis中Scan命令的使用方法
    小编给大家分享一下Redis中Scan命令的使用方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!Redis中有一个经典的问题,在巨大的数据量的情况下,做类似于查找符合某种规则的Key的信息,...
    99+
    2022-10-18
  • redis中的incr方法怎么使用
    Redis中的INCR方法用于将指定 key 的值加上给定的增量值。如果 key 不存在,则在执行命令前,将 key 的值设置为 0...
    99+
    2023-08-24
    redis incr
  • Redis在项目中的使用(JedisPool方式)
    springboot中redis相关配置 1、pom.xml中引入依赖 <dependency> <groupId>redis.clients<...
    99+
    2022-11-12
  • redis中链表命令的使用方法
    这篇文章将为大家详细讲解有关redis中链表命令的使用方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一、概述:list是一个链表结构,主要功能是push、pop、获取...
    99+
    2022-10-18
  • Redis中的BloomFilter简介及使用方法
    本篇内容主要讲解“Redis中的BloomFilter简介及使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Redis中的BloomFilter简介及使用...
    99+
    2022-10-19
  • redis中opsForList().range()的使用方法详解
    目录结论(具体测试数据请往下看)1、环境 Redis2、测试代码:3、测试数据(假设List长度为N)4、测试从索引倒数开始4.1、stringRedisTemplate.opsForList().range(key, ...
    99+
    2023-03-15
    redis opsforList().range() opsforlist().range
  • Redis事务处理的使用操作方法
    一、简介         Redis采用了乐观锁方式进行事务控制,它使用watch命令监视给定的key...
    99+
    2022-11-12
  • Jedis使用Redis事务的方法是什么
    本篇内容主要讲解“Jedis使用Redis事务的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Jedis使用Redis事务的方法是什么”吧!什么是Jedis什么是Jedis 是Redi...
    99+
    2023-07-05
  • 在Redis集群中使用pipeline批量插入的实现方法
    由于项目中需要使用批量插入功能, 所以在网上查找到了Redis 批量插入可以使用pipeline来高效的插入, 示例代码如下: String key = "key"; Jedis jedis = new...
    99+
    2022-10-18
  • PHP和Redis中的LUA脚本使用方法
    PHP和Redis中的LUA脚本使用方法LUA 是一种轻量级的脚本语言,旨在提供高效的嵌入式扩展功能。Redis 是一种开源的 NoSQL 数据库,提供高效的键值存储和缓存功能。在 Redis 中使用 LUA 脚本可以大大提高数据处理效率和...
    99+
    2023-05-15
    PHP redis Lua脚本
  • Springboot项目中使用redis的配置方法
    小编给大家分享一下Springboot项目中使用redis的配置方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!springboot是什么springboot一...
    99+
    2023-06-14
  • Redis 基础教程之事务的使用方法
    Redis 基础教程之事务的使用方法 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会...
    99+
    2022-06-04
    使用方法 基础教程 事务
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作