Exemplo n.º 1
0
        public void TestWithCacheDirectives_GetOrSet()
        {
            var cache = GetScopedCacheWithSlidingExpiration("cache1", TimeSpan.FromMinutes(2));

            cache.ClearAll();

            var valueTimestamp = DateTimeOffset.UtcNow;

            int getterInvocations = 0;

            ScopedValue <int> Getter() => new ScopedValue <int>(++getterInvocations, valueTimestamp);

            using (CacheDirectives.SetScope(CacheMethod.GetOrSet, valueTimestamp))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(1, getterInvocations);
                Assert.AreEqual(CacheMethodTaken.Set | CacheMethodTaken.GetMiss, result.MethodTaken);
                Assert.AreEqual(1, result.ScopedValue.Value);
                Assert.AreEqual(valueTimestamp, result.ScopedValue.ValueTimestamp);

                result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.Get, result.MethodTaken);
                Assert.AreEqual(1, result.ScopedValue.Value);
                Assert.AreEqual(valueTimestamp, result.ScopedValue.ValueTimestamp);
            }
        }
Exemplo n.º 2
0
        public void TestWithCacheDirectives_MinimumValueTimestamp_NotUpToDateWithIgnore()
        {
            var cache = GetScopedCacheWithSlidingExpiration("cache1", TimeSpan.FromMinutes(2));

            cache.ClearAll();

            var testTimestamp = DateTimeOffset.UtcNow;

            int getterInvocations = 0;

            ScopedValue <int> Getter() => new ScopedValue <int>(++getterInvocations, testTimestamp.AddHours(-1));

            using (CacheDirectives.SetScope(CacheMethod.GetOrSet | CacheMethod.IgnoreMinimumValueTimestamp, testTimestamp))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.Set | CacheMethodTaken.GetMiss, result.MethodTaken);

                using (CacheDirectives.SetScope(CacheMethod.Get | CacheMethod.IgnoreMinimumValueTimestamp,
                                                testTimestamp))
                {
                }

                var cacheMethodTaken = cache.TryGetScoped <int>("key", out _);
                Assert.AreEqual(CacheMethodTaken.Get, cacheMethodTaken);
            }
        }
Exemplo n.º 3
0
        public void TestWithCacheDirectives_MultipleScopes()
        {
            var cache = GetScopedCacheWithSlidingExpiration("cache1", TimeSpan.FromMinutes(2));

            cache.ClearAll();

            var testTimestamp = DateTimeOffset.UtcNow;

            int getterInvocations = 0;

            ScopedValue <int> Getter() => new ScopedValue <int>(++getterInvocations, DateTimeOffset.UtcNow);

            using (CacheDirectives.SetScope(CacheMethod.None, testTimestamp))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.None, result.MethodTaken);
                Assert.AreEqual(1, getterInvocations);
            }

            using (CacheDirectives.SetScope(CacheMethod.Get, testTimestamp))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.GetMiss, result.MethodTaken);
                Assert.AreEqual(2, getterInvocations);
            }

            using (CacheDirectives.SetScope(CacheMethod.Set, testTimestamp))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.Set, result.MethodTaken);
                Assert.AreEqual(3, getterInvocations);
            }

            using (CacheDirectives.SetScope(CacheMethod.None, testTimestamp))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.None, result.MethodTaken);
                Assert.AreEqual(4, getterInvocations);
            }

            var resultOutsideOfScope = cache.GetScoped("key", Getter);

            Assert.AreEqual(CacheMethodTaken.None, resultOutsideOfScope.MethodTaken);
            Assert.AreEqual(5, getterInvocations);

            Thread.Sleep(1);
            using (CacheDirectives.SetScope(CacheMethod.Get, DateTimeOffset.UtcNow))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.GetMiss, result.MethodTaken);
                Assert.AreEqual(6, getterInvocations);
            }

            using (CacheDirectives.SetScope(CacheMethod.Get, testTimestamp))
            {
                var result = cache.GetScoped("key", Getter);
                Assert.AreEqual(CacheMethodTaken.Get, result.MethodTaken);
                Assert.AreEqual(6, getterInvocations);
            }
        }
Exemplo n.º 4
0
        public void TestInitialize()
        {
            CacheManager.Settings = null;
            CacheManager.CacheManagerInternals = null;

            cacheDirectives = CacheDirectives.SetScope(CacheMethod.GetOrSet, DateTimeOffset.UtcNow);
        }
            public async Task OnActionExecutionAsync(
                ActionExecutingContext context,
                ActionExecutionDelegate next)
            {
                var requestedCacheDirectives = GetCacheDirectivesFromRequest(context);

                using (CacheDirectives.SetScope(requestedCacheDirectives))
                {
                    await next().ConfigureAwait(false);
                }
            }
        public async Task <HttpResponseMessage> ExecuteActionFilterAsync(
            HttpActionContext actionContext,
            CancellationToken cancellationToken,
            Func <Task <HttpResponseMessage> > continuation)
        {
            var requestedCacheDirectives = GetCacheDirectivesFromRequest(actionContext);

            using (CacheDirectives.SetScope(requestedCacheDirectives))
            {
                var response = await continuation().ConfigureAwait(false);

                return(response);
            }
        }
Exemplo n.º 7
0
 public void TestInitialize()
 {
     cacheDirectives = CacheDirectives.SetScope(CacheMethod.GetOrSet, DateTimeOffset.UtcNow);
     CacheManager.InitializeFromConfig();
     foreach (var cacheName in CacheManager.GetCacheNames())
     {
         try
         {
             CacheManager.GetCache(cacheName).ClearAll();
         }
         catch (Exception ex)
         {
             Console.WriteLine($"Failed to clear cache [{cacheName}] due to: {ex.Message}");
         }
     }
 }
Exemplo n.º 8
0
        public void TestWithCacheDirectives_Get()
        {
            var cache = GetScopedCacheWithSlidingExpiration("cache1", TimeSpan.FromMinutes(2));

            cache.ClearAll();

            int getterInvocations = 0;

            ScopedValue <int> Getter() => new ScopedValue <int>(++getterInvocations, DateTimeOffset.UtcNow);

            using (CacheDirectives.SetScope(CacheMethod.Get, DateTimeOffset.UtcNow))
            {
                Assert.AreEqual(CacheMethodTaken.GetMiss, cache.GetScoped("key", Getter).MethodTaken);
                Assert.AreEqual(CacheMethodTaken.GetMiss, cache.TryGetScoped <int>("key", out _));
                Assert.AreEqual(CacheMethodTaken.GetMiss, cache.GetScoped("key", Getter).MethodTaken);
                Assert.AreEqual(2, getterInvocations);
            }
        }
Exemplo n.º 9
0
        public void TestWithCacheDirectives_SetScope()
        {
            var cache = GetScopedCacheWithSlidingExpiration("cache1", TimeSpan.FromMinutes(2));

            cache.ClearAll();

            int getterInvocations = 0;

            ScopedValue <int> Getter() => new ScopedValue <int>(++getterInvocations, DateTimeOffset.UtcNow);

            ScopedValue <int> expectedScopedValue;

            using (CacheDirectives.SetScope(CacheMethod.Get, DateTimeOffset.UtcNow))
            {
                using (CacheDirectives.SetScope(CacheMethod.Set, DateTimeOffset.UtcNow))
                {
                    Assert.AreEqual(CacheMethodTaken.None, cache.TryGetScoped <int>("key", out _));
                    Assert.AreEqual(CacheMethodTaken.Set, cache.GetScoped("key", Getter).MethodTaken);
                    Assert.AreEqual(CacheMethodTaken.None, cache.TryGetScoped <int>("key", out _));
                    Assert.AreEqual(1, getterInvocations);

                    expectedScopedValue = Getter();
                    expectedScopedValue.ValueTimestamp = expectedScopedValue.ValueTimestamp.AddHours(2);
                    Assert.AreEqual(CacheMethodTaken.Set,
                                    cache.SetScoped("key2", expectedScopedValue.Value, expectedScopedValue.ValueTimestamp));
                }

                Assert.AreEqual(CacheMethodTaken.None, cache.SetScoped("key3", 3, DateTimeOffset.UtcNow));
                Assert.AreEqual(CacheMethodTaken.GetMiss, cache.TryGetScoped <int>("key3", out _));

                Assert.AreEqual(CacheMethodTaken.Get, cache.TryGetScoped <int>("key", out var value));
                Assert.AreEqual(1, value?.Value);

                using (CacheDirectives.SetScope(CacheMethod.Get, DateTimeOffset.UtcNow.AddHours(1)))
                {
                    Assert.AreEqual(CacheMethodTaken.GetMiss, cache.TryGetScoped <int>("key", out _));
                    Assert.AreEqual(CacheMethodTaken.Get, cache.TryGetScoped <int>("key2", out _));
                }

                Assert.AreEqual(CacheMethodTaken.Get, cache.TryGetScoped <int>("key2", out var value2));
                Assert.AreEqual(expectedScopedValue.Value, value2?.Value);
                Assert.AreEqual(expectedScopedValue.ValueTimestamp, value2?.ValueTimestamp);
            }
        }
Exemplo n.º 10
0
        public void TestNoCacheStruct()
        {
            var cache = new NoCache();

            int hits = 0;

            Func <ScopedValue <int> > getter = () => { hits++; return(new ScopedValue <int>(hits, DateTimeOffset.UtcNow)); };

            GetScopedResult <int> result;

            using (CacheDirectives.SetScope(CacheMethod.GetOrSet, DateTimeOffset.UtcNow))
            {
                result = cache.GetScoped("key", getter);
                Assert.AreEqual(1, hits);
                Assert.AreEqual(1, result.ScopedValue.Value);
                Assert.AreEqual(CacheMethodTaken.None, result.MethodTaken);

                result = cache.GetScoped("key", getter);
                Assert.AreEqual(2, hits);
                Assert.AreEqual(2, result.ScopedValue.Value);
                Assert.AreEqual(CacheMethodTaken.None, result.MethodTaken);
            }
        }
Exemplo n.º 11
0
 public void TestInitialize()
 {
     cacheDirectives = CacheDirectives.SetScope(CacheMethod.GetOrSet, DateTimeOffset.UtcNow);
 }
Exemplo n.º 12
0
        static void Run(string[] args)
        {
            Console.WriteLine($"{nameof(DemoProgram)} has started...");

            //general-invalidation
            if (args.Any(a => a.ToLowerInvariant() == "general-invalidation"))
            {
                var pattern = string.Join(" ", args.Skip(1));
                Console.WriteLine($"general-invalidation: {pattern}");

                var connectionString = CacheManager.GetConnectionString("localRedis").ConnectionString;
                using (var connection = ConnectionMultiplexer.Connect(connectionString))
                    connection.GetSubscriber().Publish("+general-invalidation", args.Last());
            }
            //invalidate-on-upsert layered
            else if (args.Any(a => a.ToLowerInvariant() == "layered-invalidate-on-upsert"))
            {
                using (CacheDirectives.SetScope(CacheMethod.GetOrSet, DateTimeOffset.UtcNow.AddMinutes(-1)))
                {
                    var layeredCacheNameToTest = string.Join(" ", args.Skip(1));
                    Console.WriteLine($"testing: layered-invalidate-on-upsert");
                    Console.WriteLine($"layeredCacheNameToTest={layeredCacheNameToTest}");

                    var layeredCache = CacheManager.GetCache(LayeredCache);
                    var layeredCacheWithAutomaticInvalidation = CacheManager.GetCache(layeredCacheNameToTest);

                    if (layeredCache.TryGet("keyA2", out string a2) && a2 == "valueA2" &&
                        layeredCacheWithAutomaticInvalidation.TryGet("keyB2", out string b2) && b2 == "valueB2")
                    {
                        layeredCache.Set("keyA2", "demoProgram.valueA2");
                        layeredCacheWithAutomaticInvalidation.Set("keyB2", "demoProgram.valueB2");
                        Console.WriteLine("Completed!");
                    }
                    else
                    {
                        Console.WriteLine("invalid!");
                    }
                }

                return;
            }
            // general tests
            else
            {
                var cache = CacheManager.GetCache(LocalCacheWithNotifier);

                // Put values in cache
                Console.WriteLine("Caching values for key1 & key2.");
                cache.Get("key1", () => { return("value1"); });
                cache.Get("key2", () => { return("value2"); });

                // Clear keys if passed as parameters

                if (args.Any(a => a.ToLowerInvariant() == "key1"))
                {
                    Console.WriteLine("Clearing key1 from cache.");
                    ClearCache(cache, "key1");
                }

                if (args.Any(a => a.ToLowerInvariant() == "key2"))
                {
                    Console.WriteLine("Clearing key2 from cache.");
                    ClearCache(cache, "key2");
                }

                // Otherwise clear all

                if (!args.Any())
                {
                    Console.WriteLine("Clearing entire cache.");
                    ClearCache(cache, null);
                }
            }

            Console.WriteLine($"{nameof(DemoProgram)} has ended.");

            Thread.Sleep(1000);

            //Console.ReadKey();
        }