61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using System;
|
|
using CacheManager.Core;
|
|
using CacheManager.Core.Internal;
|
|
using Kreta.Framework.Security;
|
|
|
|
namespace Kreta.Framework.Caching
|
|
{
|
|
public sealed class LoginInfoCache : GenericCache<LoginInfo>
|
|
{
|
|
private static readonly string LoginInfoCacheKeyPrefix = $"{nameof(Kreta)}_{nameof(LoginInfoCache)}_";
|
|
|
|
private static string GetCacheKey(string sessionId)
|
|
{
|
|
return $"{LoginInfoCacheKeyPrefix}{sessionId}";
|
|
}
|
|
|
|
public LoginInfoCache(CacheManager cacheManager) : base(cacheManager, nameof(LoginInfoCache))
|
|
{
|
|
}
|
|
|
|
public int SessionTimeout { get; set; }
|
|
|
|
public void PutLoginInfo(LoginInfo loginInfo)
|
|
{
|
|
Put(GetCacheKey(loginInfo.SessionID), loginInfo);
|
|
}
|
|
|
|
public void UpdateLoginInfo(LoginInfo loginInfo)
|
|
{
|
|
Update(GetCacheKey(loginInfo.SessionID), _ => loginInfo);
|
|
}
|
|
|
|
public void RemoveLoginInfo(string sessionId)
|
|
{
|
|
Remove(GetCacheKey(sessionId));
|
|
}
|
|
|
|
protected override void Cache_OnRemoveByHandle(object sender, CacheItemRemovedEventArgs e)
|
|
{
|
|
if (e.Key.StartsWith(LoginInfoCacheKeyPrefix, StringComparison.Ordinal) && e.Reason == CacheItemRemovedReason.Expired)
|
|
{
|
|
SDAServer.Instance.SessionManager.DeleteSession((LoginInfo)e.Value);
|
|
}
|
|
}
|
|
|
|
public LoginInfo GetLoginInfo(string sessionId)
|
|
{
|
|
return Get(GetCacheKey(sessionId));
|
|
}
|
|
|
|
public bool IsExistsLoginInfo(string sessionId)
|
|
{
|
|
return Exists(GetCacheKey(sessionId));
|
|
}
|
|
|
|
public void Ping(string sessionId)
|
|
{
|
|
Expire(GetCacheKey(sessionId), ExpirationMode.Sliding, new TimeSpan(0, SessionTimeout, 0));
|
|
}
|
|
}
|
|
}
|