Esempio n. 1
0
 /// <summary>
 /// 获取键值对
 /// </summary>
 internal static object GetValue(string key, bool onlyMemoryCache = false)
 {
     lock (_dictLocksForReadCache.GetOrAdd(key, key))
     {
         object memoryCacheValue = MemoryCacheUtil.GetValue(key);
         if (memoryCacheValue != null)
         {
             return(memoryCacheValue);
         }
         else
         {
             if (!onlyMemoryCache)
             {
                 CacheData cacheData = FileCacheUtil.GetCacheData(key);
                 if (cacheData != null)
                 {
                     int expirationSeconds = (int)cacheData.updateTime.AddSeconds(cacheData.expirationSeconds).Subtract(DateTime.Now).TotalSeconds; //剩余过期时间
                     if (cacheData.expirationSeconds == 0)
                     {
                         expirationSeconds = 0;                                         //永不过期的情况
                     }
                     MemoryCacheUtil.SetValue(key, cacheData.value, expirationSeconds); //登录系统后(非首次登录),MemoryCache为空,FileCache不为空
                     return(cacheData.value);
                 }
             }
         }
         return(null);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// 保存键值对
 /// </summary>
 internal static void SetValue(string key, object value, bool onlyMemoryCache = false, int expirationSeconds = 0)
 {
     MemoryCacheUtil.SetValue(key, value, expirationSeconds);
     if (!onlyMemoryCache) //有的数据是不能缓存在文件中的,只能缓存在内存中
     {
         FileCacheUtil.SetValue(key, value, expirationSeconds);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 获取并缓存数据
 /// 高并发的情况建议使用此重载函数,防止重复写入内存缓存
 /// </summary>
 /// <param name="cacheKey">键</param>
 /// <param name="func">在此方法中初始化数据</param>
 /// <param name="expirationSeconds">缓存过期时间(秒),0表示永不过期</param>
 /// <param name="refreshCache">立即刷新缓存</param>
 public static T TryGetValue <T>(string cacheKey, Func <T> func, int expirationSeconds = 0, bool refreshCache = false)
 {
     lock (_dictLocksForReadCache.GetOrAdd(cacheKey, cacheKey))
     {
         object cacheValue = MemoryCacheUtil.GetValue(cacheKey);
         if (cacheValue != null && !refreshCache)
         {
             return((T)cacheValue);
         }
         else
         {
             T value = func();
             MemoryCacheUtil.SetValue(cacheKey, value, expirationSeconds);
             return(value);
         }
     }
 }
Esempio n. 4
0
 /// <summary>
 /// 获取实体类属性
 /// </summary>
 private static List <PropertyInfo> GetEntityPropertiesByCache(Type baseType)
 {
     lock (_lock)
     {
         List <PropertyInfo> result = null;
         string cacheKey            = "ModelHelper.Convert.PropertyInfo." + baseType.FullName;
         object cacheValue          = MemoryCacheUtil.GetValue(cacheKey);
         if (cacheValue != null)
         {
             result = cacheValue as List <PropertyInfo>;
         }
         else
         {
             result = GetEntityProperties(baseType);
             MemoryCacheUtil.SetValue(cacheKey, result);
         }
         return(result);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// 删除全部缓存
 /// </summary>
 public static void DeleteAll()
 {
     MemoryCacheUtil.DeleteAll();
     FileCacheUtil.DeleteAll();
 }
Esempio n. 6
0
 /// <summary>
 /// 删除缓存
 /// </summary>
 public static void Delete(string key)
 {
     MemoryCacheUtil.Delete(key);
     FileCacheUtil.Delete(key);
 }