コード例 #1
0
        private string GetFromDistributedCache <T>(string key, Func <T> factory)
        {
            bool readSucceeded = true;

            try
            {
                string cachedItem = string.Empty;
                _readCircuitBreaker.ExecuteAction(() =>
                {
                    cachedItem = _distributedCache.GetString(key);
                });

                if (cachedItem != null)
                {
                    return(cachedItem);
                }
            }
            catch (Exception ex)
            {
                readSucceeded = false;
            }

            var item      = factory.Invoke();
            var cacheItem = _converter.Serialize(new CacheWrapper <T>(item));

            try
            {
                // if the read circuit is broken then writes will also be broken
                if (readSucceeded && _readCircuitBreaker.IsClosed)
                {
                    var cacheEntryOptions = new DistributedCacheEntryOptions {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(DISTRIBUTED_TTL_SECONDS)
                    };
                    _writeCircuitBreaker.ExecuteAction(() =>
                    {
                        _distributedCache.SetString(key, cacheItem, cacheEntryOptions);
                    });
                }
            }
            catch (Exception ex)
            {
                // log exception here
            }

            return(cacheItem);
        }
コード例 #2
0
 private string GetSerializedCacheWrapper <T>(T cacheItem)
 {
     return(_converter.Serialize(new CacheWrapper <T>(cacheItem)));
 }