示例介绍

示例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. Java JFR 民间指南 - 事件详解 - jdk.ThreadAllocationStatistics

    定时线程分配统计事件:jdk.ThreadAllocationStatistics 引入版本:Java 11 相关 ISSUES: Test jdk/jfr/event/runtime/TestThr ...

  2. 1109 Group Photo (25分)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  3. Zabbix页面管理

    Zabbix页面管理 Screen Screen翻译成中文为"屏幕",在一些交通管理中心.保安监控.预警中心等等地方都比较常见到监控视频,视频上有多块小视频,实际上Zabbix S ...

  4. [转]gitlab ci/cd 发布

    转自 https://meigit.readthedocs.io/en/latest/configure_gitlab_i18n_and_create_gitlab_ci_with_gitlab_ru ...

  5. hdu3746 KMP的next数组应用,求项链首尾项链循环

    题意:       给你一个项链,问你最少加多少个珠子能满足整个项链是一个循环的项链(首尾相连) 思路:      KMP的简单应用只要了解next数组的意义就好说了,下面总结下  next在循环方面 ...

  6. UVA10763交换学生

    题意:       给你N组关系,每组关系是a,b,最后问你所有的a,b出现的次数和所有的b,a出现的此时是否全部都一样. 思路:       水题,直接开了个二维的map标记,map<int ...

  7. 解决在Vim中鼠标右键不能粘贴问题

    最近维护一台服务器,使用putty登录后,用vim时,鼠标右键不能 粘贴而是进入了visual模式.网上查找一番找到了解决方法: 方 法一:在普通模式下键入" :set mouse-=a&q ...

  8. 每天一道面试题LeetCode 80--删除排序数组中的重复项 II(python实现)

    LeetCode 80--删除排序数组中的重复项 II 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输 ...

  9. Docker安装教程(超详细)

    Docker安装教程(超详细) 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章, 关注回复「资源」, 免费领取全网最热的Java架构师学习PDF, 转载请注明出处 http:// ...

  10. JWT 基本使用

    JWT 基本使用 在上一节中 session 共享功能使用 redis 进行存储,用户量激增时会导致 redis 崩溃,而 JWT 不依赖服务器,能够避免这个问题. 1.传统 session 1.1. ...