Memcached缓存集群_创建多实例
1.首先在在服务端增加两个缓存实例,如192.168.120.101:11214和192.168.120.102:11215,空间分配可按实际需求定。
2.JAVA客户端导入
下载地址:http://files.cnblogs.com/files/zhougaojun/MemClient.zip
3.缓存集群配置:memcached11212.xml
<?xml version="1.0" encoding="UTF-8"?> <memcached> <client name="mclient0" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool0"> <errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler> </client> <client name="mclient1" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool1"> <errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler> </client> <socketpool name="pool0" failover="true" initConn="5" minConn="5" maxConn="5000" maintSleep="0" nagle="false" socketTO="36000" aliveCheck="true"> <servers>192.168.120.101:11214</servers> </socketpool> <socketpool name="pool1" failover="true" initConn="5" minConn="5" maxConn="5000" maintSleep="0" nagle="false" socketTO="36000" aliveCheck="true"> <servers>192.168.120.102:11215</servers> </socketpool> <cluster name="cluster1" mode="active"> <!--mode = active,standby--> <memCachedClients>mclient0,mclient1</memCachedClients> </cluster> </memcached>
4.写工具类操作缓存
package com.zqgame.adreport.front.memcached; import java.util.Date; import com.alisoft.xplatform.asf.cache.IMemcachedCache; /** * 读取memcached11212.xml文件的工具类 * 版权拥有:深圳中青宝互动网络股份有限公司 <br> * ====================================== <br> * 修改记录 <br> * ====================================== <br> * 序号 姓名 日期 版本 简单描述 <br> * */ public class MemcachedUtil11212 { /** * 往Cache中添加动态key * @param name * @return */ public static boolean addDyncKeyToCache(String key, String keyValue, Date date){ boolean bln = false; if (keyValue != null && !"".equals(keyValue)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); bln = cache.add(key, keyValue, date); // logger.info("--add keyValue:" + keyValue + "-->" + bln); } return bln; } public static boolean addDyncKeyToCache(String key, Object keyValue, Date date){ boolean bln = false; if (keyValue != null && !"".equals(keyValue)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); bln = cache.add(key, keyValue, date); // logger.info("--add keyValue:" + keyValue + "-->" + bln); } return bln; } public static boolean addDyncKeyToCache(String key, Object keyValue, int milliseconds){ boolean bln = false; if (keyValue != null && !"".equals(keyValue)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); bln = cache.add(key, keyValue, new Date(milliseconds)); // logger.info("--add keyValue:" + keyValue + "-->" + bln); } return bln; } /** * 从Cache中取得动态key * @param name * @return */ public static String getStringDyncKeyFromCache(String key){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); Object obj = cache.get(key); if(obj != null){ return obj.toString(); } } return null; } /** * 从Cache中取得动态key * @param name * @return */ public static Object getDyncKeyFromCache(String key){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); Object obj = cache.get(key); if(obj != null){ return obj; } } return null; } /** * 判断cache中是否含有key * @param name * @return */ public static boolean containsKey(String key){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.containsKey(key); } return false; } /** * 不存在则增加,存在则更新 * * @param key * @param value * @return * @author:xuhaidui * @date:2012-4-28 */ public static boolean addOrReplace(String key, Object value){ if (key != null && !"".equals(key)){ if(containsKey(key)){ replace(key,value); }else{ addCache(key,value); } } return false; } /** * 更新值 * @param key * @param value * @return */ public static boolean replace(String key, String value){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.replace(key, value); } return false; } /** * 更新值 * @param key * @param value * @return */ public static boolean replace(String key, Object value){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.replace(key, value); } return false; } /** * 更新值 * @param key * @param value * @return */ public static boolean replace(String key, String value, Date date){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.replace(key, value, date); } return false; } /** * 更新值 * @param key * @param value * @return */ public static boolean replace(String key, Object value, Date date){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.replace(key, value, date); } return false; } /** * 更新值 * @param key * @param value * @return */ public static boolean replace(String key, Object value, int milliseconds){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.replace(key, value, new Date(milliseconds)); } return false; } public static boolean addCache(String key, String value){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.add(key, value); } return false; } public static boolean addCache(String key, Object value){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); return cache.add(key, value); } return false; } /** * 删除 * @param key * @return Object */ public static Object remove(String key){ if (key != null && !"".equals(key)){ IMemcachedCache cache = InitCacheManager11212.getInstance(); if(cache.containsKey(key)) { Object object = cache.remove(key); // logger.info("--remove key:" + key) ; return object; } } return null; } }
package com.zqgame.adreport.front.memcached; import com.alisoft.xplatform.asf.cache.ICacheManager; import com.alisoft.xplatform.asf.cache.IMemcachedCache; import com.alisoft.xplatform.asf.cache.memcached.CacheUtil; import com.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager; public class InitCacheManager11212 { private static ICacheManager<IMemcachedCache> manager11212; static { manager11212 = CacheUtil.getCacheManager(IMemcachedCache.class, MemcachedCacheManager.class.getName()); manager11212.setConfigFile("memcached11212.xml"); manager11212.setResponseStatInterval(5*1000); manager11212.start(); } public static IMemcachedCache getInstance(){ return manager11212.getCache("mclient0"); } }
5.使用方法:
MemcachedUtil11212.addCache(cacheKey,list);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。