NHibernate Tour (24): Explore NHibernate Level two cache (next)

Source: Internet
Author: User

The contents of this section

Introduced

Using the NHibernate level two cache

Enable caching queries

Manage NHibernate Level Two cache

Conclusion

Introduced

Oh, today received Microsoft "most influential developer" gift package, I am very pleased to have won the title of "Microsoft's most Influential developer" (Please enter), this article I also continue to talk about the topic of the NHibernate level two cache the remaining content, such as you modify, delete data, what is the level two cache strategy? What if we use a cached query? How do I manage the nhibernate level two cache?

Using the NHibernate level two cache

Do not know the specific configuration, please go to the NHibernate trip series of articles to navigate the contents of the previous article, this article we write a few tests to see the NHibernate two cache some details:

Test 1: Update data

When we enable level two caching, what is the data in level two cache if the data is queried for the first time and then modified? Let's write a test to see what it is:

[Test]
public void SessionFactoryCacheUpdateTest()
{
  string firstname="YJingLee";
  using (_session)
  {
    using (var tx = _session.BeginTransaction())
    {
      Console.WriteLine("第一次读取持久化实例");
      Customer customer1 = _session.Get<Customer>(1);
      Console.WriteLine("更新持久化实例");
      customer1.Name.Firstname =firstname;
      tx.Commit();
    }
  }
  ResetSession();
  Console.WriteLine("第二次读取持久化实例");
  using (_session)
  {
    Customer customer2 = _session.Get<Customer>(1);
    Console.WriteLine("新FirstName为:{0}",customer2.Name.Firstname);
    Assert.AreEqual(customer2.Name.Firstname, firstname);
  }
}

Output results:

Analysis: In the first query of data, because the level, level two cache does not exist the required data, then NHibernate from the database query data. We modify this data and submit it to the database, NHibernate executes an UPDATE statement, because we set the read and write cache policy, nhibernate updated the data content in level two cache, read this data the second time, NHibernate first from the built-in cache (one level cache) To find out if the required data exists, because it is not in the same isession, so the required data does not exist in the built-in ISession cache, and NHibernate queries the level two cache, then because of the first query of this data, there is the required data in the level two cache, The data in the cache is used directly. The data in the cache is also updated.

As for deleting, inserting data I think it is similar. I'm not going to write a test here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.