Memcached緩存集羣_建立多實例

1.首先在在服務端增長兩個緩存實例,如192.168.120.101:11214和192.168.120.102:11215,空間分配可按實際需求定。java

2.JAVA客戶端導入緩存

下載地址:http://files.cnblogs.com/files/zhougaojun/MemClient.zip網絡

3.緩存集羣配置:memcached11212.xmlsocket

<?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.寫工具類操做緩存ide

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.使用方法:memcached

MemcachedUtil11212.addCache(cacheKey,list);工具

相關文章
相關標籤/搜索