📄 performance.html
字号:
那么就可以对其进行<tt class="literal">只读</tt> 缓存。这是最简单,也是实用性最好的方法。甚至在集群中,它也能完美地运作。 </p><pre class="programlisting"><class name="eg.Immutable" mutable="false"> <cache usage="read-only"/> ....</class></pre></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="performance-cache-readwrite"></a>20.2.3. 策略:读/写缓存(Strategy: read/write) </h3></div></div><div></div></div><p> 如果应用程序需要更新数据,那么使用<tt class="literal">读/写缓存</tt> 比较合适。 如果应用程序要求“序列化事务”的隔离级别(serializable transaction isolation level),那么就决不能使用这种缓存策略。 如果在JTA环境中使用缓存,你必须指定<tt class="literal">hibernate.transaction.manager_lookup_class</tt>属性的值, 通过它,Hibernate才能知道该应用程序中JTA的<tt class="literal">TransactionManager</tt>的具体策略。 在其它环境中,你必须保证在<tt class="literal">Session.close()</tt>、或<tt class="literal">Session.disconnect()</tt>调用前, 整个事务已经结束。 如果你想在集群环境中使用此策略,你必须保证底层的缓存实现支持锁定(locking)。Hibernate内置的缓存策略并不支持锁定功能。 </p><pre class="programlisting"><class name="eg.Cat" .... > <cache usage="read-write"/> .... <set name="kittens" ... > <cache usage="read-write"/> .... </set></class></pre></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="performance-cache-nonstrict"></a>20.2.4. 策略:非严格读/写缓存(Strategy: nonstrict read/write) </h3></div></div><div></div></div><p> 如果应用程序只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离, 那么比较适合使用<tt class="literal">非严格读/写缓存</tt>策略。如果在JTA环境中使用该策略, 你必须为其指定<tt class="literal">hibernate.transaction.manager_lookup_class</tt>属性的值, 在其它环境中,你必须保证在<tt class="literal">Session.close()</tt>、或<tt class="literal">Session.disconnect()</tt>调用前, 整个事务已经结束。 </p></div><div class="sect2" lang="zh-cn"><div class="titlepage"><div><div><h3 class="title"><a name="performance-cache-transactional"></a>20.2.5. 策略:事务缓存(transactional) </h3></div></div><div></div></div><p> Hibernate的<tt class="literal">事务缓存</tt>策略提供了全事务的缓存支持, 例如对JBoss TreeCache的支持。这样的缓存只能用于JTA环境中,你必须指定 为其<tt class="literal">hibernate.transaction.manager_lookup_class</tt>属性。 </p></div><p> 没有一种缓存提供商能够支持上列的所有缓存并发策略。下表中列出了各种提供器、及其各自适用的并发策略。 </p><div class="table"><a name="d0e13479"></a><p class="title"><b>表 20.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) </b></p><table summary="
 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support)
 " border="1"><colgroup><col align="left"><col align="left"><col align="left"><col align="left"><col align="left"></colgroup><thead><tr><th align="left">Cache</th><th align="left">read-only</th><th align="left">nonstrict-read-write</th><th align="left">read-write</th><th align="left">transactional</th></tr></thead><tbody><tr><td align="left">Hashtable (not intended for production use)</td><td align="left">yes</td><td align="left">yes</td><td align="left">yes</td><td align="left"> </td></tr><tr><td align="left">EHCache</td><td align="left">yes</td><td align="left">yes</td><td align="left">yes</td><td align="left"> </td></tr><tr><td align="left">OSCache</td><td align="left">yes</td><td align="left">yes</td><td align="left">yes</td><td align="left"> </td></tr><tr><td align="left">SwarmCache</td><td align="left">yes</td><td align="left">yes</td><td align="left"> </td><td align="left"> </td></tr><tr><td align="left">JBoss TreeCache</td><td align="left">yes</td><td align="left"> </td><td align="left"> </td><td align="left">yes</td></tr></tbody></table></div></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="performance-sessioncache"></a>20.3. 管理缓存(Managing the caches) </h2></div></div><div></div></div><p> 无论何时,当你给<tt class="literal">save()</tt>、<tt class="literal">update()</tt>或 <tt class="literal">saveOrUpdate()</tt>方法传递一个对象时,或使用<tt class="literal">load()</tt>、 <tt class="literal">get()</tt>、<tt class="literal">list()</tt>、<tt class="literal">iterate()</tt> 或<tt class="literal">scroll()</tt>方法获得一个对象时, 该对象都将被加入到<tt class="literal">Session</tt>的内部缓存中。 </p><p> 当随后flush()方法被调用时,对象的状态会和数据库取得同步。 如果你不希望此同步操作发生,或者你正处理大量对象、需要对有效管理内存时,你可以调用<tt class="literal">evict()</tt> 方法,从一级缓存中去掉这些对象及其集合。 </p><pre class="programlisting">ScrollableResult cats = sess.createQuery("from Cat as cat").scroll(); //a huge result setwhile ( cats.next() ) { Cat cat = (Cat) cats.get(0); doSomethingWithACat(cat); sess.evict(cat);}</pre><p> Session还提供了一个<tt class="literal">contains()</tt>方法,用来判断某个实例是否处于当前session的缓存中。 </p><p> 如若要把所有的对象从session缓存中彻底清除,则需要调用<tt class="literal">Session.clear()</tt>。 </p><p> 对于二级缓存来说,在<tt class="literal">SessionFactory</tt>中定义了许多方法, 清除缓存中实例、整个类、集合实例或者整个集合。 </p><pre class="programlisting">sessionFactory.evict(Cat.class, catId); //evict a particular CatsessionFactory.evict(Cat.class); //evict all CatssessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittenssessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections</pre><p> <tt class="literal">CacheMode</tt>参数用于控制具体的Session如何与二级缓存进行交互。 </p><div class="itemizedlist"><ul type="disc"><li><p> <tt class="literal">CacheMode.NORMAL</tt> - 从二级缓存中读、写数据。 </p></li><li><p> <tt class="literal">CacheMode.GET</tt> - 从二级缓存中读取数据,仅在数据更新时对二级缓存写数据。 </p></li><li><p> <tt class="literal">CacheMode.PUT</tt> - 仅向二级缓存写数据,但不从二级缓存中读数据。 </p></li><li><p> <tt class="literal">CacheMode.REFRESH</tt> - 仅向二级缓存写数据,但不从二级缓存中读数据。通过 <tt class="literal">hibernate.cache.use_minimal_puts</tt>的设置,强制二级缓存从数据库中读取数据,刷新缓存内容。 </p></li></ul></div><p> 如若需要查
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -