/// <summary>
        /// Asynchronously executes the plan
        /// </summary>
        public virtual async Task <CachedValue <T> > ExecuteAsync()
        {
            CachedValue <T> cachedValue = null;

            try
            {
                cachedValue = Cache.Get <T>(Key, Region);

                CacheValidationResult validationResult = await ValidateCachedValueAsync(cachedValue);

                if (validationResult != CacheValidationResult.Valid && (validationResult == CacheValidationResult.Invalid || cachedValue == null))
                {
                    cachedValue = await RetrieveCachedValueAsync(previousCachedValue : cachedValue);
                }

                return(cachedValue);
            }
            catch (FluentCache.FluentCacheException cacheException)
            {
                if (!ExceptionHandler.TryHandleCachingFailure(cacheException))
                {
                    throw;
                }
                else
                {
                    return(null);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Asynchronously executes the plan
        /// </summary>
        public virtual async Task <CachedValue <T> > ExecuteAsync()
        {
            CachedValue <T>       cachedValue      = cachedValue = Cache.Get <T>(Key, Region);
            CacheValidationResult validationResult = await ValidateCachedValueAsync(cachedValue);

            if (validationResult != CacheValidationResult.Valid && (validationResult == CacheValidationResult.Invalid || cachedValue == null))
            {
                cachedValue = await RetrieveCachedValueAsync(previousCachedValue : cachedValue);
            }

            return(cachedValue);
        }
Beispiel #3
0
        /// <summary>
        /// Executes the plan
        /// </summary>
        public virtual CachedValue <T> Execute()
        {
            CachedValue <T>       cachedValue      = Cache.Get <T>(Key, Region);
            CacheValidationResult validationResult = ValidateCachedValue(cachedValue);

            if (validationResult != CacheValidationResult.Valid && (validationResult == CacheValidationResult.Invalid || cachedValue == null))
            {
                cachedValue = RetrieveCachedValue(previousCachedValue: cachedValue);
            }

            return(cachedValue);
        }
        /// <summary>
        /// Validates the cached value
        /// </summary>
        protected virtual CacheValidationResult ValidateCachedValue(CachedValue <T> existingCachedValue)
        {
            if (existingCachedValue == null)
            {
                return(CacheValidationResult.Unknown);
            }

            CacheValidationResult result = _cacheStrategy.Validate(existingCachedValue);

            if (result == CacheValidationResult.Valid)
            {
                Cache.MarkAsValidated(Key, Region);
            }

            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Asynchronously executes the plan
        /// </summary>
        public override async Task <CachedValue <T> > ExecuteAsync()
        {
            //If the circuit is broken then we won't even try to retrieve the value from the cache
            if (_circuitBreaker.IsBroken)
            {
                return(null);
            }

            CachedValue <T> cachedValue = null;

            try
            {
                //Get the value from the cache
                cachedValue = Cache.Get <T>(Key, Region);

                //validate the value
                CacheValidationResult validationResult = await ValidateCachedValueAsync(cachedValue);

                //if the value is invalid or non-existing then retrieve the value from the cache
                if (validationResult != CacheValidationResult.Valid && (validationResult == CacheValidationResult.Invalid || cachedValue == null))
                {
                    cachedValue = await RetrieveCachedValueAsync(previousCachedValue : cachedValue);
                }

                //We successfully retrieved the value from the cache, so reset the circuit
                _circuitBreaker.Reset();

                return(cachedValue);
            }
            catch (FluentCache.FluentCacheException cacheException)
            {
                //attempt to handle the caching failure
                if (ExceptionHandler.TryHandleCachingFailure(cacheException))
                {
                    //try to break the circuit
                    _circuitBreaker.TryBreak(cacheException);
                    return(null);
                }
                else
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Asynchronously validates the cached value
        /// </summary>
        protected virtual async Task <CacheValidationResult> ValidateCachedValueAsync(CachedValue <T> existingCachedValue)
        {
            if (_cacheStrategyAsync == null)
            {
                throw new InvalidOperationException("Specified cache strategy must be an instance of ICacheStrategyAsync<T>");
            }

            if (existingCachedValue == null)
            {
                return(CacheValidationResult.Unknown);
            }

            CacheValidationResult result = await _cacheStrategyAsync.ValidateAsync(existingCachedValue);

            if (result == CacheValidationResult.Valid)
            {
                Cache.MarkAsValidated(Key, Region);
            }

            return(result);
        }