192.168.0.1:11111, 192.168.0.2:11111 Unknown end tag for
然后配置sqlMapConfig.xml文件:
"http://ibatis-with-memcached.googlecode.com/files/sql-map-config-2.dtd"> #注意这里,不用官方的,这个dtd文件加了个新属性databaseUrl,区分不同数据库的缓存对象
ibatis的xml文件:Albums.xml #创建缓存model #加入缓存 #删除对象,删除缓存 #可以根据主键进行缓存,可以设置为空,不能不设 #可以根据组(比如用户id)进行缓存,更加细粒度化,可以设置为空,不能不设 Unknown end tag for delete from albums where id=1 and accId=1 #(加上accId可以删除分组缓存)
package com.ibatis.sqlmap.engine.cache.memcached; import java.io.Serializable; import java.util.Collection; import java.util.Date; import java.util.Map; import org.apache.log4j.Logger; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemcachedManager { private static Logger logger = Logger.getLogger(MemcachedManager.class); private static String memcachedDomain = "IBATIS_CACHED"; // memcached 域名 private String serverlist; private static MemCachedClient mcc = null; private SockIOPool pool = null; private int initConn = 5; private int minConn = 5; private int maxConn = 50; public void init() { if (mcc != null) return; logger.info("Initializing ibatis memcached start."); if (pool == null) { try { pool = SockIOPool.getInstance(memcachedDomain); pool.setServers(serverlist.split(",")); if (!pool.isInitialized()) { pool.setInitConn(initConn); pool.setMinConn(minConn); pool.setMaxConn(maxConn); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(60 * 60); pool.setSocketConnectTO(0); pool.initialize(); } } catch (Exception ex) { logger.error(ex.getMessage()); } } if (mcc == null) { mcc = new MemCachedClient(memcachedDomain); mcc.setCompressEnable(false); mcc.setCompressThreshold(0); } logger.info("Initializing youxigu Memcached ok!"); } public void closePool() { pool.shutDown(); mcc = null; pool = null; logger.info("Ibatis memcached pool closed"); } public static boolean set(Object key, Serializable obj) { logger.debug("set key:" + getKey(key) + ", value:" + obj); try { return mcc.set(getKey(key), obj); } catch (Exception e) { logger.error("Pool set error!"); e.printStackTrace(); } return false; } public static boolean set(Object key, Serializable obj, long cacheTime) { try { return mcc.set(getKey(key), obj, new Date(cacheTime)); } catch (Exception e) { logger.error("Pool set error!"); e.printStackTrace(); } return false; } public static void replace(int key, Serializable value, long cacheTime) { try { mcc.replace(getKey(key), value, new Date(cacheTime)); } catch (Exception e) { logger.error(" pool set error!"); } } public static Object get(Object key) { Object result = null; String realkey = getKey(key); try { result = mcc.get(realkey); } catch (Exception e) { e.printStackTrace(); } logger.debug("get key:" + getKey(key) + ", value:" + result); return result; } public static void setCounter(Object key, long count) { try { mcc.storeCounter(getCountKey(key), count); } catch (Exception e) { logger.error("Pool setCounter error!"); } } public static void addCounter(Object key) { if(mcc.get(getCountKey(key))==null){ mcc.storeCounter(getCountKey(key), 0); } try { mcc.incr(getCountKey(key)); } catch (Exception e) { logger.error("Pool setCounter error!"); } } public static void decreaseCounter(Object key) { try { mcc.decr(getCountKey(key)); } catch (Exception e) { logger.error("Pool setCounter error!"); } } public static void addCounter(Object key, long addValue) { try { mcc.incr(getCountKey(key), addValue); } catch (Exception e) { logger.error(" pool setCounter error!"); } } public static long getCounter(Object key) { long result = 0; try { result = mcc.getCounter(getCountKey(key)); } catch (Exception e) { logger.error(e.getMessage()); } return result; } public static boolean delete(Object key) { try { return mcc.delete(getKey(key)); } catch (Exception e) { logger.error(e.getMessage()); } return false; } public static long deleteCounter(Object key) { try { return mcc.decr(getCountKey(key)); } catch (Exception e) { logger.error(" pool setCounter error!"); } return 0; } public static void flushAll() { mcc.flushAll(); } @SuppressWarnings("unchecked") public static long size(){ long size=0L; Map> status=mcc.statsItems(); Collection