NCache获取所有会话数据

时间:2018-06-09 16:06:38

标签: ncache session-cache

在文档的此页面上(http://www.alachisoft.com/resources/docs/ncache/prog-guide/aspnet-session-state-provider.html),

我找到了:

Fetch Session Data
NCache allows viewing all session data stored in clustered cache via NCache 
Session State Module. All session data is added in the specified cache as a 
tagged cache item with tag: NC_ASP.net_session_data.

In order to retrieve previously stored session data, data can be fetched by 
GetByTag API, which returns Hashtable populated with session IDs and 
associated session data stored in the cache.

Hashtable allSessionData = cache.GetByTag(new  
Alachisoft.NCache.Runtime.Caching.Tag("NC_ASP.net_session_data"));

我可以看到哈希表填充了NCache中保存的每个会话的条目。

我可以将会话ID视为键,将字节数组(序列化的HashTable)视为值。

我可以反序列化哈希表,但我不知道如何继续枚举用户会话的所有键并获取每个值。

我可以这样做。

var cache = NCache.Caches["AspNetCache"];
var ht = cache.GetByTag(new Tag("NC_ASP.net_session_data"));
var dctNcache = ht.Cast<DictionaryEntry>().ToDictionary(c => (string)c.Key, c => c.Value);

byte[] array = (byte[])dctNcache.First().Value;
using (var ms = new MemoryStream(array)){
    var bf = new BinaryFormatter();
    var obj = bf.Deserialize(ms) as Hashtable;
    Debug.WriteLine($"{nameof(obj)} is {obj.GetType()}");
    var sd = obj["SD"] as byte[];

    using (var ms1 = new MemoryStream(sd)){
        var obj1 = bf.Deserialize(ms1);
        Debug.WriteLine($"{nameof(obj1)} is {obj1.GetType()}");
    }
}

0 个答案:

没有答案