Can first level cache be used with ICriteria or other APIs?

前端 未结 2 354
春和景丽
春和景丽 2021-01-19 20:48

In NHibernate you can easily benefit from first level cache when using Load or Get methods. But what about ICriteria,

相关标签:
2条回答
  • 2021-01-19 21:08

    No, as I understand they don't. They use only second level cache. Firs level cache is only for Get and Load.

    0 讨论(0)
  • 2021-01-19 21:13

    They use it for returning entities, but the queries go straight to the db unless you use the second level cache.

    Consider this:

    var fooUsingGet = session.Get<Foo>(fooId);
    var fooQueryById = session.Query<Foo>().Single(f => f.Id == fooId);
    

    Two queries are executed (one for the Get, one for the Query), but both variables contain the same object reference.

    Now, if you enable the 2nd level cache, query caching, and specify caching for the query:

    var fooQueryById = session.Query<Foo>().Cacheable()
                              .Single(f => f.Id == fooId);
    var fooQueryByIdAgain = session.Query<Foo>().Cacheable()
                                   .Single(f => f.Id == fooId);
    

    Only one query will be executed.

    0 讨论(0)
提交回复
热议问题