asp.net core 使用 Redis 和 Protobuf
asp.net core 使用 Redis 和 Protobuf
前言
上篇博文介绍了怎么样在 asp.net core 中使用中间件,以及如何自定义中间件。项目中刚好也用到了Redis,所以本篇就介绍下怎么样在 asp.net core 中使用 Redis 进行资源缓存和Session缓存。 如果你觉得对你有帮助的话,不妨点个【推荐】。
目录
- Redis 介绍
- asp.net core Session 介绍
- Redis & Session 实例讲解
- Session的使用
- 使用 Protobuf 给 Session添加扩展方法
Redis 介绍
下面是Redis官网的介绍:
Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Redis 是一个开源的(基于BSD许可),内存数据存储结构,常用作数据库,缓存,和消息队列。它支持如字符串、哈希表、列表、集合、排序集范围查询、位图、hyperloglogs半径和地理空间索引与查询。Redis内置主从连接,Lua脚本、LRU回收,事务和不同级别文件持久化,并提供了利用集群的主从切换和自动分区等来保证高可用性。
Redis的深入介绍我就直接开传送门了,不是本篇的重点,但是我给你们整理了一下,你们还是要感谢我滴:
redis 高可用部署及监控:http://blog.sina.com.cn/s/blog_75ad98f30101fwqj.html
redis 主从连接:http://www.tuicool.com/articles/fAnYFb
redis 事务: http://redisbook.readthedocs.io/en/latest/feature/transaction.html
redis 内存回收LRU:http://www.open-open.com/lib/view/open1427547789059.html
redis 数据持久化:http://qifuguang.me/2015/10/13/Redis%E6%8C%81%E4%B9%85%E5%8C%96/
以上知识学习完,使用和面试时应付Redis的提问应该不成问题了。
asp.net core session 介绍
session本身是一个很难解释的名词,在http中session代表服务器与浏览器的一次会话过程,这个过程可能一直,也可能间断的。
asp.net core中的session以中间件的方式提供给我们来使用。
下面来看一下使用方法:
首先,添加session的NuGet包Microsoft.AspNetCore.Http.Abstractions
到项目中,在startup.cs
文件的ConfigureServices(IServiceCollection services)
函数中,使用app.UseSession()
和app.UseCaching()
来使用session.
//在使用session之前要注入cacheing,因为session依赖于cache进行存储
services.AddCaching();
services.AddSession();
添加了session之后就需要有存储session的地方,可以使用内存存储,也可以使用其他自定义存储,比如redis或者SQL Server等。
// 重要: session的注册必须在UseMvc之前,因为MVC里面要用
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
Redis & Session 实例讲解
要在Session中使用Redis,只需要把services.AddCaching();
换成services.AddDistributedRedisCache();
就可以了,如下:
services.AddDistributedRedisCache(option =>
{
//redis 数据库连接字符串
option.Configuration = Configuration.GetConnectionString("RedisConnection");
//redis 实例名
option.InstanceName = "master";
}
);
Session的使用
在 asp.net core 的 MVC Controller 中,你可以HttpContext.Session
来获取Session对象。
如果不是在 Controller 中需要使用 Session 的话,可以使用IHttpContextAccessor
这个接口通过注入的方式来获取Session。
以下是在 Controller 中使用Session,需要引入Microsoft.AspNetCore.Http
空间:
public class HomeController : Controller
{
public IActionResult Index()
{
HttpContext.Session.SetString("Test", "Ben Rules!");
return View();
}
public IActionResult About()
{
ViewBag.Message = HttpContext.Session.GetString("Test");
return View();
}
}
以下是在除了 Controller 的其他地方使用 Session:
public class SomeOtherClass
{
private readonly IHttpContextAccessor _httpContextAccessor;
private ISession _session => _httpContextAccessor.HttpContext.Session;
public SomeOtherClass(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public void TestSet()
{
_session.SetString("Test", "Ben Rules!");
}
public void TestGet()
{
var message = _session.GetString("Test");
}
}
使用 Protobuf 给 Session添加扩展方法
默认情况下,我们只能存储byte[]
到我们的Session当中,这让我们使用起来很不方便,在Microsoft.HttpCore.Extension
中 Microsoft 给提供了一个SetString,SetInt32 和GetString,GetInt32的方法,但是在很多情况下,我们是需要使用Session来存储一个对象的,那么此时就需要给Session添加一个扩展方法。
为了追求效率和性能,我们选择Google的Protobuf序列化组件,而不使用Json.Net,在性能方面,Protobuf要比XML或者Json效率高很多。
在Nuget包中引入protobuf-net
:
public static class SessionExtensions
{
public static T Get<T>(this ISession session, string key) where T : class {
byte[] byteArray = null;
if (session.TryGetValue(key, out byteArray)) {
using (var memoryStream = new MemoryStream(byteArray)) {
var obj = ProtoBuf.Serializer.Deserialize<T>(memoryStream);
return obj;
}
}
return null;
}
public static void Set<T>(this ISession session, string key, T value) where T : class {
try {
using (var memoryStream = new MemoryStream()) {
ProtoBuf.Serializer.Serialize(memoryStream, value);
byte[] byteArray = memoryStream.ToArray();
session.Set(key, byteArray);
}
}
catch (Exception) {
throw;
}
}
}
使用Protobuf-net序列化,需要在序列化的对象打上[ProtoContract][ProtoMember]等标记。
Ps:目前Redis的扩展Microsoft.Extensions.DependencyInjection
下面的AddDistributedRedisCache
还不支持RC2,可以去github上搜索源代码,添加到项目中,也可以留下邮箱,我会发给你。
本文地址:http://www.cnblogs.com/savorboard/p/5592948.html
作者博客:Savorboard
asp.net core 使用 Redis 和 Protobuf的更多相关文章
- ASP.NET Core 使用 Redis 和 Protobuf 进行 Session 缓存
前言 上篇博文介绍了怎么样在 asp.net core 中使用中间件,以及如何自定义中间件.项目中刚好也用到了Redis,所以本篇就介绍下怎么样在 asp.net core 中使用 Redis 进行资 ...
- ASP.NET Core 使用 Redis 客户端
Mac OS 安装 Redis(用于连 Redis 服务器,方便查看数据):https://redis.io/topics/quickstart wget http://download.redis. ...
- ASP.NET Core 使用 Redis 实现分布式缓存:Docker、IDistributedCache、StackExchangeRedis
ASP.NET Core 使用 Redis 实现分布式缓存:Docker.IDistributedCache.StackExchangeRedis 前提:一台 Linux 服务器.已安装 Docker ...
- asp.net Core 使用redis(StackExchange.Redis)
原文:asp.net Core 使用redis(StackExchange.Redis) 一.添加配置(appsettings.json) "Redis": { "Def ...
- Asp.net Core 使用Redis存储Session
前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...
- asp.net core 运用 Redis 配置步骤
Redis可以用来存储session或直接存储键值对 首先要有asp.net core的项目,可以是webapi 或者MVC项目,还有有本地的Redis或者在远程服务器上,具体的安装就不讲述了 以下是 ...
- [转]Asp.net Core 使用Redis存储Session
本文转自:http://www.cnblogs.com/hantianwei/p/5723959.html 前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储 ...
- ASP.NET Core与Redis搭建一个简易分布式缓存
本文主要介绍了缓存的概念,以及如何在服务器内存中存储内容.今天的目标是利用IDistributedCache来做一些分布式缓存,这样我们就可以横向扩展我们的web应用程序. 在本教程中,我将使用Re ...
- ASP.Net Core 使用Redis实现分布式缓存
本篇我们记录的内容是怎么在Core中使用Redis 和 SQL Server 实现分布式缓存. 一.文章概念描述 分布式缓存描述: 分布式缓存重点是在分布式上,相信大家接触过的分布式有很多中,像分 ...
随机推荐
- Node.js入门-Node.js 介绍
Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...
- Objects
Obeject Object Object representation and value representation Subobjects Polyomrphic objecets Alignm ...
- DFS(White-Gray-Black)
参考<数据结构与算法> 本书在复杂深度优先遍历图时,采用三种颜色标记图中节点 1 white 表示未访问 2 gray 表示已经正在访问,其相邻节点 3 black 表示该节点所有的相邻节 ...
- phpeclipse
http://phpeclipse.sourceforge.net/update/stable/1.2.x/
- shell学习之常用命令总结
1.find命令 主要用途:主要用来做文件查找. 使用方法:查找文件的方式可以基于:文件名,文件时间属性,文件的所有者和组,文件权限属性,文件类型属性,文件大小,另外可以指定 查找目录的深度,排除指定 ...
- Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 治标又治本的解决方法: 找到在 { C:\Windows\Microsoft.NET\Fra ...
- Windows Phone 8初学者开发—第7部分:本地化应用程序
原文 Windows Phone 8初学者开发—第7部分:本地化应用程序 第7部分:本地化应用程序 原文地址: http://channel9.msdn.com/Series/Windows-Phon ...
- c++继承构造子类调用父类构造函数的问题及关于容器指针的问题及当容器里储存指针时,记得要手动释放
看下面的一个问题: class Person { private: string name; public: Person(const string& s=""){ nam ...
- 基于visual Studio2013解决C语言竞赛题之0205位数求和
题目
- 戴尔CEO:我们将专注于企业 而非手机业务
9月13日消息,据国外媒体报道,戴尔公司董事长兼首席执行官迈克尔·戴尔(Michael Dell)周五接受了CNBC采访,谈了他对戴尔未来的打算.此前一天,迈克尔·戴尔提出的以250亿美元将戴尔私有化 ...