让ibatis 支持 memcached

阅读更多
ibatis 不支持Memcached ,只支持第三方的oscache.
所以看了一下缓存模块源码,觉得的很好扩展,于是就实现了一下,不知是否合理还请大家扳砖

SqlMapConfiguration 中新建别名

registerDefaultTypeAliases方法中添加

typeHandlerFactory.putTypeAlias("MEMCACHED", MemcachedCacheController.class.getName());



public interface OtherCacheHelper {
         //初始化第三方缓存
	public void initCache() ;
         //获取第三方缓存句柄
           //client分布式中相关主机
	public T getCacheHandler(String client) ;
}



采用xmemcached连接memcached server
public class MemcacheCacheHelper implements OtherCacheHelper {

	private static XMemcachedClient xmc;

	public MemcacheCacheHelper() {
		initCache();
	}

	@Override
	public XMemcachedClient getCacheHandler(String client) {
		// TODO Auto-generated method stub
		return xmc;
	}

	@Override
	public void initCache() {
		// TODO Auto-generated method stub
		try {
			xmc = new XMemcachedClient("192.168.1.105", 11212);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


memcached管理类
public class MemcachedCacheController implements CacheController {

	private XMemcachedClient cacheHandler = null;

	//keyList 管理key
         private LinkedList keyList = null;

	private int cacheSize;

	public MemcachedCacheController() {
		cacheSize = 100;
		keyList = new LinkedList();
	}


	@Override
	public void flush(CacheModel cacheModel) {
		// TODO Auto-generated method stub
		try {
			for (String key : keyList) {
				cacheHandler.delete(key);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		keyList.clear();
	}

	@Override
	public Object getObject(CacheModel cacheModel, Object key) {
		// TODO Auto-generated method stub
		String ckey = getKey(cacheModel, key);

		
		try {
			return cacheHandler.get(ckey);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void putObject(CacheModel cacheModel, Object key, Object object) {
		String ckey = getKey(cacheModel, key);
		// TODO Auto-generated method stub
		keyList.addLast(ckey);
		
		try {
			cacheHandler.add(ckey,0,object);
			if (keyList.size() > cacheSize) {
				String first = keyList.removeFirst();
				cacheHandler.delete(first);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public Object removeObject(CacheModel cacheModel, Object key) {
		// TODO Auto-generated method stub
		String ckey = getKey(cacheModel, key);
		try {
			if (keyList.contains(ckey)) {
				return cacheHandler.delete(ckey);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public void setProperties(Properties props) {
		// TODO Auto-generated method stub
		String size = props.getProperty("cache-size");
		if (size == null) {
			size = props.getProperty("size");
		}
		if (size != null) {
			cacheSize = Integer.parseInt(size);
		}
		//获取缓存帮助类
                  String cacheHelpObject = props.getProperty("cache-helper");
		
                  String cacheclient = props.getProperty("cache-client");
		if (cacheHelpObject != null) {
			try {
				Class clazz = Resources.classForName(cacheHelpObject);
				Object obj = Resources.instantiate(clazz);
				if (obj instanceof MemcacheCacheHelper) {
					cacheHandler = ((MemcacheCacheHelper) obj)
							.getCacheHandler(cacheclient);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

//因为我认为ibaits 的缓存key 太长了,所以采用再hash的方法,再加上的缓存ID
	private String getKey(CacheModel cacheModel, Object cacheKey) {
		String key = cacheKey.toString();
		int keyhash = key.hashCode();
		String cacheId = cacheModel.getId();
		return cacheId + "_" + keyhash;
	}

}


对应实体XML设置

    
    
    
    
  





你可能感兴趣的:(iBATIS,memcached,SQL,SQL,Server,Spring)