示例#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
                );
        }
 private T LoopDeLoop <T>(IFusionCacheSerializer serializer, T obj)
 {
     return(serializer.Deserialize <T>(serializer.Serialize(obj)));
 }