`
elicer
  • 浏览: 131569 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Websphere Dynamic Cache 跟 Ibatis 集成

阅读更多
要集成Dynamic cache 跟 ibatis的cache 主要就是要写CacheController
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();
        }   
    }   
}  

使用的配置文件如下配置你自己的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是不能序列化的。 所以一般在使用时要权衡一下利弊。

<?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>
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics