示例#1
0
 /// <summary>
 /// 将 CacheOptionEntry 对象实例,转化为 MemoryCacheEntryOptions
 /// </summary>
 /// <param name="entry"></param>
 /// <returns></returns>
 public static MemoryCacheEntryOptions ConvertToMemoryCacheEntryOptions(this CacheOptionEntry entry)
 {
     return(new MemoryCacheEntryOptions
     {
         AbsoluteExpiration = entry.AbsoluteExpiration,
         AbsoluteExpirationRelativeToNow = entry.AbsoluteExpirationRelativeToNow,
         SlidingExpiration = entry.SlidingExpiration
     });
 }
示例#2
0
 /// <summary>
 /// 将 CacheOptionEntry 对象实例,转化为 DistributedCacheEntryOptions
 /// </summary>
 /// <param name="entry"></param>
 /// <returns></returns>
 public static DistributedCacheEntryOptions ConvertToDistributedCacheEntry(this CacheOptionEntry entry)
 {
     return(new DistributedCacheEntryOptions
     {
         AbsoluteExpiration = entry.AbsoluteExpiration,
         AbsoluteExpirationRelativeToNow = entry.AbsoluteExpirationRelativeToNow,
         SlidingExpiration = entry.SlidingExpiration
     });
 }
示例#3
0
        /// <summary>
        /// 将 CacheOptionEntry 对象实例,转化为相对与当前时间的 TimeSpan
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException">当 CacheOptionEntry 对象的
        /// <see cref="CacheOptionEntry.AbsoluteExpirationRelativeToNow"/>属性和<see cref="CacheOptionEntry.AbsoluteExpiration"/>都为null时</exception>
        public static TimeSpan ConvertToTimeSpanRelativeToNow(this CacheOptionEntry entry)
        {
            if (entry.AbsoluteExpirationRelativeToNow.HasValue)
            {
                return(entry.AbsoluteExpirationRelativeToNow.Value);
            }
            if (entry.AbsoluteExpiration.HasValue)
            {
                return(entry.AbsoluteExpiration.Value - DateTimeOffset.UtcNow);
            }
            if (entry.SlidingExpiration.HasValue)
            {
                throw new NotSupportedException($"Unable to convert {nameof(CacheOptionEntry.SlidingExpiration)} to TimeSpan for relative to now. This operation is not supported.");
            }

            throw new NotSupportedException($"Unable to convert {nameof(CacheOptionEntry)} to TimeSpan for relative to now. This operation is not supported.");
        }
示例#4
0
 /// <summary>
 /// 获取缓存,若不存在则创建
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="cache"></param>
 /// <param name="key"></param>
 /// <param name="func"></param>
 /// <param name="entry"></param>
 /// <returns></returns>
 public static T GetOrCreate <T>(this IMemoryCache cache, object key, Func <T> func, CacheOptionEntry entry)
 {
     return(cache.GetOrCreate(key, options =>
     {
         options.AbsoluteExpiration = entry.AbsoluteExpiration;
         options.AbsoluteExpirationRelativeToNow = entry.AbsoluteExpirationRelativeToNow;
         options.SlidingExpiration = entry.SlidingExpiration;
         return func();
     }));
 }
示例#5
0
 /// <summary>
 /// 设置滑动过期时间
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="offset"></param>
 /// <returns></returns>
 public static CacheOptionEntry SetSlidingExpiration(this CacheOptionEntry entry, TimeSpan offset)
 {
     entry.SlidingExpiration = new TimeSpan?(offset);
     return(entry);
 }
示例#6
0
 /// <summary>
 /// 设置绝对过期时间
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="absolute"></param>
 /// <returns></returns>
 public static CacheOptionEntry SetAbsoluteExpiration(this CacheOptionEntry entry, DateTimeOffset absolute)
 {
     entry.AbsoluteExpiration = new DateTimeOffset?(absolute);
     return(entry);
 }
示例#7
0
 /// <summary>
 /// 设置以对当前时间为基础时间的绝对过期时间
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="relative"></param>
 /// <returns></returns>
 public static CacheOptionEntry SetAbsoluteExpiration(this CacheOptionEntry entry, TimeSpan relative)
 {
     entry.AbsoluteExpirationRelativeToNow = new TimeSpan?(relative);
     return(entry);
 }