简体   繁体   English

从RedisSessionStateProvider获取活动会话的列表

[英]Get a list of active session from RedisSessionStateProvider

In my Azure ASP.NET MVC website I want to display how many clients are connected to a Redis sessions state provider and how long they are active. 在我的Azure ASP.NET MVC网站中,我想显示有多少个客户端连接到Redis会话状态提供程序以及它们处于活动状态的时间。 I use the aspnet-redis-providers lib on the Azure Github. 我使用Azure Github上的aspnet-redis-providers库。

In Redis, it creates a {[app_name]_[sessionkey}_Internal key with a SessionTimeout key with the value of the configured session timeout. 在Redis中,它将创建一个{[app_name] _ [sessionkey} _Internal键以及一个SessionTimeout键,该键具有配置的会话超时值。 The EXPIRE for that key is set to that time and when you check to TTL for the key you see the session access. 该密钥的EXPIRE设置为该时间,当您为该密钥检查TTL时,将看到会话访问。

How can I use the session state provider library to access this information? 如何使用会话状态提供程序库访问此信息? If that is not possible, is there any other library I can use to query this info safely, without interfering with the session state provider? 如果这不可能,那么我是否可以使用其他任何库来安全地查询此信息,而不会干扰会话状态提供程序?

Here is what I was able to do. 这就是我所能做的。 I created my own session object collection and grabbed all keys (which I am putting in DB 1) then I loop through all keys and grab the TTL. 我创建了自己的会话对象集合,并获取了所有键(将它们放入DB 1中),然后遍历所有键并获取了TTL。

using StackExchange.Redis;
using StackExchange.Redis.Extensions.Newtonsoft;
using StackExchange.Redis.Extensions.Core;
using System.Linq;

    private static Lazy<ConnectionMultiplexer> conn = new Lazy<ConnectionMultiplexer>(
       () => ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["RedisServerMaster"]
         + "," + ConfigurationManager.AppSettings["RedisServerSlave"]
         + "," + ConfigurationManager.AppSettings["RedisOptions"])

    public class SessionObjects
    {
        public string SessionId { get; set; }
        public TimeSpan? TTL { get; set; }
    }

    List<SessionObjects> lso = new List<SessionObjects>();
    var serializer = new NewtonsoftSerializer();
    StackExchangeRedisCacheClient cacheClient;
    cacheClient = new StackExchangeRedisCacheClient(rConn, serializer, 1);
    IEnumerable<string> keys = cacheClient.SearchKeys("*");
    var db = rConn.GetDatabase(1);
    foreach (var s in keys)
    {
        SessionObjects so = new SessionObjects();
        so.SessionId = s;
        so.TTL = db.KeyTimeToLive(s);
        lso.Add(so);
    }

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

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