我还需要在 c# 中的 redis 缓存中存储什么?
what more I need to can storage in redis cache in c#?
我是从nuget安装XX和StackExchange.Redis.StrongName,也把接下来的配置放在web.config RedisSessionStateProvider
<sessionState mode="Custom" customProvider="RedisSessionProvider" cookieless="true" > <providers> <add name="RedisSessionProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider" port="6380" host="XXX.redis.cache.windows.net" accessKey="OQm………15E=" ssl="true" connectionTimeoutInMilliseconds="5000" operationTimeoutInMilliseconds="1000" retryTimeoutInMilliseconds="3000" writeExceptionsToEventLog="true" /></providers>
但无法将会话存储在 redis 上,但是如果我在代码上进行连接,它是成功的。我的代码:
/// 设置连接
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
var redisOption = new ConfigurationOptions();
return ConnectionMultiplexer.Connect("XXX.redis.cache.windows.net:6380,abortConnect=false,ssl=true,password=OQmAPmp0 . . . . TJE15E=");
});
///return connection object
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
///the session is created and added some elements manually in redis to test
public ActionResult SessionStart()
{
IDatabase cache = Connection.GetDatabase();
Session["loginTime"] = DateTime.Now.ToString();
string strValue = "myvalue";
Session.Add("myvalue ", strValue);
return View();
}
我需要在redis中自动存储会话,请帮助我!
它不可能那么自动,您需要一个额外的库来结合 Redis 和 ASP.NET,以及一个小的 web.config 修改。例如,如果您未绑定到 StackExchange.Redis,则应执行以下操作
https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore, or this uses StackExchange.Redis as in your case https://github.com/welegan/RedisSessionProvider
解决方案是从 nuget 重新安装包 RedisSessionStateProvider 和 StackExchange.Redis.StrongName,之后需要更改 Web 配置中的一些东西
WEB.CONFIG
<sessionState mode="Custom" customProvider="MySessionStateStore" >
<providers>
<add name="MySessionStateStore"
type="Microsoft.Web.Redis.RedisSessionStateProvider"
host="abcde1234.redis.cache.windows.net"
accessKey="FuDmzfO3B/6M1cX1ls="
ssl="true" throwOnError="true" port="6380" writeExceptionsToEventLog="true"
databaseId = "1"
/>
</providers>
</sessionState>
控制器
然后只创建一个会话对象:
Session["test-" + Guid.NewGuid().ToString()] = DateTime.Now.ToLongDateString();
您可以使用 Redis Desktop Manager 来跟踪值或 Azure 门户
@juank 给出了解决方案
@Luca Detomi 你可以查看答案
我是从nuget安装XX和StackExchange.Redis.StrongName,也把接下来的配置放在web.config RedisSessionStateProvider
<sessionState mode="Custom" customProvider="RedisSessionProvider" cookieless="true" > <providers> <add name="RedisSessionProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider" port="6380" host="XXX.redis.cache.windows.net" accessKey="OQm………15E=" ssl="true" connectionTimeoutInMilliseconds="5000" operationTimeoutInMilliseconds="1000" retryTimeoutInMilliseconds="3000" writeExceptionsToEventLog="true" /></providers>
但无法将会话存储在 redis 上,但是如果我在代码上进行连接,它是成功的。我的代码:
/// 设置连接
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
var redisOption = new ConfigurationOptions();
return ConnectionMultiplexer.Connect("XXX.redis.cache.windows.net:6380,abortConnect=false,ssl=true,password=OQmAPmp0 . . . . TJE15E=");
});
///return connection object
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
///the session is created and added some elements manually in redis to test
public ActionResult SessionStart()
{
IDatabase cache = Connection.GetDatabase();
Session["loginTime"] = DateTime.Now.ToString();
string strValue = "myvalue";
Session.Add("myvalue ", strValue);
return View();
}
我需要在redis中自动存储会话,请帮助我!
它不可能那么自动,您需要一个额外的库来结合 Redis 和 ASP.NET,以及一个小的 web.config 修改。例如,如果您未绑定到 StackExchange.Redis,则应执行以下操作 https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore, or this uses StackExchange.Redis as in your case https://github.com/welegan/RedisSessionProvider
解决方案是从 nuget 重新安装包 RedisSessionStateProvider 和 StackExchange.Redis.StrongName,之后需要更改 Web 配置中的一些东西
WEB.CONFIG
<sessionState mode="Custom" customProvider="MySessionStateStore" >
<providers>
<add name="MySessionStateStore"
type="Microsoft.Web.Redis.RedisSessionStateProvider"
host="abcde1234.redis.cache.windows.net"
accessKey="FuDmzfO3B/6M1cX1ls="
ssl="true" throwOnError="true" port="6380" writeExceptionsToEventLog="true"
databaseId = "1"
/>
</providers>
</sessionState>
控制器
然后只创建一个会话对象:
Session["test-" + Guid.NewGuid().ToString()] = DateTime.Now.ToLongDateString();
您可以使用 Redis Desktop Manager 来跟踪值或 Azure 门户
@juank 给出了解决方案
@Luca Detomi 你可以查看答案