9.使用高速缓存提高性能

来源:互联网 发布:淘宝正品代购 编辑:程序博客网 时间:2024/06/05 20:56


1. 一个简单的ibatis高速缓存示例

<cacheModel id="categoryCache" type="MEMORY"><flushOnExecute statement="insert" /><flushOnExecute statement="update" /><flushOnExecute statement="delete" /><property name="reference-type" value="WEAK" /></cacheModel><select id="getCategory" parameterClass="Category" resultClass="Category"cacheModel="categoryCache">SELECT *FROM CategoryWHERE categoryId=#categoryId#</select>





2.ibatis高速缓存的理念

ibatis是一个以数据为中心的框架,关注的是SQL语句的执行结果,所以我们不会根据对象的唯一性来高速缓存他们ibatis高速缓存返回的所有结果,而不考虑所标识的对象是否存在于高速缓存中。

3 理解高速缓存模型





3.1 type属性





3.2 readOnly属性


用于告诉高速缓存模型应该如何检索和保存已高速缓存的对象。

3.3 serialize属性


用于指示高速缓存对象应该如何返回。

3.4 联合使用readOnly属性和serialize属性





4. 如何使用高速缓存模型中的标签


4.1 高速缓存的清除




<sqlMap namespace="Category">…<cacheModel id="categoryCache" type="MEMORY">…<flushOnExecute statement="Category.insert" />…</cacheModel>…<select id="getCategory" parameterClass="Category" resultClass="Category"cacheModel="categoryCache">SELECT *FROM CategoryWHERE parentCategoryId=#categoryId#</select>…<insert id="insert" parameterClass="Category">INSERT INTO Category(title,description,sequence)VALUES(#title#,#description#,#sequence#)</insert>…</sqlMap>


但是只允许指定一个属性。


4.2  设置高速缓存模型实现的特性




5.  高速缓存模型的类型


5.1 MEMORY


基于引用的高速缓存



MEMORY高速缓存类型只有一个特性,即reference-type,用于指定期望使用的引用类型。

<cacheModel id="categoryCache" type="MEMORY"><flushInterval hours="24" /><flushOnExecute statement="insert" /><flushOnExecute statement="update" /><flushOnExecute statement="delete" /><property name="reference-type" value="WEAK" /></cacheModel>

5.2 LRU


使用最近最少使用策略管理高速缓存。


大小限制定义了高速缓存中可以包含的对象数目。避免将那些大的内存对象放置在此类高速缓存中。


<cacheModel id="categoryCache" type="LRU"><flushInterval hours="24" /><flushOnExecute statement="insert" /><flushOnExecute statement="update" /><flushOnExecute statement="delete" /><property name="size" value="200" /></cacheModel>

5.3 FIFO


先进先出管理策略。大小限制定义了高速缓存中可以包含的对象数目。避免将那些大的内存对象放置在此类高速缓存中。

<cacheModel id="categoryCache" type="FIFO"><flushInterval hours="24" /><flushOnExecute statement="insert" /><flushOnExecute statement="update" /><flushOnExecute statement="delete" /><property name="size" value="1000" /></cacheModel>

5.4 OSCACHE

对OSCache JAR文件依赖。oscache.properties文件 放在类路径的根目录下
<cacheModel id="categoryCache" type="OSCACHE"><flushInterval hours="24" /><flushOnExecute statement="insert" /><flushOnExecute statement="update" /><flushOnExecute statement="delete" /></cacheModel>

5.5 自己的高速缓存模型


第一:以上4种高速缓存模型实际上是com.ibatis.sqlmap.engine.cache.CacheController接口的具体实现
第二:他们的名称实际上只是映射到这些实现的全限定名的别名


6. 确定高速缓存策略


6.1 高速缓存只读的长效数据


6.2 高速缓存可读写数据


6.3 高速缓存旧的静态数据



0 0
原创粉丝点击