Esempio n. 1
0
 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);
 }
        public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
        {
            var cachedResponse = entry as CachedResponse;

            if (cachedResponse != null)
            {
                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)
                });
            }
        }