码迷,mamicode.com
首页 > 系统相关 > 详细

Memcached缓存集群_创建多实例

时间:2015-06-02 20:03:04      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

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>
memcached11212.xml

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;
    }
    
    
    
    
    
}
MemcachedUtil11212.java
技术分享
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");
    }
}
InitCacheManager11212.java

5.使用方法:

MemcachedUtil11212.addCache(cacheKey,list);

Memcached缓存集群_创建多实例

标签:

原文地址:http://www.cnblogs.com/zhougaojun/p/4547383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!