01-08-03【Nhibernate (版本3.3.1.4000) 出入江湖】二級緩衝:NHibernate內建的HashtableProvider之緩衝管理

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

http://www.cnblogs.com/lyj/archive/2008/11/28/1343418.html

 

管理NHibernate二級緩衝

   NHibernate二級緩衝由ISessionFactory建立並由ISessionFactory自我維護。我們使用NHibernate操作資料時,ISessionFactory能夠自動同步緩衝,保證緩衝的有效性。但是當我們大量操作資料時,往往NHibernate不能維護緩衝持久有效。ISessionFactory提供了可程式化方式的緩衝管理方法。

 

ISessionFactory提供了一系列的EvictXXX()方法可以方便的從二級緩衝中刪除一個執行個體、刪除一個集合、一個具名快取等操作

 

?Evict(persistentClass):從二級緩衝中刪除persistentClass類所有執行個體

?Evict(persistentClass, id):從二級緩衝中刪除指定的持久化執行個體

?EvictEntity(entityName):從二級緩衝中刪除具名執行個體

?EvictCollection(roleName):從二級緩衝中刪除集合

?EvictCollection(roleName, id):從二級緩衝中刪除指定的集合

?EvictQueries():從二級緩衝中重新整理全部查詢結果集

?EvictQueries(cacheRegion):從二級緩衝中重新整理指定查詢結果集

ISession內建緩衝可以共用ISessionFactory緩衝,通過指定ISession的CacheMode可以控制ISession和ISessionFactory的互動方式。ISession可以通過以下五種方式和ISessionFactory互動:

 

?Ignore:更新資料時將二級緩衝失效,其它時間不和二級緩衝互動

?Put:向二級緩衝寫資料,但不從二級緩衝讀資料

?Get:從二級緩衝讀資料,僅在資料更新時向二級緩衝寫資料

?Normal:預設。從二級緩衝讀/寫資料

?Refresh:向二級緩衝寫資料,想不從二級緩衝讀資料,通過在設定檔設定cache.use_minimal_puts從資料庫中讀取資料時,強制二級緩衝重新整理

 

測試5:管理NHibernate二級緩衝

我們可以使用ISessionFactory提供了一系列的EvictXXX()方法從二級緩衝中刪除一個執行個體,看看這個例子在第一次讀取持久化執行個體時,結果集儲存在二級緩衝中,使用Evict方法從二級緩衝中刪除所有持久化執行個體,第二次查詢相同資料,二級緩衝中不存在則重新從資料庫中查詢了~~

[Test]public void SessionFactoryManageTest(){    ISessionFactory _sessionFactory = (new Configuration()).Configure().BuildSessionFactory();    Console.WriteLine("第一次讀取持久化執行個體");    using (ISession _session = _sessionFactory.OpenSession())    {        Customer customer1 = _session.Get<Customer>(1);        Customer customer2 = _session.Get<Customer>(2);    }    Console.WriteLine("從二級緩衝中刪除Customer類所有執行個體");    _sessionFactory.Evict(typeof(Customer));    //也可以_sessionFactory.EvictEntity("DomainModel.Entities.Customer");    Console.WriteLine("第二次讀取持久化執行個體");    using (ISession _session = _sessionFactory.OpenSession())    {        Customer customer1 = _session.Get<Customer>(1);    }}

 

測試6:強制重新整理快取區域

我們使用ISession提供的.SetCacheMode(CacheMode.Refresh) 方法可以強制重新整理快取區域,這樣可以避免資料不一致問題~~

[Test]public void QueryCacheTest(){    using (_session)    {        Console.WriteLine("第一次查詢某資料,顯式緩衝查詢結果");        IList<Customer> customers =             _session.CreateQuery("from Customer c where c.CustomerId > 2")            .SetCacheable(true)            .SetCacheRegion("queryCache")            .List<Customer>();        Assert.AreEqual(11, customers.Count);    }    ResetSession();    using (_session)    {        Console.WriteLine("第二次查詢某資料,顯式緩衝查詢結果");        Console.WriteLine("----指定特定的具名快取地區並強制重新整理快取區域----");        IList<Customer> customers =             _session.CreateQuery("from Customer c where c.CustomerId > 2")            .SetCacheable(true)            .SetCacheRegion("queryCache")            .SetCacheMode(CacheMode.Refresh)            .List<Customer>();        Assert.AreEqual(11, customers.Count);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.