123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace WingCloudServer.GeneralDocTools.Helper
- {
- public class CommonMemoryCaching
- {
- private static readonly object SynObject = new object();
- private static CommonMemoryCaching instance = null;
- private CommonMemoryCaching()
- {
- Task.Run(async () =>
- {
- while (true)
- {
- List<string> list = new List<string>();
- foreach (var item in this._myCacheExpire.Keys)
- {
- this._myCacheExpire.TryGetValue(item, out var expireObj);
- if (expireObj != null)
- {
- if (expireObj.Item1 < expireObj.Item2)
- {
- list.Add(item);
- }
- }
- }
- foreach (var item in list)
- {
- this._myCacheExpire.TryRemove(item, out var expireObj);
- this._myCache.TryRemove(item, out var cacheObj);
- }
- await Task.Delay(500);
- }
- });
- }
- /// <summary>
- /// 单例模式
- /// </summary>
- public static CommonMemoryCaching GetInstance()
- {
- if (instance == null)
- {
- lock (SynObject)
- {
- if (instance == null)
- {
- instance = new CommonMemoryCaching();
- }
- }
- }
- return instance;
- }
- private readonly ConcurrentDictionary<string, object> _myCache = new ConcurrentDictionary<string, object>();
- private readonly ConcurrentDictionary<string, Tuple<DateTime, DateTime>> _myCacheExpire = new ConcurrentDictionary<string, Tuple<DateTime, DateTime>>();
- private const long DefaultExpireTime = 300000;
- /// <summary>
- /// 获取和设置缓存信息
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="cacheItemName"></param>
- /// <param name="objectSettingFunction"></param>
- /// <param name="expireTime">默认过期时间5分钟</param>
- /// <returns></returns>
- public T GetOrSetObjectFromCache<T>(string cacheItemName, Func<T> objectSettingFunction, bool isSetExpire = true, long expireTime = DefaultExpireTime)
- {
- T cacheObject = default(T);
- this._myCache.TryGetValue(cacheItemName, out var cacheObj);
- if (cacheObj != null)
- {
- cacheObject = (T)cacheObj;
- }
- else
- {
- if (objectSettingFunction != null)
- {
- cacheObject = objectSettingFunction();
- this._myCache.TryAdd(cacheItemName, cacheObject);
- if (isSetExpire)
- {
- Tuple<DateTime, DateTime> tuple = Tuple.Create<DateTime, DateTime>(DateTime.Now, DateTime.Now.AddMilliseconds(expireTime));
- this._myCacheExpire.TryAdd(cacheItemName, tuple);
- }
- }
- }
- return cacheObject;
- }
- /// <summary>
- /// 设置缓存
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="cacheTimeInMinutes"></param>
- public void SetValueToCache(string key, object value, bool isSetExpire = true, long expireTime = DefaultExpireTime)
- {
- if (Contains(key))
- {
- Remove(key);
- }
- this._myCache.TryAdd(key, value);
- if (isSetExpire)
- {
- Tuple<DateTime, DateTime> tuple = Tuple.Create<DateTime, DateTime>(DateTime.Now, DateTime.Now.AddMilliseconds(expireTime));
- this._myCacheExpire.TryAdd(key, tuple);
- }
- }
- /// <summary>
- /// 获取缓存
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public object GetValueFromCache(string key)
- {
- if (Contains(key))
- {
- this._myCache.TryGetValue(key, out var cacheObj);
- return cacheObj;
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 验证缓存是否存在
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public bool Contains(string key)
- {
- return this._myCache.ContainsKey(key);
- }
- /// <summary>
- /// 删除制定缓存
- /// </summary>
- /// <param name="key"></param>
- public void Remove(string key)
- {
- this._myCache.TryRemove(key, out var cacheObj);
- this._myCacheExpire.TryRemove(key, out var expireObj);
- }
- /// <summary>
- /// 移除所有缓存
- /// </summary>
- public void RemoveAll()
- {
- this._myCache.Clear();
- this._myCacheExpire.Clear();
- }
- }
- }
|