Exemple #1
0
        public void CanWriteCacheVaryByRules()
        {
            var cache         = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
            var responseCache = new DistributedResponseCache(cache);

            using var stream = new MemoryStream();
            using (var writer = new StreamWriter(stream, leaveOpen: true))
            {
                writer.Write("data");
            }
            stream.Position = 0;

            Assert.That(() => responseCache.Set("test", new CachedVaryByRules
            {
                Headers         = new StringValues("x-test-header"),
                QueryKeys       = new StringValues("x-test-query"),
                VaryByKeyPrefix = "test"
            }, TimeSpan.FromMinutes(1)), Throws.Nothing);

            var item = responseCache.Get("test");

            Assert.That(item, Is.Not.Null);
            var realItem = item as CachedVaryByRules;

            Assert.That(item, Is.Not.Null);
        }
 public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
 {
     if (!cacheController.ShouldWriteToCache)
     {
         return;
     }
     cache.Set(key, entry, validFor);
 }
Exemple #3
0
        public void CanWriteCachedResponse()
        {
            var mockDistributedCache = new Mock <IDistributedCache>();
            var responseCache        = new DistributedResponseCache(mockDistributedCache.Object);

            using var stream = new MemoryStream();
            using (var writer = new StreamWriter(stream, leaveOpen: true))
            {
                writer.Write("data");
            }
            stream.Position = 0;

            Assert.That(() => responseCache.Set("test", new CachedResponse
            {
                Body    = stream,
                Headers = new HeaderDictionary(new Dictionary <string, StringValues>
                {
                    ["x-test-header"] = "testing...testing"
                })
            }, TimeSpan.FromMinutes(1)), Throws.Nothing);
        }