Cannot Cache NHibernate Future Criteria Results
- by Emilian
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?