日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

缓存初解(四)---Ibatis的缓存配置+Ehcache

發布時間:2025/7/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 缓存初解(四)---Ibatis的缓存配置+Ehcache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目完結,整理一些技術方面的相關收獲。
已經記不得EhCacheController這個實現類最早來自于那里了,總之稍加修改后非常有效果,大家就這么用了,感謝最初開源的那位兄弟。這里,主要是做個記錄,為以后類似擴展(譬如Memcached)做個準備。

iBatis提供CacheController接口,用于實現第三方緩存架構的擴展。
這里以iBatis 2.3.0,EhCache 1.2.3版本為基礎,構建iBatis+EhCache實現。

EhCacheController類:

package com.ibatis.sqlmap.engine.cache.ehcache;import java.net.URL; import java.util.Properties;import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element;import com.ibatis.sqlmap.engine.cache.CacheController; import com.ibatis.sqlmap.engine.cache.CacheModel;/*** EhCache Implementation of the* {@link com.ibatis.sqlmap.engine.cache.CacheController} interface to be able* to use EhCache as a cache implementation in iBatis. You can configure your* cache model as follows, by example, in your sqlMapping files:* * <pre>* <code>* <cacheModel id="myCache" readOnly="true" serialize="false"* type="com.ibatis.sqlmap.engine.cache.EhCacheController" > * <property name="configLocation"* value="/path-to-ehcache.xml"/> * </cacheModel> </code>* </pre>* * Alternatively, you can use a type alias in your type attribute and defining* the class with a <code><typeAlias></code> declaration:* * <pre>* <code>* <sqlMapConfig>* <typeAlias alias="EHCACHE" * type="com.ibatis.sqlmap.engine.cache.ehcache.EhCacheController" />* </sqlMapConfig>* </code>* </pre>* */ public class EhCacheController implements CacheController {/*** The EhCache CacheManager.*/private CacheManager cacheManager;public static final String CONFIG_LOCATION = "configLocation";/*** Default Configure Location*/public static final String DEFAULT_CONFIG_LOCATION = "/ehcache.xml";/*** Flush a cache model.* * @param cacheModel* - the model to flush.*/public void flush(CacheModel cacheModel) {getCache(cacheModel).removeAll();}/*** Get an object from a cache model.* * @param cacheModel* - the model.* @param key* - the key to the object.* @return the object if in the cache, or null(?).*/public Object getObject(CacheModel cacheModel, Object key) {Object result = null;Element element = getCache(cacheModel).get(key);if (element != null) {result = element.getObjectValue();}return result;}/*** Put an object into a cache model.* * @param cacheModel* - the model to add the object to.* @param key* - the key to the object.* @param object* - the object to add.*/public void putObject(CacheModel cacheModel, Object key, Object object) {getCache(cacheModel).put(new Element(key, object));}/*** Remove an object from a cache model.* * @param cacheModel* - the model to remove the object from.* @param key* - the key to the object.* @return the removed object(?).*/public Object removeObject(CacheModel cacheModel, Object key) {Object result = this.getObject(cacheModel, key);getCache(cacheModel).remove(key);return result;}/*** Gets an EH Cache based on an iBatis cache Model.* * @param cacheModel* - the cache model.* @return the EH Cache.*/private Cache getCache(CacheModel cacheModel) {String cacheName = cacheModel.getId();Cache cache = cacheManager.getCache(cacheName);return cache;}/*** Shut down the EH Cache CacheManager.*/public void finalize() {if (cacheManager != null) {cacheManager.shutdown();}}/*** Configure a cache controller. Initialize the EH Cache Manager as a* singleton.* * @param props* - the properties object continaing configuration information.*/@Overridepublic void configure(Properties props) {String configLocation = props.getProperty(CONFIG_LOCATION);// if can not found ehcache.xml from configLocaion,// use default configure file.if (configLocation == null) {configLocation = DEFAULT_CONFIG_LOCATION;}URL url = getClass().getResource(configLocation);cacheManager = CacheManager.create(url);} }

?這里默認在根目錄下獲取ehcache.xml文件,可以通過cacheModel配置進行修改。

在SqlMapConfig.xml文件中配置一個別名,作為全局變量,供其余SqlMap使用

<typeAliasalias="EHCACHE"type="com.ibatis.sqlmap.engine.cache.ehcache.EhCacheController" />

?
順便提一句,要使用緩存注意SqlMapConfig.xml文件中settings節點配置cacheModelsEnabledtrue

<settingscacheModelsEnabled="true"useStatementNamespaces="true" ... />

?如果要變更ehcache.xml文件路徑為/config/ehcache.xml,可以在上述節點中下入如下代碼:

<property name="configLocation" value="/config/ehcache.xml" />

?然后,就可以通過ehcache.xml控制ehcache緩存了!

舉例說明iBatis SqlMap & ehcahce.xml,以免有些兄弟混淆!

以Account類為示例,在SqlMap中的配置為:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapPUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN""http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMapnamespace="Account"><cacheModelid="cache"type="EHCACHE"><flushIntervalhours="1" /><!-- flush操作,需要指明 Namespace --><flushOnExecutestatement="Account.create" /></cacheModel><typeAliasalias="account"type="org.zlex.acl.Account" /><resultMapid="accountMap"class="account"><resultproperty="accountId"column="accountId" /><resultproperty="accountName"column="accountName" /><resultproperty="mail"column="mail" /><resultproperty="realName"column="realName" /><resultproperty="status"column="status" /><resultproperty="lastLoginTime"column="lastLoginTime" /></resultMap><selectid="readByAccountName"parameterClass="string"resultMap="accountMap"cacheModel="cache"><![CDATA[SELECT accountId,accountName,mail,realName,status,lastLoginTimeFROM acl_accountWHERE accountName = #accountName# ]]></select><insertid="create"parameterClass="account"><![CDATA[INSERT INTO acl_account(accountName,mail,realName,status,lastLoginTime) VALUES (#accountName#,#mail#,#realName#,#status#,#lastLoginTime#)]]><selectKeyresultClass="long"keyProperty="accountId"><![CDATA[select LAST_INSERT_ID() as id ]]></selectKey></insert> </sqlMap>

?注意:

引用 <select id="readByAccountName" parameterClass="string" resultMap="accountMap" cacheModel="cache">

這里的cacheModel="cache",對應的在ehcache.xml中就應該是:

<cache name="Account.cache" maxElementsInMemory="10000" eternal="false"maxElementsOnDisk="1000" overflowToDisk="true" timeToIdleSeconds="300"/>

?因為,我使用了useStatementNamespaces="true"

?

轉載于:https://www.cnblogs.com/wcyBlog/p/3949697.html

總結

以上是生活随笔為你收集整理的缓存初解(四)---Ibatis的缓存配置+Ehcache的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。