Cannot Cache NHibernate Future Criteria Results

Posted by Emilian on Stack Overflow See other posts from Stack Overflow or by Emilian
Published on 2010-04-14T13:17:48Z Indexed on 2010/04/14 13:23 UTC
Read the original article Hit count: 452

Filed under:
|
|

I have the following code:

public void FuturesQuery()
{
    using (var session = SessionFactory.OpenSession())
    {
        var blogs = session.CreateCriteria<Blog>()
            .SetMaxResults(5)
            .SetCacheable(true)
            .SetCacheMode(CacheMode.Normal)
            .SetCacheRegion("BlogQuery")
            .Future<Blog>();
        var countOfBlogs = session.CreateCriteria<Blog>()
            .SetProjection(Projections.Count(Projections.Id()))
            .SetCacheable(true)
            .SetCacheMode(CacheMode.Normal)
            .SetCacheRegion("BlogQuery")
            .FutureValue<int>();
        Console.WriteLine("Number of blogs: {0}", countOfBlogs.Value);
        foreach (var blog in blogs)
        {
            Console.WriteLine(blog.Title);
        }
    }

    using (var session = SessionFactory.OpenSession())
    {
        var blogs = session.CreateCriteria<Blog>()
            .SetMaxResults(5)
            .SetCacheable(true)
            .SetCacheMode(CacheMode.Normal)
            .SetCacheRegion("BlogQuery")
            .Future<Blog>();
        var countOfBlogs = session.CreateCriteria<Blog>()
            .SetProjection(Projections.Count(Projections.Id()))
            .SetCacheable(true)
            .SetCacheMode(CacheMode.Normal)
            .SetCacheRegion("BlogQuery")
            .FutureValue<int>();
        Console.WriteLine("Number of blogs: {0}", countOfBlogs.Value);
        foreach (var blog in blogs)
        {
            Console.WriteLine(blog.Title);
        }
    }
}

I was expecting that the second time I query for blogs and count of blogs I will get values from cache but instead the queries hit the database. If I don't use Futures I get the expected results.

Does this means that results from Criteria using futures cannot be cached?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about futures