`
weishuwei
  • 浏览: 322184 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ibatis缓存

阅读更多

1.SqlMapConfig.xml中
<settings
 cacheModelsEnabled="true" //设置为true
enhancementEnabled="true"
 lazyLoadingEnabled="true"
.............
 />

2.cacheModel cacheModel的属性值等于指定的cacheModel元素的name属性值。属性cacheModel定义查询mapped statement的缓存。每一个查询mapped statement可以使用不同或相同的cacheModel。详细讨论见后面的章节,以下只给出个例子。
<cacheModel id="product-cache" imlementation="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
 <flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”size” value=”1000” />
</cacheModel>

<statement id=”getProductList” parameterClass=”int” cacheModel=”product-cache”>
select * from PRODUCT where PRD_CAT_ID = #value#
</statement>
上面例子中,“getProductList”的缓存使用WEAK引用类型,每24小时刷新一次,或当更新的操作发生时刷新。

(1)动态查询情况下的缓存:
没有参数时缓存结果,当有参数时,会重新查询一次数据库,然后把查询的结果放到缓存里;总结即当查询参数一致时,会从缓存中读取,但参数发生变化时,会根据参数重新查询数据库,并且把数据放入缓存中,当下次查询参数一致时,就会从数据库中查询.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<cacheModel id="oneday_cache" type="LRU" readOnly="false" serialize="true">
       <flushInterval hours="24"/>
       <flushOnExecute statement="updateMyDate"/>
       <property name="size" value="200"/>
 </cacheModel>  
 
(1)属性readOnly如果不写,默认是true,这时的缓存效果无疑最好,因为系统不需要考虑更新操作引起缓存与实际数据不一致的问题,只读缓存的例子是固化到数据库中的一些配置参数表。但是,通常我们想缓存的数据是需要增删改的,这时务必记得要加上readOnly = "false";

注意:readOnly针对的是缓存,即便设置readOnly="true",修改数据库信息后,缓存的信息也会同步变化(会通过再次查询一下数据库,然后放到缓存当中去)

(2)属性serialize,如果不写,默认为false, 将它设为true,可以提高整体应用(而不仅仅是每个Session)的性能。这种缓存为每一个Session返回缓存对象不同的实例(复本)。因此每一个Session都可以安全修改返回的对象.注意,此时readOnly必须为false。
如果你把它设为 true ,记得检查两件事,一件事是缓存中存放的对象(你想查询的POJO)必须是可序列化的, 即实现Serializable接口。如果你有一个复杂对象属性,它也必须满足这个规则,你的整个对象树必须是可序列化的。
      另一件事是关闭sql-map-config中的延迟加载属性,即lazyload=false,原因是,它使用了动态代理机制, 那个代理对象并不是Serializable的。
   
    缓存类型的最佳适应情形:

    MEMORY     没有统一的对象重用模式的应用,或内存不足的应用。
    LRU        在较长的期间内,用户经常使用某些特定对象。
    FIFO       用户在短时间内持续引用特定的查询,而后很可能不再使用。
   
    根据个人实践,内存充足时使用LRU,否则使用MEMORY(WEAK)通常能获得较好的效果。

※《ibatis开发指南》有关于上面几种缓存类型的详细说明。

<cacheModel type="OSCACHE" id="cache">
    <flushInterval hours="24"/>
    <flushOnExecute statement="WapIndex.delete"/>
    <flushOnExecute statement="WapIndex.update"/>
    <property value="100" name="size"/>
</cacheModel>

++++++++++++++++++++++++++++++++++++++++++++=
IBatis中的强制刷新缓存
(1)刷新指定的缓存
public void flushDataCache(java.lang.String cacheId)
Flushes the data cache that matches the cache model ID provided. cacheId should include the namespace, even when useStatementNamespaces="false".
由上所述:即便我们配置useStatementNamespaces="false",调用的cacheId时也要包括namespace;
(2)刷新所有缓存
你也可以调用client.flushDataCache(),这样就刷新了所有的缓存模型而不是某一个.
++++++++++++++++++++++++++++++++++++++++++++

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics