简体   繁体   中英

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. I use the aspnet-redis-providers lib on the Azure Github.

In Redis, it creates a {[app_name]_[sessionkey}_Internal key with a SessionTimeout key with the value of the configured session timeout. The EXPIRE for that key is set to that time and when you check to TTL for the key you see the session access.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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