示例介绍

示例1:借助Redis实现购物车功能

示例2:Redis实现网页缓存和数据缓存

借助Redis实现购物车功能

每个用户的购物车都是一个散列,散列存储了商品ID与商品订购数量之间的映射。订购商品时,对购物车进行更新:如果用户订购某件商品的数量大于0,那么将这件商品的ID以及用户订购该商品的数量添加到散列里面,如果用户购买的商品已经存在散列里面,那么新的订购数量会覆盖已有的订购数量;相反,如果用户订购某件商品数量不大于0,那么程序将从散列里面移除该条目。

由于整个示例分Controller和WebApi两部分,在不考虑封装Redis操作的情况下提取Redis连接字符串到appSetting.json中

"ConnectionStrings": {
"Redis": "localhost,abortConnect=false"
},

保存用户购物查数据示例代码:

[HttpGet("UpdateCart")]
public string UpdateCart(string token,string item,int count)
{
var key = $"cart:{token}";
using (var redis = ConnectionMultiplexer.Connect(redisConnectionStr))
{
var db = redis.GetDatabase();
//更新购物车数据
if (count <= 0)
{
db.HashDeleteAsync(key, item);
}
else
{
db.HashSetAsync(key, item,count);
}
} return "更新购物车成功";
}

Redis实现网页缓存和数据缓存

针对不会频繁更新得动态网页或Ajax返回的数据可以缓存在Redis中,减少与数据库的交互提升系统效应效率。

该示例只是为了演示借助Reids实现页面缓存和数据缓存效果,实际使用中可通过ResponseCache特性实现缓存。

详细介绍:https://docs.microsoft.com/en-us/aspnet/core/performance/caching/response?view=aspnetcore-5.0

网页数据缓存

新建示例项目时为WebApi项目,修改Startup类添加Web Mvc支持。

ConfigureServices方法中修改services.AddControllers()services.AddControllersWithViews()

Configure方法中app.UseEndpoints修改为如下代码

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});

定义中间件类RedisCacheMiddleware,用来缓存网页页面

public class RedisCacheMiddleware
{ private readonly RequestDelegate _next; public IConfiguration Configuration { get; } public RedisCacheMiddleware(RequestDelegate next, IConfiguration configuration)
{
_next = next;
Configuration = configuration;
} public async Task InvokeAsync(HttpContext context)
{ var path = context.Request.Path.Value.ToLower(); if (path == "/home" || path == "/home/index")
{ var responseContent = ""; //Copy a pointer to the original response body stream
var originalBodyStream = context.Response.Body; using (var redis = ConnectionMultiplexer.Connect(Configuration.GetConnectionString("Redis")))
{
var db = redis.GetDatabase();
if (db.KeyExists(path))
{
responseContent = db.StringGet(path);
await RespondWithIndexHtml(context.Response, responseContent);
return;
}
else
{
using (var responseBody = new MemoryStream())
{
//...and use that for the temporary response body
context.Response.Body = responseBody; //Continue down the Middleware pipeline, eventually returning to this class
await _next(context); //Format the response from the server
responseContent = await FormatResponse(context.Response); //Copy the contents of the new memory stream (which contains the response) to the original stream, which is then returned to the client.
await responseBody.CopyToAsync(originalBodyStream);
} db.StringSet(path, responseContent, expiry: TimeSpan.FromSeconds(30));
}
} } // Call the next delegate/middleware in the pipeline
await _next(context);
} private async Task RespondWithIndexHtml(HttpResponse response,string html)
{
response.StatusCode = 200;
response.ContentType = "text/html"; await response.WriteAsync(html, Encoding.UTF8);
} private async Task<string> FormatResponse(HttpResponse response)
{
//We need to read the response stream from the beginning...
response.Body.Seek(0, SeekOrigin.Begin); //...and copy it into a string
string text = await new StreamReader(response.Body).ReadToEndAsync(); //We need to reset the reader for the response so that the client can read it.
response.Body.Seek(0, SeekOrigin.Begin); //Return the string for the response
return text;
} }

添加中间件调用

app.UseMiddleware<RedisCacheMiddleware>();

运行效果如图:

网页上的时间会每30秒更新一次。

.Net Core提供的响应缓存中间件:https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/middleware?view=aspnetcore-5.0

Ajax数据缓存

本例只是简单的数据存储,实际使用中可编写单独的服务,让这个服务将指定的数据缓存到Redis里面,并不定期地对这些缓存进行更新。

示例代码

[HttpGet("GetDateTime")]
public string GetDateTime()
{
var dateTime = "";
var key = "data:datetime";
using (var redis = ConnectionMultiplexer.Connect(redisConnectionStr))
{
var db = redis.GetDatabase();
if (db.KeyExists(key))
{
dateTime = db.StringGet(key);
}
else
{
dateTime = DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss");
db.StringSet(key, dateTime,expiry:TimeSpan.FromSeconds(5));
}
} return dateTime;
}

参考资料

Using Middleware in .NET 5.0 to Log Requests and Responses

如何在 asp.net core 的中间件中返回具体的页面

Swashbuckle.AspNetCore.ReDoc/ReDocMiddleware.cs

.Net Redis实战——使用Redis构建Web应用的更多相关文章

  1. Redis实战之Redis + Jedis

    用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET 等.基于这些限制,有必要考虑Redis! 相关链接: Redis实战 Redis实战之Redi ...

  2. Redis实战之Redis + Jedis[转]

    http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报   目录(?)[-] ...

  3. Redis实战:如何构建类微博的亿级社交平台

    微博及 Twitter 这两大社交平台都重度依赖 Redis 来承载海量用户访问.本文介绍如何使用 Redis 来设计一个社交系统,以及如何扩展 Redis 让其能够承载上亿用户的访问规模. 虽然单台 ...

  4. 实战案例--Grunt构建Web程序

    GruntJS构建Web程序.使用Gruntjs来搭建一个前端项目,然后使用grunt合并,压缩JS文件,熟练了node.js安装和grunt.js安装后,接下来来实战一个案例,案例是根据snandy ...

  5. Redis实战总结-Redis的高可用性

    在之前的博客<Redis实战总结-配置.持久化.复制>给出了一种Redis主从复制机制,简单地实现了Redis高可用.然后,如果Master服务器宕机,会导致整个Redis瘫痪,这种方式的 ...

  6. Redis 实战 —— 05. Redis 其他命令简介

    发布与订阅 P52 Redis 实现了发布与订阅(publish/subscribe)模式,又称 pub/sub 模式(与设计模式中的观察者模式类似).订阅者负责订阅频道,发送者负责向频道发送二进制字 ...

  7. Redis 实战 —— 14. Redis 的 Lua 脚本编程

    简介 Redis 从 2.6 版本开始引入使用 Lua 编程语言进行的服务器端脚本编程功能,这个功能可以让用户直接在 Redis 内部执行各种操作,从而达到简化代码并提高性能的作用. P248 在不编 ...

  8. 分布式缓存技术redis学习系列(五)——redis实战(redis与spring整合,分布式锁实现)

    本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...

  9. 分布式缓存技术redis系列(五)——redis实战(redis与spring整合,分布式锁实现)

    本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...

随机推荐

  1. Nest 中处理 XML 类型的请求与响应

    公众号及小程序的微信接口是通过 xml 格式进行数据交换的. 比如接收普通消息的接口: 当普通微信用户向公众账号发消息时,微信服务器将 POST 消息的 XML 数据包到开发者填写的 URL 上. - ...

  2. aws EKS

    登陆aws账号 1)找到eks 相关的项目,并进入 2)填写集群的名称,然后下一步 3)集群设置页面,添加集群服务角色 (aws eks cluster role) 4)继续集群配置 5)集群创建完成 ...

  3. 1.8.5- input按钮组

  4. PAT 乙级 -- 1007 -- 素数对猜想

    题目简述 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数."素数对猜想"认为"存在无穷 ...

  5. POJ1376简单广搜

    题意:       给你一个n*m的矩阵,然后给你机器人的起点和终点,还有起点的方向,然后每次机器人有两种操作,左右旋转90度,或者是朝着原来的方向走1,2或者3步,机器人再走的过程中不能碰到格子,也 ...

  6. [CTF]栅栏密码

    [CTF]栅栏密码 ---------------------  作者:adversity`  来源:CSDN  原文:https://blog.csdn.net/qq_40836553/articl ...

  7. 【转】风控中的特征评价指标(二)——PSI

    转自:https://zhuanlan.zhihu.com/p/79682292 风控业务背景 在风控中,稳定性压倒一切.原因在于,一套风控模型正式上线运行后往往需要很久(通常一年以上)才会被替换下线 ...

  8. Linux下查看在线用户及用户进程

    #该服务器下的所有用户运行进程的情况 ps -ax -u #查看java程序下用户的进程情况 ps -ax -u |grep java   或  ps aux|grep java cat /etc/p ...

  9. 10个 解放双手的 IDEA 插件,这些代码都不用写(第二弹)

    本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 鸽了很久没发文,不写文章的日子真的好惬意,每天也不用愁着写点什 ...

  10. 使用C#操作注册表

    这节讲一下使用C#操作注册表. 首先来了解一下,什么是注册表,注册表是Windows中特有的一个东西,百度百科中对其解释如下:Windows注册表(Registry)实质上是一个庞大的数据库,它存储着 ...