using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Ant.Service.Utility
{
///
/// 缓存管理
/// 将用户凭证、令牌的关系数据存放于Cache中
///
public class CacheManager
{
private volatile static CacheManager instance = null;
private static object locker = new Object();
public static CacheManager Instance
{
get
{
if (instance == null)
{
lock (locker)
{
if (instance == null)
{
instance = new CacheManager();
}
}
}
return instance;
}
}
///
/// 初始化数据结构
///
///
/// ----------------------------------------------------
/// | token(令牌) | info(用户凭证) | timeout(过期时间) |
/// |--------------------------------------------------|
///
private static void cacheInit(string token, object obj, double timeout)
{
if (HttpContext.Current.Cache[token] == null)
{
if (timeout == 0 || timeout == null)
{
timeout = double.Parse(System.Configuration.ConfigurationManager.AppSettings["timeout"]);
}
//HttpContext.Current.Cache.Insert(UserName, dt, null, DateTime.Now.AddMinutes(timeout), TimeSpan.Zero);//TimeSpan.Zero表示不使用平滑过期策略
//HttpContext.Current.Cache.Insert(UserName, dt, null, DateTime.MaxValue, TimeSpan.FromMinutes(timeout));//表示缓存连续30分钟没有访问就过期。
HttpContext.Current.Cache.Insert(token, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(timeout));//表示缓存连续30分钟没有访问就过期。
}
}
///
/// 判断令牌是否存在
///
/// 令牌
///
public static bool TokenIsExist(string token)
{
if (token.IsEmpty())
{
return false;
}
else
{
object dt = HttpContext.Current.Cache[token];
if (dt == null)
return false;
else
return true;
}
}
///
/// 通过令牌获取缓存的值
///
/// 令牌
///
public static object GetTokenValue(string token)
{
object dt = HttpContext.Current.Cache[token];
return dt;
}
///
/// 获取所有列表数据
///
///
public static List GetAllList()
{
List cacheKeys = new List();
Dictionary dictionary = new Dictionary();
IDictionaryEnumerator CacheEnum = HttpContext.Current.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
string token = CacheEnum.Key.ToString();
cacheKeys.Add(token);
}
return cacheKeys;
}
///
/// 判断令牌是否存在
///
/// 令牌
///
public static bool TokenClear(string token)
{
object obj = HttpContext.Current.Cache.Remove(token);
if (obj == null)
return false;
else
return true;
}
///
///
/// 移除全部缓存
///
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpContext.Current.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
///
/// 更新令牌过期时间
///
/// 令牌
/// 过期时间
public static void TokenTimeUpdate(string token, DateTime time)
{
DataTable dt = (DataTable)HttpContext.Current.Cache["CERT"];
DataRow[] dr = dt.Select("token = '" + token + "'");
if (dr.Length > 0)
{
dr[0]["timeout"] = time;
}
}
///
/// 添加令牌
///
/// 令牌
/// 凭证
/// 过期时间
public static void TokenInsert(string token, object info, double timeout = 0)
{
if (TokenIsExist(token))
{
HttpContext.Current.Cache[token] = info;//凭证存则替换缓存内容
//FieldValueCollection.Instance[token] = username;
}
else
{
//FieldValueCollection.Instance.Add(token, username);
cacheInit(token, info, timeout);
}
}
}//end class
}