Websphere Dynamic Cache 跟 Ibatis 集成

本文介绍如何通过实现CacheController来集成DynamicCache与iBatis缓存。具体包括自定义DynamicCacheController类的方法实现,如flush、getObject等,并详细说明了配置文件中的关键参数及其作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]要集成Dynamic cache 跟 ibatis的cache 主要就是要写CacheController[/b]
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ibatis.sqlmap.engine.cache.CacheController;
import com.ibatis.sqlmap.engine.cache.CacheModel;
import com.ibm.websphere.cache.DistributedMap;



public class DynamicCacheController implements CacheController {
//对应dynamic cache的 jndi
private String cacheInstance = "cacheInstance";



/**
* @return the cacheManager
*/
public DistributedMap getCacheManager() {
return cacheManager;
}

/**
* @param cacheManager the cacheManager to set
*/
public void setCacheManager(DistributedMap cacheManager) {
this.cacheManager = cacheManager;
}

/** The DynamicCache CacheManager. */
private DistributedMap cacheManager;

/**
* Flush a cache model.
* @param cacheModel - the model to flush.
*/
public void flush(CacheModel cacheModel) {
cacheManager.clear();
}

/**
* 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 = cacheManager.get(key);

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) {
cacheManager.put(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);
cacheManager.remove(key);
return result;
}

/**
* Configure a cache controller. Initialize the DynamicCache Manager as a singleton.
* @param props - the properties object continaing configuration information.
*/
public void setProperties(Properties props) {

try {
cacheManager = (DistributedMap) new InitialContext().lookup(cacheInstance);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



/**
* Shut down the DynamicCache CacheManager.
*/
public void finalize() {
if (cacheManager != null) {
cacheManager.clear();
}
}
}

[b]使用的配置文件如下[/b][b]配置你自己的CacheModel 指定type是我们自己写的cacheController,这里就是DynamicCacheController,在配置cachemodel时有两点注意的地方它有两个属性,一个是readonly,表示这个cacheInstance是不是只读的,还有一个属性是serialize,如果设为false 表示这是一个session level的cache,反之true表示这个一个application level的cache,每次都会返回一个instance,但是此时所有存入cache的东西都必须是serializable的,这就带来了一个问题了,如果设为true的话,我们就不能用lazy load了,因为lazy load的 proxy是不能序列化的。 所以一般在使用时要权衡一下利弊。[/b]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="TestCache">

<!-- Alias for the classes -->

<typeAlias alias="tableColumnCache" type="com.******.TableTestDTO" />
<typeAlias alias="MapCacheController" type="com.**********.DynamicCacheTestController"/>
<!-- readOnly表示这个Cache Instance是只读的,serialize=true表示这个cache 是application level的,负责只是session level的-->
<cacheModel id="tableTestCacheModel" type="MapCacheController" readOnly="true" serialize="false">
<flushInterval hours="24"/>
<property name="CacheSize" value="100"/>

</cacheModel>


<!-- cacheModel="product-cache" resultMap="productCache" -->
<statement id="loadTableColumnCache" cacheModel="tableTestCacheModel" resultMap="listTableColumnCache">

</statement>


</sqlMap>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值