Configuring Redis for ASP.NET Core Session Store(转载)
原文:https://dotnetthoughts.net/configuring-redis-for-aspnet-core-session-store/
Posted by Anuraj on Monday, November 14, 2016 Reading time :1 minute
ASP.NET Core Redis Cache Session
This post is about Configuring Redis for ASP.NET Core Session Store. 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. Redis works with an in-memory dataset. it also supports persisting the dataset to disk. Moreover, It provides master-slave asynchronous replication. Redis is not officially supported on windows. However, the Microsoft Open Tech group develops and maintains Windows port targeting Win64 available here. You can install redis using chocolatey package manager, using choco install redis-64
command. Once you install redis you can run redis-server
command to start the redis server.
You can test the installation using redis-cli
command. You can set and get the values. For setting the values you need to use command like set key value
and for getting the values get key
. To list all the keys you can execute keys *
command. To remove all the values flushall
command can be used.
To use Redis in ASP.NET Core, you need to reference, Microsoft.Extensions.Caching.Redis.Core
package. And you can configure the Redis Cache using AddDistributedRedisCache
method in the ConfigureServices
method.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedRedisCache(options =>
{
options.InstanceName = "Sample";
options.Configuration = "localhost";
});
services.AddMvc();
}
Unlike previous versions of ASP.NET, Session in ASP.NET Core doesn’t will not be available by default. You need to configure session. To use session first you need to update the project.json with Microsoft.AspNetCore.Session
package. And once you added it you need to add session and configure it.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedRedisCache(options =>
{
options.InstanceName = "Sample";
options.Configuration = "localhost";
});
services.AddSession();
services.AddMvc();
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
And here is the project.json file changes
"Microsoft.Extensions.Caching.Redis.Core": "1.0.3",
"Microsoft.AspNetCore.Session": "1.0.0"
Now in code you can use Set and TryGetValue methods to set and get values from session.
var bytes = Encoding.UTF8.GetBytes("World");
HttpContext.Session.Set("Hello", bytes);
And here is the code to get values.
var bytes = default(byte[]);
HttpContext.Session.TryGetValue("Hello", out bytes);
var content = Encoding.UTF8.GetString(bytes);
Get and Set methods in session is accepting byte array, so you need to convert string values to bytes.
Happy Programming :)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?