Пример #1
0
 public static FusionCacheEntryOptions SetFactoryTimeouts(this FusionCacheEntryOptions options, int?softTimeoutMs = null, int?hardTimeoutMs = null, bool?keepTimedOutFactoryResult = null)
 {
     if (softTimeoutMs is object)
     {
         options.FactorySoftTimeout = TimeSpan.FromMilliseconds(softTimeoutMs.Value);
     }
     if (hardTimeoutMs is object)
     {
         options.FactoryHardTimeout = TimeSpan.FromMilliseconds(hardTimeoutMs.Value);
     }
     if (keepTimedOutFactoryResult is object)
     {
         options.AllowTimedOutFactoryBackgroundCompletion = keepTimedOutFactoryResult.Value;
     }
     return(options);
 }
Пример #2
0
        /// <summary>
        /// Creates a new <see cref="FusionCacheMemoryEntry"/> 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 FusionCacheMemoryEntry CreateFromOptions(object?value, FusionCacheEntryOptions options, bool isFromFailSafe)
        {
            if (options.IsFailSafeEnabled == false)
            {
                return(new FusionCacheMemoryEntry(value, null));
            }

            DateTimeOffset exp;

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

            return(new FusionCacheMemoryEntry(value, new FusionCacheEntryMetadata(exp, isFromFailSafe)));
        }
Пример #3
0
        public void SetDistributedEntry <TValue>(string operationId, string key, FusionCacheEntry <TValue> entry, FusionCacheEntryOptions options, CancellationToken token = default)
        {
            if (IsCurrentlyUsable() == false)
            {
                return;
            }

            token.ThrowIfCancellationRequested();

            var distributedOptions = options.ToDistributedCacheEntryOptions();

            options.DistributedOptionsModifier?.Invoke(distributedOptions, entry.Value);

            ExecuteOperation(
                operationId,
                key,
                ct =>
            {
                byte[]? data;
                try
                {
                    if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
                    {
                        _logger.Log(LogLevel.Debug, "FUSION (K={CacheKey} OP={CacheOperationId}): serializing the entry {Entry}", key, operationId, entry.ToLogString());
                    }

                    data = Serializer !.Serialize(entry);
                }
Пример #4
0
        public async Task SetDistributedEntryAsync <TValue>(string operationId, string key, FusionCacheEntry <TValue> entry, FusionCacheEntryOptions options, CancellationToken token)
        {
            if (IsCurrentlyUsable() == false)
            {
                return;
            }

            token.ThrowIfCancellationRequested();

            var distributedOptions = options.ToDistributedCacheEntryOptions();

            options.DistributedOptionsModifier?.Invoke(distributedOptions, entry.Value);

            await ExecuteOperationAsync(
                operationId,
                key,
                async ct =>
            {
                byte[]? data;
                try
                {
                    if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
                    {
                        _logger.Log(LogLevel.Debug, "FUSION (K={CacheKey} OP={CacheOperationId}): serializing the entry {Entry}", key, operationId, entry.ToLogString());
                    }

                    data = await Serializer !.SerializeAsync(entry).ConfigureAwait(false);
                }
                catch (Exception exc)
                {
                    if (_logger?.IsEnabled(_options.SerializationErrorsLogLevel) ?? false)
                    {
                        _logger.Log(_options.SerializationErrorsLogLevel, exc, "FUSION (K={CacheKey} OP={CacheOperationId}): an error occurred while serializing an entry {Entry}", key, operationId, entry.ToLogString());
                    }

                    data = null;
                }

                if (data is null)
                {
                    return;
                }

                ct.ThrowIfCancellationRequested();

                if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
                {
                    _logger.Log(LogLevel.Debug, "FUSION (K={CacheKey} OP={CacheOperationId}): setting the entry in distributed {Entry}", key, operationId, entry.ToLogString());
                }

                await Cache !.SetAsync(key, data, distributedOptions, token).ConfigureAwait(false);
            },
                "saving entry in distributed",
                options,
                distributedOptions,
                token
                ).ConfigureAwait(false);
        }
Пример #5
0
        public void ExecuteOperation(string operationId, string key, Action <CancellationToken> action, string actionDescription, FusionCacheEntryOptions options, DistributedCacheEntryOptions?distributedOptions, CancellationToken token)
        {
            if (IsCurrentlyUsable() == false)
            {
                return;
            }

            token.ThrowIfCancellationRequested();

            var actionDescriptionInner = actionDescription + (options.AllowBackgroundDistributedCacheOperations ? " (background)" : null);

            if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
            {
                _logger.LogDebug("FUSION (K={CacheKey} OP={CacheOperationId}): " + actionDescriptionInner + " {DistributedOptions}", key, operationId, distributedOptions.ToLogString());
            }

            FusionCacheExecutionUtils.RunSyncActionAdvanced(
                action,
                options.DistributedCacheHardTimeout,
                false,
                options.AllowBackgroundDistributedCacheOperations == false,
                exc => ProcessDistributedCacheError(operationId, key, exc, actionDescriptionInner),
                false,
                token
                );
        }
        public void SetEntry <TValue>(string operationId, string key, FusionCacheMemoryEntry entry, FusionCacheEntryOptions options)
        {
            var memoryOptions = options.ToMemoryCacheEntryOptions();

            options.MemoryOptionsModifier?.Invoke(memoryOptions, entry.GetValue <TValue>());

            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());
            }

            _cache.Set <FusionCacheMemoryEntry>(key, entry, memoryOptions);
        }
Пример #7
0
        public void SetEntry <TValue>(string operationId, string key, IFusionCacheEntry entry, FusionCacheEntryOptions options, CancellationToken token = default)
        {
            if (IsCurrentlyUsable() == false)
            {
                return;
            }

            token.ThrowIfCancellationRequested();

            var distributedOptions = options.ToDistributedCacheEntryOptions();

            options.DistributedOptionsModifier?.Invoke(distributedOptions, entry.GetValue <TValue>());

            ExecuteOperation(
                operationId,
                key,
                ct =>
            {
                var distributedEntry = entry.AsDistributedEntry <TValue>();

                byte[]? data;
                try
                {
                    if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
                    {
                        _logger.Log(LogLevel.Debug, "FUSION (K={CacheKey} OP={CacheOperationId}): serializing the entry {Entry}", key, operationId, distributedEntry.ToLogString());
                    }

                    data = _serializer.Serialize(distributedEntry);
                }
                catch (Exception exc)
                {
                    if (_logger?.IsEnabled(_options.SerializationErrorsLogLevel) ?? false)
                    {
                        _logger.Log(_options.SerializationErrorsLogLevel, exc, "FUSION (K={CacheKey} OP={CacheOperationId}): an error occurred while serializing an entry {Entry}", key, operationId, distributedEntry.ToLogString());
                    }

                    data = null;
                }

                if (data is null)
                {
                    return;
                }

                ct.ThrowIfCancellationRequested();

                if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
                {
                    _logger.Log(LogLevel.Debug, "FUSION (K={CacheKey} OP={CacheOperationId}): setting the entry in distributed {Entry}", key, operationId, distributedEntry.ToLogString());
                }

                _cache.Set(WireFormatVersionPrefix + key, data, distributedOptions);
            },
                "saving entry in distributed",
                options,
                distributedOptions,
                token
                );
        }