public void Set_And_Get_Should_Succeed()
        {
            var cacheKey = $"{_namespace}_{Guid.NewGuid().ToString()}";

            hybridCaching_1.Set(cacheKey, "val", TimeSpan.FromSeconds(30));

            var res = hybridCaching_1.Get <string>(cacheKey);

            Assert.Equal("val", res.Value);
        }
Beispiel #2
0
        public void Distributed_Set_Throw_Exception_Should_Not_Break()
        {
            A.CallTo(() => fakeDisProvider.Set(A <string> .Ignored, A <string> .Ignored, A <TimeSpan> .Ignored)).Throws(new Exception());

            var key = $"d-set-{Guid.NewGuid().ToString()}";

            fakeHybrid.Set(key, "123", TimeSpan.FromSeconds(30));

            var res = fakeHybrid.Get <string>(key);

            Assert.True(res.HasValue);
            Assert.Equal(default(string), res.Value);
        }
        public void GetSyncWithDataRetriever_DistributedCacheHasValue_SetLocalWithDefaultOrDistributedExpiration(int distributedExpirationMinutes, int setLocalExpirationMinutes)
        {
            var cacheKey       = GetUniqueCacheKey();
            var fakeLocalCache = A.Fake <IInMemoryCaching>();

            A.CallTo(() => fakeLocalCache.ProviderName)
            .Returns("testCache");
            A.CallTo(() => fakeLocalCache.Get <string>(cacheKey))
            .Returns(CacheValue <string> .NoValue);
            A.CallTo(() => fakeLocalCache.Add($"{cacheKey}_Lock", A <int> ._, A <TimeSpan> ._))
            .Returns(true);
            var localProvider =
                new DefaultInMemoryCachingProvider("testCache", new[] { fakeLocalCache },
                                                   new InMemoryOptions()
            {
                MaxRdSecond = 0
            });
            var fakeDistributedProvider = A.Fake <IEasyCachingProvider>();
            var fakeProvidersFactory    = A.Fake <IEasyCachingProviderFactory>();

            A.CallTo(() => fakeProvidersFactory.GetCachingProvider(LocalCacheProviderName)).Returns(localProvider);
            A.CallTo(() => fakeProvidersFactory.GetCachingProvider(DistributedCacheProviderName)).Returns(fakeDistributedProvider);
            var hybridProvider = new HybridCachingProvider("TestProvider",
                                                           new HybridCachingOptions()
            {
                TopicName = "TestTopicName", LocalCacheProviderName = LocalCacheProviderName,
                DistributedCacheProviderName = DistributedCacheProviderName
            }, fakeProvidersFactory);

            var newExpiration        = TimeSpan.FromMinutes(5);
            var distributeExpiration = TimeSpan.FromMinutes(distributedExpirationMinutes);
            var dataRetriever        = A.Fake <Func <string> >();

            A.CallTo(() => dataRetriever.Invoke()).Returns("cachedValue");

            A.CallTo(() => fakeDistributedProvider.GetExpiration(cacheKey)).Returns(distributeExpiration);
            A.CallTo(() => fakeDistributedProvider.Get(cacheKey, A <Func <string> > ._, A <TimeSpan> ._))
            .Returns(new CacheValue <string>("cachedValue", true));


            var res = hybridProvider.Get(cacheKey, dataRetriever, newExpiration);


            Assert.NotNull(res);
            Assert.True(res.HasValue);
            Assert.Equal("cachedValue", res.Value);

            A.CallTo(() => fakeDistributedProvider.GetExpiration(cacheKey)).MustHaveHappenedOnceExactly();
            A.CallTo(() => fakeLocalCache.Set(cacheKey, A <string> ._, TimeSpan.FromMinutes(setLocalExpirationMinutes)))
            .MustHaveHappenedOnceExactly();
        }
        public void GetWithDataRetriever_LocalCacheHasValue_DistributedCacheAndDataRetrieverShouldNotBeCalled()
        {
            var(hybridProvider, localProvider, fakeDistributedProvider, _) = CreateCachingProviderWithFakes();
            var dataRetriever = CreateFakeDataRetriever(result: "value");

            localProvider.Set("key", "cachedValue", Expiration);


            var res = hybridProvider.Get("key", dataRetriever, Expiration);


            Assert.True(res.HasValue);
            Assert.Equal("cachedValue", res.Value);

            var cachedValue = localProvider.Get <string>("key");

            Assert.True(cachedValue.HasValue);
            Assert.Equal("cachedValue", cachedValue.Value);

            A.CallTo(() => dataRetriever.Invoke()).MustNotHaveHappened();
            fakeDistributedProvider.CallToGetWithDataRetriever <string>().MustNotHaveHappened();
            A.CallTo(() => fakeDistributedProvider.GetExpiration("key")).MustNotHaveHappened();
        }