Пример #1
0
        public static TValue GetSet <TValue>(string script, Func <TValue> func)
        {
            var hashKey = script.GetHashCode();
            var expired = TimeSpan.FromMinutes(15);

            var triggerScriptInCache = MemoryCacheHelper.Get <int, TValue>(hashKey);

            if (triggerScriptInCache == null)
            {
                triggerScriptInCache = MemoryCacheHelper.Put <int, TValue>(hashKey, func(), expired);
            }

            return(triggerScriptInCache);
        }
        public static void Put <T>(DbContext dbContext, string key, T value, TimeSpan expiredTime)
        {
            switch (dbContext.CacheMediaType)
            {
            case CacheMediaType.Local:
                MemoryCacheHelper.Put(key, value, expiredTime);
                break;

            case CacheMediaType.Redis:
                break;

            default:
                break;
            }
        }
Пример #3
0
        /// <summary>
        /// Get Configs
        /// </summary>
        /// <returns></returns>
        private static IEnumerable <T> GetConfigs()
        {
            //modifyTime reagard as config version
            DateTime configDbVersion = ModifyTime;

            //if memory cache exist
            if (MemoryCacheHelper.Exist(versionCacheKey) && MemoryCacheHelper.Exist(versionCacheKey))
            {
                //if cache version >= db version,cache is available.
                if (Convert.ToDateTime(MemoryCacheHelper.Get <int, DateTime>(versionCacheKey)) >= configDbVersion)
                {
                    return(MemoryCacheHelper.Get <int, IEnumerable <T> >(configCacheKey));
                }
            }
            //if memory cache not exist
            var dbConfig = GetConfigsFromDb();

            MemoryCacheHelper.Put(configCacheKey, dbConfig);
            MemoryCacheHelper.Put(versionCacheKey, configDbVersion);
            return(dbConfig);
        }
Пример #4
0
        public void Put <T>(string key, T value, TimeSpan expiredTime)
        {
            switch (_CacheOptions.CacheMediaType)
            {
            case CacheMediaType.Local:
                MemoryCacheHelper.Put(key, value, expiredTime);
                break;

            case CacheMediaType.Redis:
                try
                {
                    GetRedisCacheProvider().Set(key, JsonConvert.SerializeObject(value), expiredTime);
                }
                catch (ArgumentException argEx)
                {
                    throw argEx;
                }
                break;

            default:
                break;
            }
        }
Пример #5
0
 public void Put <T>(string key, T value, TimeSpan expiredTime)
 {
     MemoryCacheHelper.Put <string, T>(key, value, expiredTime);
 }