Пример #1
0
        public void Remove(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            _cache.Remove(key);
        }
        /// <summary>
        /// Get the specified cacheKey, dataRetriever and expiration.
        /// </summary>
        /// <returns>The get.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <param name="dataRetriever">Data retriever.</param>
        /// <param name="expiration">Expiration.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public override CacheValue <T> BaseGet <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
            ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

            ////mutex key
            //Lock(cacheKey);

            var result = _cache.Get <T>(cacheKey);

            if (result.HasValue)
            {
                if (_options.EnableLogging)
                {
                    _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}");
                }

                CacheStats.OnHit();

                return(result);
            }

            CacheStats.OnMiss();

            if (_options.EnableLogging)
            {
                _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}");
            }

            if (!_cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs)))
            {
                System.Threading.Thread.Sleep(_options.SleepMs);
                return(Get(cacheKey, dataRetriever, expiration));
            }

            var res = dataRetriever();

            if (res != null)
            {
                Set(cacheKey, res, expiration);
                //remove mutex key
                _cache.Remove($"{cacheKey}_Lock");

                return(new CacheValue <T>(res, true));
            }
            else
            {
                //remove mutex key
                _cache.Remove($"{cacheKey}_Lock");
                return(CacheValue <T> .NoValue);
            }
        }
        /// <summary>
        /// Get the specified cacheKey, dataRetriever and expiration.
        /// </summary>
        /// <returns>The get.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <param name="dataRetriever">Data retriever.</param>
        /// <param name="expiration">Expiration.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public override CacheValue <T> BaseGet <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
            ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));

            ////mutex key
            //Lock(cacheKey);

            var result = _cache.Get <T>(cacheKey);

            if (result.HasValue)
            {
                OnCacheMiss(cacheKey);

                return(result);
            }

            OnCacheMiss(cacheKey);

            if (!_cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs)))
            {
                System.Threading.Thread.Sleep(_options.SleepMs);
                return(Get(cacheKey, dataRetriever, expiration));
            }

            var res = dataRetriever();

            if (res != null || _options.CacheNulls)
            {
                Set(cacheKey, res, expiration);
                //remove mutex key
                _cache.Remove($"{cacheKey}_Lock");

                return(new CacheValue <T>(res, true));
            }
            else
            {
                //remove mutex key
                _cache.Remove($"{cacheKey}_Lock");
                return(CacheValue <T> .NoValue);
            }
        }
Пример #4
0
        public void Remove(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            _caching.Remove(cacheKey);
        }