Exemplo n.º 1
0
        /// <summary>
        /// 根据枚举获取缓存时间
        /// </summary>
        /// <param name="cacheEnum"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        private static TimeSpan GetCacheTime(CacheTimeEnum cacheEnum, TimeSpan?time = null)
        {
            switch (cacheEnum)
            {
            case CacheTimeEnum.Second:
                return(TimeSpan.FromSeconds(1));

            case CacheTimeEnum.Rapid:
                return(TimeSpan.FromSeconds(5));

            case CacheTimeEnum.Short:
                return(TimeSpan.FromMinutes(10));

            case CacheTimeEnum.Quarter:
                return(TimeSpan.FromMinutes(15));

            case CacheTimeEnum.HalfHour:
                return(TimeSpan.FromMinutes(30));

            case CacheTimeEnum.Hour:
                return(TimeSpan.FromHours(1));

            case CacheTimeEnum.Day:
                return(TimeSpan.FromDays(1));

            case CacheTimeEnum.HalfWeek:
                return(TimeSpan.FromDays(3));

            case CacheTimeEnum.Week:
                return(TimeSpan.FromDays(7));

            case CacheTimeEnum.Month:
                return(TimeSpan.FromDays(30));

            case CacheTimeEnum.Customize:
            default:
                return(time ?? TimeSpan.FromMinutes(10));
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 设置缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="cacheEnum"></param>
 /// <param name="time"></param>
 /// <returns></returns>
 public static bool SetCacheValue <T>(string key, T value, CacheTimeEnum cacheEnum, TimeSpan?time = null)
 {
     return(Set(key, value, GetCacheTime(cacheEnum, time)));
 }
Exemplo n.º 3
0
 /// <summary>
 /// 设置缓存异步方法
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="cacheEnum"></param>
 /// <param name="time">只有cacheEnum传Customize才有效</param>
 /// <returns></returns>
 public static async Task <bool> SetCacheValueAsync <T>(string key, T value, CacheTimeEnum cacheEnum, TimeSpan?time = null)
 {
     return(await SetAsync(key, value, GetCacheTime(cacheEnum, time)));
 }
Exemplo n.º 4
0
 /// <summary>
 /// 获取缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="cacheEnum"></param>
 /// <param name="func"></param>
 /// <param name="time">只有cacheEnum传Customize才有效</param>
 /// <returns></returns>
 public static T GetFromCache <T>(string key, Func <T> func = null, CacheTimeEnum cacheEnum = CacheTimeEnum.Customize, TimeSpan?time = null)
 {
     return(func == null?Get <T>(key) : GetOrSet(key, func, GetCacheTime(cacheEnum, time)));
 }
Exemplo n.º 5
0
 /// <summary>
 /// 获取缓存异步方法
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="cacheEnum"></param>
 /// <param name="func"></param>
 /// <param name="time">只有cacheEnum传Customize才有效</param>
 /// <returns></returns>
 public static async Task <T> GetFromCacheAsync <T>(string key, Func <Task <T> > func = null, CacheTimeEnum cacheEnum = CacheTimeEnum.Customize, TimeSpan?time = null)
 {
     return(func == null ? await GetAsync <T>(key) : await GetOrSetAsync(key, func, GetCacheTime(cacheEnum, time)));
 }