开发者

NHibernate query cache not used

I am using NH 2.1.2.4.

I have the query cache setup with Fluent NHibernate like this:

MsSqlConfiguration.MsSql2005
    .Cache(c => c.UseQueryCache()
            .ProviderClass(typeof(NHibernate.Caches.SysCache2.SysCacheProvider)))

My criteria looks like this:

Session.CreateCriteria<Employee>()
    .Add(Restrictions.Eq("Username", username))
    .SetMaxResults(1)
    .SetCacheable(true)
    .UniqueResult<Employee>();

However, when I use SQL Profiler I can see the query is still always being executed against the database. No inserts or updates to the Employee table are being made. Why is it not working?

My Employee mapping class specifies Employee as cachable like this:

public sealed class EmployeeDbMap : ClassMap<Employee>
{
    public EmployeeDbMap()
    {
        ....

        Cache.ReadWrite();
    }
} 
开发者_Go百科

BTW I see there are a number of related questions on Stackoverflow, but none seem to have good answers: here and here.


If you are not caching the Employee entity too, a query will be needed to load it, as the query cache stores the results as ids.

Also, NHibernate cache only works if you do all your work (including queries) inside transactions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜