예제 #1
0
        /// <summary>
        /// Get Cache value if key exist and cast it to <see cref="TItem"/>. Or create cache entry
        /// </summary>
        /// <param name="cache">Cache instance</param>
        /// <param name="key">Cache key</param>
        /// <param name="fabric">Fabric for build Cache value</param>
        /// <param name="ttl">Ttl for cache entry <see cref="CacheEntryOptions.Ttl"/></param>
        /// <typeparam name="TItem">Cast value to this Type</typeparam>
        /// <returns>Value of cache casted to <see cref="TItem"/></returns>
        public static TItem GetOrCreate <TItem>(this ICache cache, string key, Func <TItem> fabric, TimeSpan ttl)
        {
            var options = new CacheEntryOptions()
            {
                Ttl = ttl
            };

            return(cache.GetOrCreate(key, fabric, options));
        }
예제 #2
0
        /// <summary>
        /// Create or update cache entry and set ttl on entry
        /// </summary>
        /// <param name="cache">Cache instance</param>
        /// <param name="key">Cache key</param>
        /// <param name="value">Cache value</param>
        /// <param name="ttl">Ttl for cache entry <see cref="CacheEntryOptions.Ttl"/></param>
        public static void Set(this ICache cache, string key, object value, TimeSpan ttl)
        {
            var entryOptions = new CacheEntryOptions()
            {
                Ttl = ttl,
            };

            var entry = new CacheEntry(key, value, entryOptions);

            cache.Set(key, entry);
        }
예제 #3
0
파일: CacheEntry.cs 프로젝트: deminds/Cache
        /// <summary>
        /// Constructor for create new cache entry
        /// </summary>
        /// <param name="key">Key of cache</param>
        /// <param name="value">Value of cache</param>
        /// <param name="options"><see cref="CacheEntryOptions"/> of <see cref="CacheEntry"/></param>
        public CacheEntry(string key, object value, CacheEntryOptions options)
        {
            Key      = key ?? throw new ArgumentNullException(nameof(key));
            Value    = value ?? throw new ArgumentNullException(nameof(value));
            _options = options;

            _options = options ?? throw new ArgumentNullException(nameof(options));

            if (options.Ttl != null)
            {
                _expireDateTime = DateTime.Now + options.Ttl;
            }
        }
예제 #4
0
        /// <summary>
        /// Create or update cache entry and set ttl and callback on entry
        /// </summary>
        /// <param name="cache">Cache instance</param>
        /// <param name="key">Cache key</param>
        /// <param name="value">Cache value</param>
        /// <param name="ttl">Ttl for cache entry <see cref="CacheEntryOptions.Ttl"/></param>
        /// <param name="updateDataCallback">Callback for execute after <see cref="ttl"/> expired</param>
        public static void Set(this ICache cache, string key, object value, TimeSpan ttl,
                               Func <object> updateDataCallback)
        {
            var entryOptions = new CacheEntryOptions()
            {
                Ttl = ttl,
                UpdateDataCallback = updateDataCallback,
                IsAutoDeleted      = false
            };

            var entry = new CacheEntry(key, value, entryOptions);

            cache.Set(key, entry);
        }
예제 #5
0
        /// <summary>
        /// Get Cache value if key exist and cast it to <see cref="TItem"/>. Or create cache entry
        /// </summary>
        /// <param name="cache">Cache instance</param>
        /// <param name="key">Cache key</param>
        /// <param name="fabric">Fabric for build Cache value</param>
        /// <param name="options"><see cref="CacheEntryOptions"/> specified for <see cref="CacheEntry"/> if it not in Cache yet</param>
        /// <typeparam name="TItem">Type of Cache value</typeparam>
        /// <returns>Value of cache casted to <see cref="TItem"/></returns>
        public static TItem GetOrCreate <TItem>(this ICache cache, string key, Func <TItem> fabric, CacheEntryOptions options)
        {
            if (cache.TryGet <TItem>(key, out var value))
            {
                return(value);
            }

            value = fabric();
            var cacheEntry = new CacheEntry(key, value, options);

            cache.Set(key, cacheEntry);

            return(value);
        }