缓存----Ibatis /Hibernate

iBatis缓存的使用方法及解释 :以iBatis2.3为例,做以下配置即可实现对某些查询进行缓存

1、<settings lazyLoadingEnabled="false"  cacheModelsEnabled="true"  enhancementEnabled="true" />

    注释:

            lazyLoadingEnabled  延迟加载数据;

            cacheModelsEnabled  全局性启用或关闭SqlMapCilent cache缓存;

            enhancementEnabled  运行时,字节码增强

2、在sqlMap文件中加入cacheModel的配置:

    <cacheModel  id="model-cache" type="MEMORY" readOnly="true" >

       <flushInterval seconds="60"/>
       <flushOnExecute  statement="insertProduct"/>
       <flushOnExecute  statement="updateProduct"/>
       <flushOnExecute  statement="deleteProduct"/>

       <property name="size" value="1000" />
    </cacheModel>

      注意:如果在sqlMapConfig里面设置了useStatementNamespaces="true",那么上面的flushOnExecute中的statement里面需要带上namespace,如<flushOnExecute statement="Product.insertProduct"/>,如果useStatementNamespaces="false",则可以直接按上面的写。

扫描二维码关注公众号,回复: 528885 查看本文章

    注释:

            cacheModel的id属性用于后面的statement引用;

            type属性标识采用何种cache方式:

                  MEMORY---使用 reference 类型来管理 cache 的行为。垃圾收集器可以根据 reference类型判断是否要回收 cache 中的数据。MEMORY实现适用于没有统一的对象重用模式的应用,或内存不足的应用

                  LRU---用“近期最少使用”原则来确定如何从 Cache 中清除对象,当 Cache溢出时,最近最少使用的对象将被从 Cache 中清除

                  FIFO---用“先进先出”原则来确定如何从 Cache 中清除对象。当 Cache 溢出时,最先进入 Cache 的对象将从 Cache 中清除。

                  OSCACHE---是OSCache2.0缓存引擎的一个 Plugin。它具有高度的可配置性,分布式,高度的灵活性。 
           flushInterval 属性是配置自动更新缓存的周期,可以是hours,minutes,seconds或milliseconds

           flushOnExecute这是标识什么时候触发更新,statement的名字就是后面的增删改语句的statement的id

3、配置需要进行cache的sql statment:

      <statement id="getProductList"  parameterClass="int"  cacheModel="model-cache">
          select * from PRODUCT where PRD_CAT_ID = #value#
      </statement>

    注释:cacheModel="model-cache"---指定该语句执行上面定义的model-cache缓存

猜你喜欢

转载自2277259257.iteye.com/blog/2162465
今日推荐