Esempio n. 1
0
        /// <summary>
        /// 异步将对象存入缓存中,使用功能配置
        /// </summary>
        public static Task SetAsync(this IDistributedCache cache, string key, object value, IFunction function)
        {
            key.CheckNotNullOrEmpty(nameof(key));
            value.CheckNotNull(nameof(value));
            function.CheckNotNull(nameof(function));

            DistributedCacheEntryOptions options = function.ToCacheOptions();

            if (options == null)
            {
                return(Task.FromResult(0));
            }
            return(cache.SetAsync(key, value, options));
        }
Esempio n. 2
0
        /// <summary>
        /// 将对象存入缓存中,使用功能配置
        /// </summary>
        public static void Set(this IDistributedCache cache, string key, object value, IFunction function)
        {
            key.CheckNotNullOrEmpty(nameof(key));
            value.CheckNotNull(nameof(value));
            function.CheckNotNull(nameof(function));

            DistributedCacheEntryOptions options = function.ToCacheOptions();

            if (options == null)
            {
                return;
            }
            cache.Set(key, value, options);
        }
Esempio n. 3
0
        /// <summary>
        /// 将<see cref="IFunction"/>的缓存配置转换为<see cref="DistributedCacheEntryOptions"/>
        /// </summary>
        public static DistributedCacheEntryOptions ToCacheOptions(this IFunction function)
        {
            function.CheckNotNull(nameof(function));
            if (function.CacheExpirationSeconds == 0)
            {
                return(null);
            }
            DistributedCacheEntryOptions options = new DistributedCacheEntryOptions();

            if (!function.IsCacheSliding)
            {
                options.SetAbsoluteExpiration(TimeSpan.FromSeconds(function.CacheExpirationSeconds));
            }
            else
            {
                options.SetSlidingExpiration(TimeSpan.FromSeconds(function.CacheExpirationSeconds));
            }
            return(options);
        }