public void GroupByQueryIsCacheable2()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            var c = db
                    .Customers
                    .WithOptions(o => o.SetCacheable(true))
                    .GroupBy(x => x.Address.Country)
                    .Select(x => x.Key)
                    .ToList();

            c = db
                .Customers
                .GroupBy(x => x.Address.Country)
                .Select(x => x.Key)
                .ToList();

            c = db
                .Customers
                .WithOptions(o => o.SetCacheable(true))
                .GroupBy(x => x.Address.Country)
                .Select(x => x.Key)
                .ToList();

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
        private void OneToOneFetchTest <TPerson, TDetails>() where TPerson : Person, new() where TDetails : Details, new()
        {
            List <object> ids = this.CreatePersonAndDetails <TPerson, TDetails>();

            IStatistics statistics = Sfi.Statistics;

            // Clear the second level cache and the statistics
            Sfi.EvictEntity(typeof(TPerson).FullName);
            Sfi.EvictEntity(typeof(TDetails).FullName);
            Sfi.EvictQueries();

            statistics.Clear();

            // Fill the empty caches with data.
            this.FetchPeopleById <TPerson>(ids);

            // Verify that no data was retrieved from the cache.
            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");

            statistics.Clear();

            this.FetchPeopleById <TPerson>(ids);

            Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
        }
        public void QueryIsCacheableWithRegion()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();
            Sfi.EvictQueries("test");
            Sfi.EvictQueries("other");

            var x = (from c in db.Customers
                     select c)
                    .WithOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
                    .ToList();

            var x2 = (from c in db.Customers
                      select c)
                     .WithOptions(o => o.SetCacheable(true).SetCacheRegion("test"))
                     .ToList();

            var x3 = (from c in db.Customers
                      select c)
                     .WithOptions(o => o.SetCacheable(true).SetCacheRegion("other"))
                     .ToList();

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
Exemple #4
0
        public void FutureFetchIsCacheable()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();
            var multiQueries = Sfi.ConnectionProvider.Driver.SupportsMultipleQueries;

            db.Orders
            .Fetch(SelectMode.Fetch, x => x.Customer)
            .Where(x => x.OrderId == 10248)
            .Cacheable()
            .Future();

            var order = db.Orders
                        .Fetch(
                SelectMode.Fetch,
                x => x.OrderLines,
                x => x.OrderLines.First().Product,
                x => x.OrderLines.First().Product.OrderLines)
                        .Where(x => x.OrderId == 10248)
                        .Cacheable()
                        .Future()
                        .ToList()
                        .First();

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(multiQueries ? 1 : 2), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(2), "Unexpected cache miss count");

            Sfi.Statistics.Clear();
            Session.Clear();

            db.Orders
            .Fetch(SelectMode.Fetch, x => x.Customer)
            .Where(x => x.OrderId == 10248)
            .Cacheable()
            .Future();

            order = db.Orders
                    .Fetch(
                SelectMode.Fetch,
                x => x.OrderLines,
                x => x.OrderLines.First().Product,
                x => x.OrderLines.First().Product.OrderLines)
                    .Where(x => x.OrderId == 10248)
                    .Cacheable()
                    .Future()
                    .ToList()
                    .First();

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(2), "Unexpected cache hit count");
        }
Exemple #5
0
        public void QueryIsCacheable2()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            db.Customers.Cacheable().Take(1).List();
            db.Customers.Take(1).List();

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0), "Unexpected cache hit count");
        }
        public void FetchIsCachable()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            Order order;

            using (var s = Sfi.OpenSession())
                using (var t = s.BeginTransaction())
                {
                    order = s.Query <Order>()
                            .WithOptions(o => o.SetCacheable(true))
                            .Fetch(x => x.Customer)
                            .FetchMany(x => x.OrderLines)
                            .ThenFetch(x => x.Product)
                            .ThenFetchMany(x => x.OrderLines)
                            .Where(x => x.OrderId == 10248)
                            .ToList()
                            .First();

                    t.Commit();
                }

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count");

            Sfi.Statistics.Clear();

            using (var s = Sfi.OpenSession())
                using (var t = s.BeginTransaction())
                {
                    order = s.Query <Order>()
                            .WithOptions(o => o.SetCacheable(true))
                            .Fetch(x => x.Customer)
                            .FetchMany(x => x.OrderLines)
                            .ThenFetch(x => x.Product)
                            .ThenFetchMany(x => x.OrderLines)
                            .Where(x => x.OrderId == 10248)
                            .ToList()
                            .First();
                    t.Commit();
                }

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
        public void CacheableBeforeOtherClauses()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            db.Customers
            .WithOptions(o => o.SetCacheable(true))
            .Where(c => c.ContactName != c.CompanyName).Take(1).ToList();
            db.Customers.Where(c => c.ContactName != c.CompanyName).Take(1).ToList();

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0), "Unexpected cache hit count");
        }
        private void OneToOneUpdateTest <TPerson, TDetails>() where TPerson : Person, new() where TDetails : Details, new()
        {
            List <object> ids = this.CreatePersonAndDetails <TPerson, TDetails>();

            IStatistics statistics = Sfi.Statistics;

            // Clear the second level cache and the statistics
            Sfi.EvictEntity(typeof(TPerson).FullName);
            Sfi.EvictEntity(typeof(TDetails).FullName);
            Sfi.EvictQueries();

            statistics.Clear();

            // Fill the empty caches with data.
            this.FetchPeopleById <TPerson>(ids);

            // Verify that no data was retrieved from the cache.
            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
            statistics.Clear();

            int personId = DeleteDetailsFromFirstPerson <TPerson>();

            // Verify that the cache was updated
            Assert.AreEqual(1, statistics.SecondLevelCachePutCount, "Second level cache put count");
            statistics.Clear();

            // Verify that the Person was updated in the cache
            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    TPerson person = s.Get <TPerson>(personId);

                    Assert.IsNull(person.Details);
                }

            Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
            statistics.Clear();

            // Verify that the Details was removed from the cache and deleted.
            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    TDetails details = s.Get <TDetails>(personId);

                    Assert.Null(details);
                }

            Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
        }
 protected override void OnTearDown()
 {
     using (var s = OpenSession())
         using (var tx = s.BeginTransaction())
         {
             s.CreateQuery("delete from ReadOnlyItem").ExecuteUpdate();
             s.CreateQuery("delete from ReadWriteItem").ExecuteUpdate();
             s.CreateQuery("delete from ReadOnly").ExecuteUpdate();
             s.CreateQuery("delete from ReadWrite").ExecuteUpdate();
             tx.Commit();
         }
     // Must manually evict "readonly" entities since their caches are readonly
     Sfi.Evict(typeof(ReadOnly));
     Sfi.Evict(typeof(ReadOnlyItem));
     Sfi.EvictQueries();
 }
Exemple #10
0
        public void FetchIsCacheableForJoinAlias()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            Customer  customer     = null;
            OrderLine orderLines   = null;
            Product   product      = null;
            OrderLine prOrderLines = null;

            var order = db.Orders
                        .JoinAlias(x => x.Customer, () => customer)
                        .JoinAlias(x => x.OrderLines, () => orderLines, JoinType.LeftOuterJoin)
                        .JoinAlias(() => orderLines.Product, () => product)
                        .JoinAlias(() => product.OrderLines, () => prOrderLines, JoinType.LeftOuterJoin)
                        .Where(x => x.OrderId == 10248)
                        .Cacheable()
                        .List()
                        .First();

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count");

            Sfi.Statistics.Clear();
            Session.Clear();

            order = db.Orders
                    .JoinAlias(x => x.Customer, () => customer)
                    .JoinAlias(x => x.OrderLines, () => orderLines, JoinType.LeftOuterJoin)
                    .JoinAlias(() => orderLines.Product, () => product)
                    .JoinAlias(() => product.OrderLines, () => prOrderLines, JoinType.LeftOuterJoin)
                    .Where(x => x.OrderId == 10248)
                    .Cacheable()
                    .List()
                    .First();

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
Exemple #11
0
        public void FetchIsCacheable()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            var order = db.Orders
                        .Fetch(
                SelectMode.Fetch,
                x => x.Customer,
                x => x.OrderLines,
                x => x.OrderLines.First().Product,
                x => x.OrderLines.First().Product.OrderLines)
                        .Where(x => x.OrderId == 10248)
                        .Cacheable()
                        .List()
                        .First();

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count");

            Sfi.Statistics.Clear();
            Session.Clear();

            order = db.Orders
                    .Fetch(
                SelectMode.Fetch,
                x => x.Customer,
                x => x.OrderLines,
                x => x.OrderLines.First().Product,
                x => x.OrderLines.First().Product.OrderLines)
                    .Where(x => x.OrderId == 10248)
                    .Cacheable()
                    .List()
                    .First();

            AssertFetchedOrder(order);

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
Exemple #12
0
        public void CanBeCombinedWithFetch()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            db.Customers
            .Cacheable()
            .List();

            db.Orders
            .Cacheable()
            .Take(1)
            .List();

            db.Customers
            .Fetch(SelectMode.Fetch, x => x.Orders)
            .Cacheable()
            .Take(1)
            .List();

            db.Orders
            .Fetch(SelectMode.Fetch, x => x.OrderLines)
            .Cacheable()
            .Take(1)
            .List();

            db.Customers
            .Fetch(SelectMode.Fetch, x => x.Address)
            .Where(x => x.CustomerId == "VINET")
            .Cacheable()
            .SingleOrDefault();

            var customer = db.Customers
                           .Fetch(SelectMode.Fetch, x => x.Address)
                           .Where(x => x.CustomerId == "VINET")
                           .Cacheable()
                           .SingleOrDefault();

            Assert.That(NHibernateUtil.IsInitialized(customer.Address), Is.True, "Expected the fetched Address to be initialized");
            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(5), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(5), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
        public void CacheHqlQueryWithFetchAndTransformerThatChangeTuple()
        {
            if (!TestDialect.SupportsDuplicatedColumnAliases)
            {
                Assert.Ignore("Ignored due to GH-2092");
            }

            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            // the combination of query and transformer doesn't make sense.
            // It's simply used as example of returned data being transformed before caching leading to mismatch between
            // Loader.ResultTypes collection and provided tuple
            var order = session.CreateQuery("select o.Employee.FirstName, o from Order o join fetch o.Customer where o.OrderId = :id")
                        .SetInt32("id", 10248)
                        .SetCacheable(true)
                        .SetResultTransformer(Transformers.RootEntity)
                        .UniqueResult <Order>();

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count");
            Assert.That(order, Is.Not.Null);
            Assert.That(order.Customer, Is.Not.Null);
            Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.True);

            session.Clear();
            Sfi.Statistics.Clear();

            order = session.CreateQuery("select o.Employee.FirstName, o from Order o join fetch o.Customer where o.OrderId = :id")
                    .SetInt32("id", 10248)
                    .SetCacheable(true)
                    .SetResultTransformer(Transformers.RootEntity)
                    .UniqueResult <Order>();

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
            Assert.That(order, Is.Not.Null);
            Assert.That(order.Customer, Is.Not.Null);
            Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.True);
        }
        public void CanBeCombinedWithFetch()
        {
            //NH-2587
            //NH-3982 (GH-1372)

            Sfi.Statistics.Clear();
            Sfi.EvictQueries();

            db.Customers
            .WithOptions(o => o.SetCacheable(true))
            .ToList();

            db.Orders
            .WithOptions(o => o.SetCacheable(true))
            .ToList();

            db.Customers
            .WithOptions(o => o.SetCacheable(true))
            .Fetch(x => x.Orders)
            .ToList();

            db.Orders
            .WithOptions(o => o.SetCacheable(true))
            .Fetch(x => x.OrderLines)
            .ToList();

            var customer = db.Customers
                           .WithOptions(o => o.SetCacheable(true))
                           .Fetch(x => x.Address)
                           .Where(x => x.CustomerId == "VINET")
                           .SingleOrDefault();

            customer = db.Customers
                       .WithOptions(o => o.SetCacheable(true))
                       .Fetch(x => x.Address)
                       .Where(x => x.CustomerId == "VINET")
                       .SingleOrDefault();

            Assert.That(NHibernateUtil.IsInitialized(customer.Address), Is.True, "Expected the fetched Address to be initialized");
            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(5), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(5), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
Exemple #15
0
        public void HitCacheInSameSession()
        {
            Sfi.EvictQueries();
            Sfi.Statistics.Clear();
            var entities = new System.Collections.Generic.List <Entity>();

            using (var session = OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    for (int i = 0; i < 3; i++)
                    {
                        var e = new Entity {
                            Name = "Name" + i
                        };
                        entities.Add(e);
                        session.Save(e);
                    }
                    transaction.Commit();
                }

                var queryString = "from Entity";

                using (var tx = session.BeginTransaction())
                {
                    // this query will hit the database and create the cache
                    session.CreateQuery(queryString).SetCacheable(true).List();
                    tx.Commit();
                }

                using (var transaction = session.BeginTransaction())
                {
                    //and this one SHOULD served by the cache
                    session.CreateQuery(queryString).SetCacheable(true).List();
                    transaction.Commit();
                }

                var qs = Sfi.Statistics.GetQueryStatistics(queryString);
                Assert.AreEqual(1, qs.CacheHitCount);
                Assert.AreEqual(1, qs.CachePutCount);
            }
        }
        public void ProjectedEntitiesAreCachable()
        {
            Sfi.Statistics.Clear();
            Sfi.EvictQueries();
            var dto = session.Query <Order>()
                      .WithOptions(o => o.SetCacheable(true))
                      .Where(x => x.OrderId == 10248)
                      .Select(x => new { x.Customer, Order = x })
                      .FirstOrDefault();

            Assert.That(dto, Is.Not.Null, "dto should not be null");
            Assert.That(dto.Order, Is.Not.Null, "dto.Order should not be null");
            Assert.That(NHibernateUtil.IsInitialized(dto.Order), Is.True, "dto.Order should be initialized");
            Assert.That(dto.Customer, Is.Not.Null, "dto.Customer should not be null");
            Assert.That(NHibernateUtil.IsInitialized(dto.Customer), Is.True, "dto.Customer from cache should be initialized");

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count");

            Sfi.Statistics.Clear();
            session.Clear();

            dto = session.Query <Order>()
                  .WithOptions(o => o.SetCacheable(true))
                  .Where(x => x.OrderId == 10248)
                  .Select(x => new { x.Customer, Order = x })
                  .FirstOrDefault();

            Assert.That(dto, Is.Not.Null, "dto from cache should not be null");
            Assert.That(dto.Order, Is.Not.Null, "dto.Order from cache should not be null");
            Assert.That(NHibernateUtil.IsInitialized(dto.Order), Is.True, "dto.Order from cache should be initialized");
            Assert.That(dto.Customer, Is.Not.Null, "dto.Customer from cache should not be null");
            Assert.That(NHibernateUtil.IsInitialized(dto.Customer), Is.True, "dto.Customer from cache should be initialized");

            Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count");
            Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count");
            Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count");
            Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
        }
Exemple #17
0
        public void SimpleProjections()
        {
            var transformer = new CustomTransformer();

            Sfi.EvictQueries();
            Sfi.Statistics.Clear();

            const string queryString = "select i.Name, i.Description from AnotherItem i where i.Name='widget'";

            object savedId;

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    s.CreateQuery(queryString).SetCacheable(true).List();
                    var i = new AnotherItem {
                        Name = "widget"
                    };
                    savedId = s.Save(i);
                    tx.Commit();
                }

            QueryStatistics  qs = Sfi.Statistics.GetQueryStatistics(queryString);
            EntityStatistics es = Sfi.Statistics.GetEntityStatistics(typeof(AnotherItem).FullName);

            Thread.Sleep(200);

            IList result;

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    s.CreateQuery(queryString).SetCacheable(true).List();
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(0));

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    s.CreateQuery(queryString).SetCacheable(true).List();
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(1));

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    s.CreateQuery(queryString).SetCacheable(true).SetResultTransformer(transformer).List();
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(2), "hit count should go up since the cache contains the result before the possible application of a resulttransformer");

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    s.CreateQuery(queryString).SetCacheable(true).SetResultTransformer(transformer).List();
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(3), "hit count should go up since we are using the same resulttransformer");
            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    result = s.CreateQuery(queryString).SetCacheable(true).List();
                    Assert.That(result.Count, Is.EqualTo(1));
                    var i = s.Get <AnotherItem>(savedId);
                    i.Name = "Widget";
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(4));
            Assert.That(qs.CacheMissCount, Is.EqualTo(2));

            Thread.Sleep(200);

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    s.CreateQuery(queryString).SetCacheable(true).List();

                    var i = s.Get <AnotherItem>(savedId);
                    Assert.That(i.Name, Is.EqualTo("Widget"));

                    s.Delete(i);
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(4));
            Assert.That(qs.CacheMissCount, Is.EqualTo(3));
            Assert.That(qs.CachePutCount, Is.EqualTo(3));
            Assert.That(qs.ExecutionCount, Is.EqualTo(3));
            Assert.That(es.FetchCount, Is.EqualTo(0));             //check that it was being cached
        }
Exemple #18
0
        public void QueryCacheInvalidation()
        {
            Sfi.EvictQueries();
            Sfi.Statistics.Clear();

            const string queryString = "from Item i where i.Name='widget'";

            object savedId = CreateItem(queryString);

            QueryStatistics  qs = Sfi.Statistics.GetQueryStatistics(queryString);
            EntityStatistics es = Sfi.Statistics.GetEntityStatistics(typeof(Item).FullName);

            Thread.Sleep(200);

            IList result;

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    result = s.CreateQuery(queryString).SetCacheable(true).List();
                    Assert.That(result.Count, Is.EqualTo(1));
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(0));

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    result = s.CreateQuery(queryString).SetCacheable(true).List();
                    Assert.That(result.Count, Is.EqualTo(1));
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(1));
            Assert.That(es.FetchCount, Is.EqualTo(0));

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    result = s.CreateQuery(queryString).SetCacheable(true).List();
                    Assert.That(result.Count, Is.EqualTo(1));
                    Assert.That(NHibernateUtil.IsInitialized(result[0]));
                    var i = (Item)result[0];
                    i.Name = "Widget";
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(2));
            Assert.That(qs.CacheMissCount, Is.EqualTo(2));
            Assert.That(es.FetchCount, Is.EqualTo(0));

            Thread.Sleep(200);

            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    s.CreateQuery(queryString).SetCacheable(true).List();

                    var i = s.Get <Item>(savedId);
                    Assert.That(i.Name, Is.EqualTo("Widget"));

                    s.Delete(i);
                    tx.Commit();
                }

            Assert.That(qs.CacheHitCount, Is.EqualTo(2));
            Assert.That(qs.CacheMissCount, Is.EqualTo(3));
            Assert.That(qs.CachePutCount, Is.EqualTo(3));
            Assert.That(qs.ExecutionCount, Is.EqualTo(3));
            Assert.That(es.FetchCount, Is.EqualTo(0));             //check that it was being cached
        }