Exemplo n.º 1
0
        /// <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));

            var result = BaseGet <T>(cacheKey);

            if (result.HasValue)
            {
                return(result);
            }

            var flag = _memcachedClient.Store(Enyim.Caching.Memcached.StoreMode.Add, this.HandleCacheKey($"{cacheKey}_Lock"), 1, TimeSpan.FromMilliseconds(_options.LockMs));

            if (!flag)
            {
                System.Threading.Thread.Sleep(_options.SleepMs);
                return(Get(cacheKey, dataRetriever, expiration));
            }

            var item = dataRetriever();

            if (item != null || _options.CacheNulls)
            {
                this.Set(cacheKey, item, expiration);
                _memcachedClient.Remove(this.HandleCacheKey($"{cacheKey}_Lock"));
                return(new CacheValue <T>(item, true));
            }
            else
            {
                _memcachedClient.Remove(this.HandleCacheKey($"{cacheKey}_Lock"));
                return(CacheValue <T> .NoValue);
            }
        }
Exemplo n.º 2
0
        /// <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));

            if (_memcachedClient.Get(this.HandleCacheKey(cacheKey)) is T result)
            {
                CacheStats.OnHit();

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

                return(new CacheValue <T>(result, true));
            }

            CacheStats.OnMiss();

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

            var flag = _memcachedClient.Store(Enyim.Caching.Memcached.StoreMode.Add, this.HandleCacheKey($"{cacheKey}_Lock"), 1, TimeSpan.FromMilliseconds(_options.LockMs));

            if (!flag)
            {
                System.Threading.Thread.Sleep(_options.SleepMs);
                return(Get(cacheKey, dataRetriever, expiration));
            }

            var item = dataRetriever();

            if (item != null)
            {
                this.Set(cacheKey, item, expiration);
                _memcachedClient.Remove(this.HandleCacheKey($"{cacheKey}_Lock"));
                return(new CacheValue <T>(item, true));
            }
            else
            {
                _memcachedClient.Remove(this.HandleCacheKey($"{cacheKey}_Lock"));
                return(CacheValue <T> .NoValue);
            }
        }
        /// <summary>
        /// Remove the specified cacheKey.
        /// </summary>
        /// <returns>The remove.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public void Remove(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            _memcachedClient.Remove(this.HandleCacheKey(cacheKey));
        }