Esempio n. 1
0
        /// <summary>
        /// Creates a new <see cref="FusionCacheEntryOptions"/> object by duplicating all the options of the current one.
        /// </summary>
        /// <param name="duration">A custom <see cref="Duration"/> that, if specified, will overwrite the current one.</param>
        /// <param name="includeOptionsModifiers">If false, the <see cref="MemoryOptionsModifier"/> and <see cref="DistributedOptionsModifier"/> will not be duplicated.</param>
        /// <returns>The newly created <see cref="FusionCacheEntryOptions"/> object.</returns>
        public FusionCacheEntryOptions Duplicate(TimeSpan?duration = null, bool includeOptionsModifiers = true)
        {
            var res = new FusionCacheEntryOptions()
            {
                Duration    = duration ?? Duration,
                LockTimeout = LockTimeout,

                Size              = Size,
                Priority          = Priority,
                JitterMaxDuration = JitterMaxDuration,

                IsFailSafeEnabled        = IsFailSafeEnabled,
                FailSafeMaxDuration      = FailSafeMaxDuration,
                FailSafeThrottleDuration = FailSafeThrottleDuration,

                FactorySoftTimeout = FactorySoftTimeout,
                FactoryHardTimeout = FactoryHardTimeout,
                AllowTimedOutFactoryBackgroundCompletion = AllowTimedOutFactoryBackgroundCompletion,

                DistributedCacheSoftTimeout = DistributedCacheSoftTimeout,
                DistributedCacheHardTimeout = DistributedCacheHardTimeout,
                AllowBackgroundDistributedCacheOperations = AllowBackgroundDistributedCacheOperations
            };

            if (includeOptionsModifiers)
            {
                res.MemoryOptionsModifier      = MemoryOptionsModifier;
                res.DistributedOptionsModifier = DistributedOptionsModifier;
            }

            return(res);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new instance of a <see cref="FusionCacheOptions"/> object.
        /// </summary>
        public FusionCacheOptions()
        {
            DefaultEntryOptions = new FusionCacheEntryOptions();

            // LOG LEVELS
            SerializationErrorsLogLevel = LogLevel.Error;
            DistributedCacheSyntheticTimeoutsLogLevel = LogLevel.Warning;
            DistributedCacheErrorsLogLevel            = LogLevel.Warning;
            FactorySyntheticTimeoutsLogLevel          = LogLevel.Warning;
            FactoryErrorsLogLevel      = LogLevel.Warning;
            FailSafeActivationLogLevel = LogLevel.Warning;
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new <see cref="FusionCacheEntry{TValue}"/> instance from a value and some options.
        /// </summary>
        /// <param name="value">The value to be cached.</param>
        /// <param name="options">The <see cref="FusionCacheEntryOptions"/> object to configure the entry.</param>
        /// <param name="isFromFailSafe">Indicates if the value comes from a fail-safe activation.</param>
        /// <returns>The newly created entry.</returns>
        public static FusionCacheEntry <TValue> CreateFromOptions(TValue value, FusionCacheEntryOptions options, bool isFromFailSafe)
        {
            DateTimeOffset exp;

            if (isFromFailSafe)
            {
                exp = DateTimeOffset.UtcNow.AddMilliseconds(options.GetJitterDurationMs()) + options.FailSafeThrottleDuration;
            }
            else
            {
                exp = DateTimeOffset.UtcNow.AddMilliseconds(options.GetJitterDurationMs()) + options.Duration;
            }

            return(new FusionCacheEntry <TValue>(
                       value,
                       exp,
                       options.Priority,
                       isFromFailSafe
                       ));
        }
Esempio n. 4
0
        private FusionCacheEntry <TValue>?MaybeGetFallbackEntry <TValue>(string operationId, string key, FusionCacheEntry <TValue>?distributedEntry, FusionCacheEntry <TValue>?memoryEntry, FusionCacheEntryOptions options, out bool failSafeActivated)
        {
            failSafeActivated = false;

            if (options.IsFailSafeEnabled)
            {
                if (_logger?.IsEnabled(LogLevel.Trace) ?? false)
                {
                    _logger.LogTrace("FUSION (K={CacheKey} OP={CacheOperationId}): trying to activate FAIL-SAFE", key, operationId);
                }
                if (distributedEntry is object)
                {
                    // FAIL SAFE (FROM DISTRIBUTED)
                    if (_logger?.IsEnabled(_options.FailSafeActivationLogLevel) ?? false)
                    {
                        _logger.Log(_options.FailSafeActivationLogLevel, "FUSION (K={CacheKey} OP={CacheOperationId}): FAIL-SAFE activated (from distributed)", key, operationId);
                    }
                    failSafeActivated = true;
                    return(distributedEntry);
                }
                else if (memoryEntry is object)
                {
                    // FAIL SAFE (FROM MEMORY)
                    if (_logger?.IsEnabled(_options.FailSafeActivationLogLevel) ?? false)
                    {
                        _logger.Log(_options.FailSafeActivationLogLevel, "FUSION (K={CacheKey} OP={CacheOperationId}): FAIL-SAFE activated (from memory)", key, operationId);
                    }
                    failSafeActivated = true;
                    return(memoryEntry);
                }
                else
                {
                    if (_logger?.IsEnabled(_options.FailSafeActivationLogLevel) ?? false)
                    {
                        _logger.Log(_options.FailSafeActivationLogLevel, "FUSION (K={CacheKey} OP={CacheOperationId}): unable to activate FAIL-SAFE (no entries in memory or distributed)", key, operationId);
                    }
                    return(null);
                }
            }
            else
            {
                if (_logger?.IsEnabled(LogLevel.Trace) ?? false)
                {
                    _logger.LogTrace("FUSION (K={CacheKey} OP={CacheOperationId}): FAIL-SAFE not enabled", key, operationId);
                }
                return(null);
            }
        }
Esempio n. 5
0
        private void SetMemoryEntry <TValue>(string operationId, string key, FusionCacheEntry <TValue> entry, FusionCacheEntryOptions options)
        {
            var memoryOptions = options.ToMemoryCacheEntryOptions();

            options.MemoryOptionsModifier?.Invoke(memoryOptions, entry.Value);

            if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
            {
                _logger.LogDebug("FUSION (K={CacheKey} OP={CacheOperationId}): saving entry in memory {Options} {Entry}", key, operationId, memoryOptions.ToLogString(), entry.ToLogString());
            }

            _memoryCache.Set <FusionCacheEntry <TValue> >(key, entry, memoryOptions);
        }