public void MemoryCacheProviderGetOrCreateCacheTest()
        {
            MemoryCacheProvider  target = MemoryCacheProvider.GetInstance();
            IQueryable <Product> query  = null;

            using (ProductContext context = new ProductContext())
            {
                query = context.Products.OrderBy(one => one.ProductNumber).Where(one => one.IsActive);

                target.RemoveFromCache <Product>(query);

                var actual = target.GetOrCreateCache <Product>(query);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");

                SQLCommandHelper.ExecuteNonQuery("Update Products Set IsActive = 0");

                actual = target.GetOrCreateCache <Product>(query);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");

                target.RemoveFromCache <Product>(query);

                actual = target.GetOrCreateCache <Product>(query);
                Assert.AreEqual(0, actual.Count(), "Should have 0 rows");

                target.RemoveFromCache <Product>(query);
            }
        }
        public void MemoryCacheProviderGetOrCreateCacheUsageTest()
        {
            EFCacheExtensions.SetCacheProvider(MemoryCacheProvider.GetInstance());
            using (ProductContext context = new ProductContext())
            {
                var query = context.Products
                            .OrderBy(one => one.ProductNumber)
                            .Where(one => one.IsActive).AsCacheable();

                Assert.AreEqual(2, query.Count(), "Should have 2 rows");

                SQLCommandHelper.ExecuteNonQuery("Update Products Set IsActive = 0");

                query = context.Products
                        .OrderBy(one => one.ProductNumber)
                        .Where(one => one.IsActive).AsCacheable();
                Assert.AreEqual(2, query.Count(), "Should have 2 rows");


                IQueryable <Product> cleanupQuery = context.Products
                                                    .OrderBy(one => one.ProductNumber)
                                                    .Where(one => one.IsActive);

                EFCacheExtensions.RemoveFromCache <Product>(cleanupQuery);

                query = context.Products
                        .OrderBy(one => one.ProductNumber)
                        .Where(one => one.IsActive).AsCacheable();
                Assert.AreEqual(0, query.Count(), "Should have 0 rows");

                EFCacheExtensions.RemoveFromCache <Product>(cleanupQuery);
            }
        }
        public void MemoryCacheProviderGetOrCreateCacheWithExpirationTest()
        {
            EFCacheExtensions.SetCacheProvider(MemoryCacheProvider.GetInstance());
            MemoryCacheProvider  target = MemoryCacheProvider.GetInstance();
            IQueryable <Product> query  = null;

            using (ProductContext context = new ProductContext())
            {
                query = context.Products.OrderBy(one => one.ProductNumber).Where(one => one.IsActive);
                target.RemoveFromCache <Product>(query);

                TimeSpan cacheDuration = TimeSpan.FromSeconds(3);

                var actual = target.GetOrCreateCache <Product>(query, cacheDuration);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");
                SQLCommandHelper.ExecuteNonQuery("Update Products Set IsActive = 0");
                actual = target.GetOrCreateCache <Product>(query, cacheDuration);
                Assert.AreEqual(2, actual.Count(), "Should have 2 rows");
                Thread.Sleep(4000);

                actual = target.GetOrCreateCache <Product>(query, cacheDuration);
                Assert.AreEqual(0, actual.Count(), "Should have 0 rows");

                target.RemoveFromCache <Product>(query);
            }
        }