Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Second Level Cache With NHibernate Linq Provider 1.0

How to enable NHibernate Second-Level Cache with NHibernate Linq Provider 1.0 ?

Second-Level Cache seems to work only with ICriteria usage.

like image 204
Yoann. B Avatar asked Sep 03 '09 18:09

Yoann. B


3 Answers

Yes, I finally worked this one out:

public IQuerable<T> CreateLinqQuery()
{
    var query = session.Linq<T>();
    query.QueryOptions.SetCachable(true);
    return query;
}

Update As others have pointed out, in NH3, use query.Cacheable(). However be very careful to do it like this:

// Correct way:
query = query.Cacheable();

// This won't work:
query.Cacheable();
like image 74
cbp Avatar answered Sep 20 '22 16:09

cbp


For NHibernate 3+ it's session.Query<T>().Cacheable()

like image 26
Mauricio Scheffer Avatar answered Sep 20 '22 16:09

Mauricio Scheffer


NH3 RC version:

public IQueryable<T> Queryable<T>()
{
    IQueryable<T> queryable = SessionFactory.OpenSession().Queryable<T>();
    queryable = queryable.Cacheable<T>();

    return queryable;
}
like image 21
Chris S Avatar answered Sep 20 '22 16:09

Chris S