示例#1
0
        public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
        {
            if (entry is CachedResponse cachedResponse)
            {
                var segmentStream = new SegmentWriteStream(StreamUtilities.BodySegmentSize);
                cachedResponse.Body.CopyTo(segmentStream);

                _cache.Set(
                    key,
                    new MemoryCachedResponse
                {
                    Created      = cachedResponse.Created,
                    StatusCode   = cachedResponse.StatusCode,
                    Headers      = cachedResponse.Headers,
                    BodySegments = segmentStream.GetSegments(),
                    BodyLength   = segmentStream.Length
                },
                    new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = validFor,
                    Size = CacheEntryHelpers.EstimateCachedResponseSize(cachedResponse)
                });
            }
            else
            {
                _cache.Set(
                    key,
                    entry,
                    new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = validFor,
                    Size = CacheEntryHelpers.EstimateCachedVaryByRulesySize(entry as CachedVaryByRules)
                });
            }
        }
 internal ResponseCachingStream(Stream innerStream, long maxBufferSize, int segmentSize, Action startResponseCallback, Func <Task> startResponseCallbackAsync)
 {
     _innerStream                = innerStream;
     _maxBufferSize              = maxBufferSize;
     _segmentSize                = segmentSize;
     _startResponseCallback      = startResponseCallback;
     _startResponseCallbackAsync = startResponseCallbackAsync;
     _segmentWriteStream         = new SegmentWriteStream(_segmentSize);
 }
示例#3
0
        public void Set(string baseKey, string key, IResponseCacheEntry entry, TimeSpan validFor)
        {
            try
            {
                _semaphore.Wait();
                if (entry is CachedResponse cachedResponse)
                {
                    var segmentStream = new SegmentWriteStream(StreamUtilities.BodySegmentSize);
                    cachedResponse.Body.CopyTo(segmentStream);

                    _cache.Set(
                        key,
                        new MemoryCachedResponse
                    {
                        Created      = cachedResponse.Created,
                        StatusCode   = cachedResponse.StatusCode,
                        Headers      = cachedResponse.Headers,
                        BodySegments = segmentStream.GetSegments(),
                        BodyLength   = segmentStream.Length
                    },
                        new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = validFor,
                        Size = CacheEntryHelpers.EstimateCachedResponseSize(cachedResponse)
                    });
                }
                else
                {
                    _cache.Set(
                        key,
                        entry,
                        new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = validFor,
                        Size = CacheEntryHelpers.EstimateCachedVaryByRulesySize(entry as CachedVaryByRules)
                    });
                }

                var cacheKey = baseKey ?? key;
                if (cacheKey.StartsWith("PRJUID=") && !_cacheKeys.ContainsKey(cacheKey))
                {
                    _cacheKeys.TryAdd(cacheKey, new ConcurrentBag <string>());
                }

                if (!string.IsNullOrEmpty(baseKey) && !string.IsNullOrEmpty(key))
                {
                    _cacheKeys.TryGetValue(baseKey, out var cacheBag);
                    cacheBag.Add(key);
                }
            }
            finally
            {
                _semaphore.Release();
            }
        }