简体   繁体   English

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

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

I was installed XX from nuget and StackExchange.Redis.StrongName , also put the next configuration in the web.config RedisSessionStateProvider 我是从的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>

but can't storage the session on redis, but if I do the connection on code, it is succesfull. 但无法在Redis上存储会话,但是如果我在代码上进行连接,则说明连接成功。 my code: 我的代码:

/// set the conecction ///设置概念

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();
}

I need storage the session automatically in redis, please help me! 我需要将会话自动存储在Redis中,请帮帮我!

The solution was reinstall the packages RedisSessionStateProvider and StackExchange.Redis.StrongName from nuget, after that is need change some things in the web config 解决方案是从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>

CONTROLLER 控制器

And then only create a session object: 然后只创建一个会话对象:

Session["test-" + Guid.NewGuid().ToString()] = DateTime.Now.ToLongDateString();

You can use Redis Desktop Manager for tracking the values or the Azure portal 您可以使用Redis Desktop Manager跟踪值或Azure门户 在此处输入图片说明

the solution was gave by @juank. 该解决方案由@juank提供。

@Luca Detomi you can review the answer @Luca Detomi,您可以查看答案

It can't be that automatic, your need one more extra library to marry Redis and ASP.NET, and a small web.config modification. 它不可能是自动的,您需要一个额外的库来与Redis和ASP.NET结合,还需要对web.config进行小的修改。 For example, the following shall do if you are not tied to StackExchange.Redis https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore , or this uses StackExchange.Redis as in your case https://github.com/welegan/RedisSessionProvider 例如,如果您不依赖于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