Beispiel #1
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
                );
        }
        public static FusionCacheMemoryEntry AsMemoryEntry(this IFusionCacheEntry entry)
        {
            if (entry is FusionCacheMemoryEntry)
            {
                return((FusionCacheMemoryEntry)entry);
            }

            return(new FusionCacheMemoryEntry(entry.GetValue <object>(), entry.Metadata));
        }
        public static FusionCacheDistributedEntry <TValue> AsDistributedEntry <TValue>(this IFusionCacheEntry entry)
        {
            if (entry is FusionCacheDistributedEntry <TValue> )
            {
                return((FusionCacheDistributedEntry <TValue>)entry);
            }

            return(new FusionCacheDistributedEntry <TValue>(entry.GetValue <TValue>(), entry.Metadata));
        }