如何使用 RedisSessionStateProvider 配置 Redis 缓存过期?

How to config Redis cache expiration with RedisSessionStateProvider?

我创建了一个 MVC 示例应用程序,使用 RedisSessionStateProvider 作为自定义会话状态。我将一个变量设置为会话状态,效果很好。但是我不知道如何同步会话和Redis中的项目过期时间。有人可以帮忙吗?

这是来自web.config

的参数
        <!--
      <add name="MySessionStateStore" 
        host = "127.0.0.1" [String]
        port = "" [number]
        accessKey = "" [String]
        ssl = "false" [true|false]
        throwOnError = "true" [true|false]
        retryTimeoutInMilliseconds = "5000" [number]
        databaseId = "0" [number]
        applicationName = "MvcTestApp" [String]
        connectionTimeoutInMilliseconds = "5000" [number]
        operationTimeoutInMilliseconds = "1000" [number]
        connectionString = "<Valid StackExchange.Redis connection string>" [String]
        loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
        loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
      />
    -->

我使用的是最新版本: RedisSessionStateProvider 版本 1.6.4 Redis 版本 2.8.21

Redis中设置的session项的过期时间Session会过期,你不需要做任何处理。它将由 RedisSessionStateProvider

处理

所有会话状态提供者都没有过期属性。有个HttpSessionState.Timeout

Gets and sets the amount of time, in minutes, allowed between requests before the session-state provider terminates the session.

The Timeout property cannot be set to a value greater than 525,600 minutes (1 year). The default value is 20 minutes.

因此,asp.net 为我们管理了这个逻辑,我们无需担心。

在redis客户端,可以使用两条命令keys *查看所有key,ttl <key>查看什么时候过期。