Exemple #1
0
        public void Queryable_WithExpiration()
        {
            QueryCacheManager.Cache = new MemoryCache(Guid.NewGuid().ToString());

            TestContext.DeleteAll(x => x.Entity_Basics);
            TestContext.Insert(x => x.Entity_Basics, 1);

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Entity_Basics.FromCache(DateTime.Now.AddMinutes(2)).Count();
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Entity_Basics);
                QueryCacheManager.ExpireAll();

                // EXPIRED
                var cacheCountExpired = QueryCacheHelper.GetCacheCount();

                // AFTER
                var itemCountAfter  = ctx.Entity_Basics.FromCache(DateTime.Now.AddMinutes(2)).Count();
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are not equal
                Assert.AreNotEqual(itemCountBefore, itemCountAfter);

                // TEST: The cache count are equal
                Assert.AreEqual(cacheCountBefore, cacheCountAfter);

                // TEST: The cache count after ExpireAll call is zero
                Assert.AreEqual(0, cacheCountExpired);
            }
        }
        public void Tag_NotEqual()
        {
            var testCacheKey = Guid.NewGuid().ToString();

            TestContext.DeleteAll(x => x.Entity_Basics);
            TestContext.Insert(x => x.Entity_Basics, 1);

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Entity_Basics.FromCache(testCacheKey).Count();
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Entity_Basics);

                // AFTER
                var itemCountAfter  = ctx.Entity_Basics.FromCache(testCacheKey, Guid.NewGuid().ToString()).Count();
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are NOT equal (A new cache key is used, the query is materialized)
                Assert.AreNotEqual(itemCountBefore, itemCountAfter);
                Assert.AreEqual(0, itemCountAfter);

                // TEST: The cache count are NOT equal (A new cache key is used)
                Assert.AreEqual(cacheCountBefore + 1, cacheCountAfter);
            }
        }
Exemple #3
0
        public void Tag_Expire()
        {
            var testCacheKey = Guid.NewGuid().ToString();

            TestContext.DeleteAll(x => x.Entity_Basics);
            TestContext.Insert(x => x.Entity_Basics, 1);

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Entity_Basics.FromCache(testCacheKey).Count();
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Entity_Basics);

                QueryCacheManager.ExpireTag(testCacheKey);
                var cacheCountExpired = QueryCacheHelper.GetCacheCount();

                // TEST: The cache count are NOT equal (The cache key has been removed)
                Assert.AreEqual(cacheCountBefore - 1, cacheCountExpired);

                // AFTER
                var itemCountAfter  = ctx.Entity_Basics.FromCache(testCacheKey).Count();
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are NOT equal (The query has been expired)
                Assert.AreNotEqual(itemCountBefore, itemCountAfter);
                Assert.AreEqual(0, itemCountAfter);

                // TEST: The cache count are NOT equal (The expired cache key is added)
                Assert.AreEqual(cacheCountExpired + 1, cacheCountAfter);
                Assert.AreEqual(cacheCountBefore, cacheCountAfter);
            }
        }
        public void Tag_Equal()
        {
            var testCacheKey = Guid.NewGuid().ToString();

            TestContext.DeleteAll(x => x.Entity_Basics);
            TestContext.Insert(x => x.Entity_Basics, 1);

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Entity_Basics.FromCache(testCacheKey).Count();
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Entity_Basics);

                // AFTER
                var itemCountAfter  = ctx.Entity_Basics.FromCache(testCacheKey).Count();
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are equal
                Assert.AreEqual(itemCountBefore, itemCountAfter);

                // TEST: The cache count are equal
                Assert.AreEqual(cacheCountBefore, cacheCountAfter);
            }
        }
Exemple #5
0
        public void Queryable_WithDefault()
        {
            QueryCacheManager.ExpireAll();

            TestContext.DeleteAll(x => x.Entity_Basics);
            TestContext.Insert(x => x.Entity_Basics, 1);

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Entity_Basics.FromCache().Count();
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Entity_Basics);
                QueryCacheManager.ExpireAll();

                // EXPIRED
                var cacheCountExpired = QueryCacheHelper.GetCacheCount();

                // AFTER
                var itemCountAfter  = ctx.Entity_Basics.FromCache().Count();
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are not equal
                Assert.AreNotEqual(itemCountBefore, itemCountAfter);

                // TEST: The cache count are equal
                Assert.AreEqual(cacheCountBefore, cacheCountAfter);

                // TEST: The cache count after ExpireAll call is zero
                Assert.AreEqual(0, cacheCountExpired);
            }
        }
        public void FromCache_QueryDeferred_WithDefault()
        {
            TestContext.DeleteAll(x => x.Entity_Basics);
            TestContext.Insert(x => x.Entity_Basics, 1);

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Entity_Basics.DeferredCount().FromCache();
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Entity_Basics);

                // AFTER
                var itemCountAfter  = ctx.Entity_Basics.DeferredCount().FromCache();
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are equal
                Assert.AreEqual(itemCountBefore, itemCountAfter);

                // TEST: The cache count are equal
                Assert.AreEqual(cacheCountBefore, cacheCountAfter);
            }
        }
Exemple #7
0
        public void FromCacheAsync_QueryDelayed_WithExpirations()
        {
            TestContext.DeleteAll(x => x.Entity_Basics);
            TestContext.Insert(x => x.Entity_Basics, 1);

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Entity_Basics.DelayedCount().FromCacheAsync(DateTime.Now.AddMinutes(2)).Result;
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Entity_Basics);

                // AFTER
                var itemCountAfter  = ctx.Entity_Basics.DelayedCount().FromCacheAsync(DateTime.Now.AddMinutes(2)).Result;
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are equal
                Assert.AreEqual(itemCountBefore, itemCountAfter);

                // TEST: The cache count are equal
                Assert.AreEqual(cacheCountBefore, cacheCountAfter);
            }
        }
Exemple #8
0
        public void IncludeOptimized()
        {
            TestContext.DeleteAll(x => x.Association_OneToMany_Rights);
            TestContext.DeleteAll(x => x.Association_OneToMany_Lefts);

            using (var ctx = new TestContext())
            {
                var left  = ctx.Association_OneToMany_Lefts.Add(new Association_OneToMany_Left());
                var right = ctx.Association_OneToMany_Rights.Add(new Association_OneToMany_Right());

                left.Rights = new List <Association_OneToMany_Right>();
                left.Rights.Add(right);

                ctx.SaveChanges();
            }

            using (var ctx = new TestContext())
            {
                // BEFORE
                var itemCountBefore  = ctx.Association_OneToMany_Lefts.IncludeOptimized(x => x.Rights).FromCache().Count();
                var cacheCountBefore = QueryCacheHelper.GetCacheCount();

                TestContext.DeleteAll(x => x.Association_OneToMany_Rights);
                TestContext.DeleteAll(x => x.Association_OneToMany_Lefts);

                // AFTER
                var itemCountAfter  = ctx.Association_OneToMany_Lefts.IncludeOptimized(x => x.Rights).FromCache().Count();
                var cacheCountAfter = QueryCacheHelper.GetCacheCount();

                // TEST: The item count are equal
                Assert.AreEqual(itemCountBefore, itemCountAfter);

                // TEST: The cache count are equal
                Assert.AreEqual(cacheCountBefore, cacheCountAfter);
            }
        }