IBatis学习关于缓存的使用范例

    技术2022-05-19  22

        在特定硬件基础上(同时假设系统不存在设计上的缺漏和糟糕低效的SQL 语句)Cache往往是提升系统性能的最关键因素)。    相对Hibernate 等封装较为严密的ORM 实现而言(因为对数据对象的操作实现了较为严密的封装,可以保证其作用范围内的缓存同步,而ibatis 提供的是半封闭的封装实现,因此对缓存的操作难以做到完全的自动化同步)。

        ibatis 的缓存机制使用必须特别谨慎。特别是flushOnExecute 的设定(见“ibatis配置”一节中的相关内容),需要考虑到所有可能引起实际数据与缓存数据不符的操作。如本模块中其他Statement对数据的更新,其他模块对数据的更新,甚至第三方系统对数据的更新。否则,脏数据的出现将为系统的正常运行造成极大隐患。如果不能完全确定数据更新操作的波及范围,建议避免Cache的盲目使用。 

     

    4种缓存模式的基本配置(Xxx.xml中):

        MEMORY类型的Cache正是借助SoftReference、WeakReference以及通常意义上的JavaReference实现了对象的缓存管理。

        LRU型Cache 当Cache达到预先设定的最大容量时,ibatis会按照“最少使用”原则将使用频率最少的对象从缓冲中清除。

        FIFO型Cache先进先出型缓存,最先放入Cache中的数据将被最先废除。

        OSCache与上面几种类型的Cache不同,OSCache来自第三方组织Opensymphony   

     

    <!--type: MEMORY;FIFO(先进先出);LRU(最少使用);OSCACHE; 以上4者均为缩写-->

        <cacheModel id="deptCacheWithMEMORY" type="MEMORY" readOnly="false" serialize="true">        <!--flushInterval 间隔多长时间清除缓存 -->        <!--flushOnExecute 当发生什么操作时清除缓存 -->     <flushInterval hours="12"/>     <flushOnExecute statement="Department.addOneDept"/>     <flushOnExecute statement="Department.updateOneDept"/>     <flushOnExecute statement="Department.deleteOneDept"/>     <property name="reference-type" value="WEAK"/>    </cacheModel>        <cacheModel id="deptCacheWithFIFO" type="FIFO" readOnly="false" serialize="true">        <!--flushInterval 间隔多长时间清除缓存 -->        <!--flushOnExecute 当发生什么操作时清除缓存 -->     <flushInterval hours="12"/>     <flushOnExecute statement="Department.updateOneDept"/>     <property name="size" value="2"/>    </cacheModel>     <cacheModel id="deptCacheWithLRU" type="LRU" readOnly="false" serialize="true">        <!--flushInterval 间隔多长时间清除缓存 -->        <!--flushOnExecute 当发生什么操作时清除缓存 -->     <flushInterval hours="12"/>     <flushOnExecute statement="Department.updateOneDept"/>     <property name="size" value="10"/>    </cacheModel>        <!--cacheModel:此查询所使用的缓存模式 --> <select id="getAllDeptWithMemory" resultClass="department"  cacheModel="deptCacheWithMEMORY">  select deptNo,dname as deptName,loc as location from dept </select>


    最新回复(0)