Example #1
0
        internal static void DisposeLinkingScope()
        {
            var currentLink = ContextLink;
            var priorLink   = ((EntryLink)currentLink).Parent;

            ContextLink = priorLink;
        }
Example #2
0
 internal static IEntryLink CreateLinkingScope()
 {
     var parentLink = ContextLink;
     var newLink = new EntryLink(parent: parentLink);
     ContextLink = newLink;
     return newLink;
 }
Example #3
0
        internal static IEntryLink CreateLinkingScope()
        {
            var parentLink = ContextLink;
            var newLink    = new EntryLink(parent: parentLink);

            ContextLink = newLink;
            return(newLink);
        }
        public void UpdateCacheEntryOptions_CopiesTriggersFromEntryLink()
        {
            // Arrange
            var expiresSliding = TimeSpan.FromSeconds(30);
            var expected = new[] { Mock.Of<IExpirationTrigger>(), Mock.Of<IExpirationTrigger>() };
            var cache = new MemoryCache(new MemoryCacheOptions());
            var cacheTagHelper = new CacheTagHelper(cache)
            {
                ExpiresSliding = expiresSliding
            };

            var entryLink = new EntryLink();
            entryLink.AddExpirationTriggers(expected);

            // Act
            var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions(entryLink);

            // Assert
            Assert.Equal(expected, cacheEntryOptions.Triggers.ToArray());
        }
        public void UpdateCacheEntryOptions_PrefersAbsoluteExpirationSpecifiedOnEntryLinkOverExpiresOn()
        {
            // Arrange
            var expiresOn1 = DateTimeOffset.UtcNow.AddDays(12);
            var expiresOn2 = DateTimeOffset.UtcNow.AddMinutes(4);
            var cache = new MemoryCache(new MemoryCacheOptions());
            var cacheTagHelper = new CacheTagHelper(cache)
            {
                ExpiresOn = expiresOn1
            };

            var entryLink = new EntryLink();
            entryLink.SetAbsoluteExpiration(expiresOn2);

            // Act
            var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions(entryLink);

            // Assert
            Assert.Equal(expiresOn2, cacheEntryOptions.AbsoluteExpiration);
        }
        public void UpdateCacheEntryOptions_UsesAbsoluteExpirationSpecifiedOnEntryLink()
        {
            // Arrange
            var expiresOn = DateTimeOffset.UtcNow.AddMinutes(7);
            var cache = new MemoryCache(new MemoryCacheOptions());
            var cacheTagHelper = new CacheTagHelper(cache)
            {
            };

            var entryLink = new EntryLink();
            entryLink.SetAbsoluteExpiration(expiresOn);

            // Act
            var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions(entryLink);

            // Assert
            Assert.Equal(expiresOn, cacheEntryOptions.AbsoluteExpiration);
        }
Example #7
0
 internal static void DisposeLinkingScope()
 {
     var currentLink = ContextLink;
     var priorLink = ((EntryLink)currentLink).Parent;
     ContextLink = priorLink;
 }
Example #8
0
 public EntryLink(EntryLink parent)
 {
     Parent = parent;
 }
        public void UpdateCacheContext_CopiesTriggersFromEntryLink()
        {
            // Arrange
            var expiresSliding = TimeSpan.FromSeconds(30);
            var expected = new[] { Mock.Of<IExpirationTrigger>(), Mock.Of<IExpirationTrigger>() };
            var triggers = new List<IExpirationTrigger>();
            var cache = new MemoryCache(new MemoryCacheOptions());
            var cacheContext = new Mock<ICacheSetContext>();
            cacheContext.Setup(c => c.SetSlidingExpiration(expiresSliding))
                        .Verifiable();
            cacheContext.Setup(c => c.AddExpirationTrigger(It.IsAny<IExpirationTrigger>()))
                        .Callback<IExpirationTrigger>(triggers.Add)
                        .Verifiable();
            var cacheTagHelper = new CacheTagHelper
            {
                MemoryCache = cache,
                ExpiresSliding = expiresSliding
            };

            var entryLink = new EntryLink();
            entryLink.AddExpirationTriggers(expected);

            // Act
            cacheTagHelper.UpdateCacheContext(cacheContext.Object, entryLink);

            // Assert
            cacheContext.Verify();
            Assert.Equal(expected, triggers);
        }
Example #10
0
 public EntryLink(EntryLink parent)
 {
     Parent = parent;
 }
        public void UpdateCacheContext_PrefersAbsoluteExpirationSpecifiedOnEntryLinkOverExpiresOn()
        {
            // Arrange
            var expiresOn1 = DateTimeOffset.UtcNow.AddDays(12);
            var expiresOn2 = DateTimeOffset.UtcNow.AddMinutes(4);
            var cache = new MemoryCache(new MemoryCacheOptions());
            var cacheContext = new Mock<ICacheSetContext>();
            var sequence = new MockSequence();
            cacheContext.InSequence(sequence)
                        .Setup(c => c.SetAbsoluteExpiration(expiresOn1))
                        .Verifiable();

            cacheContext.InSequence(sequence)
                        .Setup(c => c.SetAbsoluteExpiration(expiresOn2))
                        .Verifiable();

            var cacheTagHelper = new CacheTagHelper
            {
                MemoryCache = cache,
                ExpiresOn = expiresOn1
            };

            var entryLink = new EntryLink();
            entryLink.SetAbsoluteExpiration(expiresOn2);

            // Act
            cacheTagHelper.UpdateCacheContext(cacheContext.Object, entryLink);

            // Assert
            cacheContext.Verify();
        }
        public void UpdateCacheContext_UsesAbsoluteExpirationSpecifiedOnEntryLink()
        {
            // Arrange
            var expiresOn = DateTimeOffset.UtcNow.AddMinutes(7);
            var cache = new MemoryCache(new MemoryCacheOptions());
            var cacheContext = new Mock<ICacheSetContext>(MockBehavior.Strict);
            cacheContext.Setup(c => c.SetAbsoluteExpiration(expiresOn))
                        .Verifiable();
            var cacheTagHelper = new CacheTagHelper
            {
                MemoryCache = cache
            };

            var entryLink = new EntryLink();
            entryLink.SetAbsoluteExpiration(expiresOn);

            // Act
            cacheTagHelper.UpdateCacheContext(cacheContext.Object, entryLink);

            // Assert
            cacheContext.Verify();
        }
Example #13
0
        // Internal for unit testing
        internal void UpdateCacheContext(ICacheSetContext cacheSetContext, EntryLink entryLink)
        {
            if (ExpiresOn != null)
            {
                cacheSetContext.SetAbsoluteExpiration(ExpiresOn.Value);
            }

            if (ExpiresAfter != null)
            {
                cacheSetContext.SetAbsoluteExpiration(ExpiresAfter.Value);
            }

            if (ExpiresSliding != null)
            {
                cacheSetContext.SetSlidingExpiration(ExpiresSliding.Value);
            }

            if (Priority != null)
            {
                cacheSetContext.SetPriority(Priority.Value);
            }

            cacheSetContext.AddEntryLink(entryLink);
        }
Example #14
0
        /// <inheritdoc />
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var key = GenerateKey(context);
            TagHelperContent result;
            if (!MemoryCache.TryGetValue(key, out result))
            {
                // Create an EntryLink and flow it so that it is accessible via the ambient EntryLinkHelpers.ContentLink
                // for user code.
                var entryLink = new EntryLink();
                using (entryLink.FlowContext())
                {
                    result = await context.GetChildContentAsync();
                }

                MemoryCache.Set(key, cacheSetContext =>
                {
                    UpdateCacheContext(cacheSetContext, entryLink);
                    return result;
                });
            }

            // Clear the contents of the "cache" element since we don't want to render it.
            output.SuppressOutput();
            output.Content.SetContent(result);
        }
Example #15
0
        public void Main()
        {
            IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
            object result;
            string key = "Key";
            object newObject = new object();
            object state = new object();

            // Basic CRUD operations:

            // Create / Overwrite
            result = cache.Set(key, newObject);
            result = cache.Set(key, context => new object());
            result = cache.Set(key, state, context => new object());

            // Retrieve, null if not found
            result = cache.Get(key);

            // Retrieve
            bool found = cache.TryGetValue(key, out result);

            // Delete
            cache.Remove(key);

            // Conditional operations:

            // Retrieve / Create when we want to lazily create the object.
            result = cache.GetOrSet(key, context => new object());

            // Retrieve / Create when we want to lazily create the object.
            result = cache.GetOrSet(key, state, context => new object());

            // Cache entry configuration:

            // Stays in the cache as long as possible
            result = cache.GetOrSet(key, state, context =>
            {
                context.SetPriority(CachePreservationPriority.NeverRemove);
                return new object();
            });

            // Automatically remove if not accessed in the given time
            result = cache.GetOrSet(key, state, context =>
            {
                context.SetSlidingExpiration(TimeSpan.FromMinutes(5));
                return new object();
            });

            // Automatically remove at a certain time
            result = cache.GetOrSet(key, state, context =>
            {
                context.SetAbsoluteExpiration(new DateTime(2014, 12, 31));
                // or relative:
                // context.SetAbsoluteExpiration(TimeSpan.FromMinutes(5));
                return new object();
            });

            // Automatically remove if not accessed in the given time
            // Automatically remove at a certain time (if it lives that long)
            result = cache.GetOrSet(key, state, context =>
            {
                context.SetSlidingExpiration(TimeSpan.FromMinutes(5));

                context.SetAbsoluteExpiration(new DateTime(2014, 12, 31));
                // or relative:
                // context.SetAbsoluteExpiration(TimeSpan.FromMinutes(5));
                return new object();
            });

            // Callback when evicted
            result = cache.GetOrSet(key, state, context =>
            {
                context.RegisterPostEvictionCallback((echoKey, value, reason, substate) =>
                    Console.WriteLine(echoKey + ": '" + value + "' was evicted due to " + reason), state: null);
                return new object();
            });

            // Remove on trigger
            var cts = new CancellationTokenSource();
            result = cache.GetOrSet(key, state, context =>
            {
                context.AddExpirationTrigger(new CancellationTokenTrigger(cts.Token));
                return new object();
            });

            result = cache.GetOrSet(key, context =>
            {
                var link = new EntryLink();

                var inner1 = cache.GetOrSet("subkey1", link, subContext =>
                {
                    return "SubValue1";
                });

                string inner2;
                using (link.FlowContext())
                {
                    inner2 = cache.GetOrSet("subkey2", subContext =>
                    {
                        return "SubValue2";
                    });
                }

                context.AddEntryLink(link);

                return inner1 + inner2;
            });
        }