123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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
- {
- /// <summary>
- /// 缓存管理
- /// 将用户凭证、令牌的关系数据存放于Cache中
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 初始化数据结构
- /// </summary>
- /// <remarks>
- /// ----------------------------------------------------
- /// | token(令牌) | info(用户凭证) | timeout(过期时间) |
- /// |--------------------------------------------------|
- /// </remarks>
- 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分钟没有访问就过期。
- }
- }
- /// <summary>
- /// 判断令牌是否存在
- /// </summary>
- /// <param name="token">令牌</param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 通过令牌获取缓存的值
- /// </summary>
- /// <param name="token">令牌</param>
- /// <returns></returns>
- public static object GetTokenValue(string token)
- {
- object dt = HttpContext.Current.Cache[token];
- return dt;
- }
- /// <summary>
- /// 获取所有列表数据
- /// </summary>
- /// <returns></returns>
- public static List<string> GetAllList()
- {
- List<string> cacheKeys = new List<string>();
- Dictionary<string, object> dictionary = new Dictionary<string, object>();
- IDictionaryEnumerator CacheEnum = HttpContext.Current.Cache.GetEnumerator();
- while (CacheEnum.MoveNext())
- {
- string token = CacheEnum.Key.ToString();
- cacheKeys.Add(token);
- }
- return cacheKeys;
- }
- /// <summary>
- /// 判断令牌是否存在
- /// </summary>
- /// <param name="token">令牌</param>
- /// <returns></returns>
- public static bool TokenClear(string token)
- {
- object obj = HttpContext.Current.Cache.Remove(token);
- if (obj == null)
- return false;
- else
- return true;
- }
- /// <returns></returns>
- /// <summary>
- /// 移除全部缓存
- /// </summary>
- public static void RemoveAllCache()
- {
- System.Web.Caching.Cache _cache = HttpContext.Current.Cache;
- IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
- while (CacheEnum.MoveNext())
- {
- _cache.Remove(CacheEnum.Key.ToString());
- }
- }
- /// <summary>
- /// 更新令牌过期时间
- /// </summary>
- /// <param name="token">令牌</param>
- /// <param name="time">过期时间</param>
- 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;
- }
- }
- /// <summary>
- /// 添加令牌
- /// </summary>
- /// <param name="token">令牌</param>
- /// <param name="info">凭证</param>
- /// <param name="timeout">过期时间</param>
- 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
- }
|