繁体   English   中英

我还需要在C#中的Redis缓存中存储什么呢?

[英]what more I need to can storage in redis cache in c#?

我是从的NuGet和StackExchange.Redis.StrongName装XX,也把一个配置在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中,请帮帮我!

解决方案是从nuget重新安装软件包RedisSessionStateProvider和StackExchange.Redis.StrongName,之后需要在Web配置中进行一些更改

网页配置

<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,您可以查看答案

它不可能是自动的,您需要一个额外的库来与Redis和ASP.NET结合,还需要对web.config进行小的修改。 例如,如果您不依赖于StackExchange.Redis https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore ,或者您像在您的情况下那样使用StackExchange.Redis,那么下面的操作就可以实现:https://github.com/welegan/ RedisSessionProvider

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM