And*_*rew 4 c# asp.net redis stackexchange.redis
我试图了解如何通过用 Redis 缓存替换所有 IIS 会话状态值来实现 Redis。我让 redis 使用 docker 镜像工作。我正在使用 C# .Net Forms Web 应用程序。到目前为止,我已经包含了 StackExchange.Redis nuget 包并进行了设置。
public class Redis
{
private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
var redisConnectionString = ConfigurationManager.AppSettings["RedisConnectionString"];
var options = ConfigurationOptions.Parse(redisConnectionString);
options.AbortOnConnectFail = false;
return ConnectionMultiplexer.Connect(options);
});
public static ConnectionMultiplexer Connection => LazyConnection.Value;
}
Run Code Online (Sandbox Code Playgroud)
但我很困惑如何存储用户上下文会话键值对。我的意思是,当我为用户存储密钥时,说“UserId”,密钥可以只是“UserId”吗?或者我是否需要在它前面添加一个唯一的用户特定上下文 ID。我找不到任何关于 redis 如何根据不同用户工作的信息。它如何知道用户的上下文以及如何获得正确的密钥?
我读到的只是它是一个存储值的哈希表,这对于单个 UserId 来说很好,但是我将拥有很多具有 UserId 的用户?
如果有人能帮助我理解这一点,那就太好了,谢谢
好的,在查看以下链接后,我能够使其全部正常工作
\nhttps://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-aspnet-session-state-provider
\n\n\nRun Code Online (Sandbox Code Playgroud)\ndocker run -p 8055:6379 --name redis --restart=always -d redis \xe2\x80\x93-requirepass <your_long_password_here>\n
\n\nRun Code Online (Sandbox Code Playgroud)\ndocker container ls\n
\n\nRun Code Online (Sandbox Code Playgroud)\nInstall-Package Microsoft.Web.RedisSessionStateProvider\n
\n\nRun Code Online (Sandbox Code Playgroud)\n<sessionState mode="InProc" timeout="60" />\n
\n\nRun Code Online (Sandbox Code Playgroud)\n<sessionState mode="Custom" customProvider="MySessionStateStore">\n <providers>\n <add name="MySessionStateStore"\n type="Microsoft.Web.Redis.RedisSessionStateProvider"\n connectionString="localhost:8055,password=your_long_password_here"/>\n </providers>\n</sessionState>\n
您现在应该看到可以无缝运行您的网站,并且您现在将使用 Redis 缓存,而不再使用会话状态。
\n| 归档时间: |
|
| 查看次数: |
5376 次 |
| 最近记录: |