天天看點

Memcached 在 Java中的使用

1、MemcacheConfig 配置檔案,存放于class目錄裡面,使用者生産環境:
           
#memcache is use
isUse=0
#memcache
server1=127.0.0.1:12000
#memfix
memfix_flow=joinsoft_css_flow
memfix_bill=joinsoft_css_bill
memfix_billpagedm=joinsoft_css_pagedm

memfix_cj=joinsoft_cj


BILLFORM_FLOW_TIME=43200
BILLFORM_FLOW_BILL_TIME=43200
BILLPAGEDM_TIME=43200
CJ_TIME=43200
           
2、讀取properties檔案的工具類:
           
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * 讀取properties檔案的工具類
 * @author admin
 *
 */
public class PropertyUtil {
	private static Map<String, PropertyUtil> instance = Collections.synchronizedMap(new HashMap<String, PropertyUtil>());
	protected String sourceUrl;
	protected ResourceBundle resourceBundle;
	private static Map<String, String> convert = Collections.synchronizedMap(new HashMap<String, String>());

	protected PropertyUtil(String sourceUrl) {
		this.sourceUrl = sourceUrl;
		load();
	}

	/**
	 * 擷取執行個體
	 * @param sourceUrl 檔案通常在WEB-INF\classes目錄下,以*.properties命名,使用此方法時隻要傳檔案名(不含擴充名)
	 * @return
	 */
	public static PropertyUtil getInstance(String sourceUrl) {
		synchronized (PropertyUtil.class) {
			PropertyUtil manager = (PropertyUtil) instance.get(sourceUrl);
			if (manager == null) {
				manager = new PropertyUtil(sourceUrl);
				instance.put(sourceUrl, manager);
			}
			return manager;
		}
	}

	private synchronized void load() {
		try {
			this.resourceBundle = ResourceBundle.getBundle(this.sourceUrl);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 擷取某一個key值 
	 * @param key
	 * @return
	 */
	public String getProperty(String key) {
		if(this.resourceBundle == null) return null;
		if(key == null || key.trim().equals("")) return null;
		
		try {
			return new String(this.resourceBundle.getString(key).getBytes("iso-8859-1"), "utf-8");
		} 
		catch (Exception e) {
			e.printStackTrace();
			try{
				return this.resourceBundle.getString(key);
			}
			catch(Exception e2){
				e2.printStackTrace();
				return null;
			}
		}
	}

	/**
	 * 全部健值轉化到map
	 * @return
	 */
	public Map<String, String> readyConvert() {
		if(this.resourceBundle == null) return null;
		Enumeration<String> enu = this.resourceBundle.getKeys();
		while (enu.hasMoreElements()) {
			String key = (String) enu.nextElement();
			String value = this.resourceBundle.getString(key);
			convert.put(value, key);
		}
		return convert;
	}

	/**
	 * 
	 * @param resourcebundle
	 * @return
	 */
	public Map<String, String> readyConvert(ResourceBundle resourcebundle) {
		if(resourcebundle == null) return null;
		Enumeration<String> enu = resourcebundle.getKeys();
		while (enu.hasMoreElements()) {
			String key = (String) enu.nextElement();
			String value = resourcebundle.getString(key);
			convert.put(value, key);
		}
		return convert;
	}

}
           

3、MemCache 對象

import java.util.Map;
import java.util.Set;


public class MemCache {
    private static MemCacheService memCacheService = null;

    private static MemCache memCache = new MemCache();

    public static MemCache getInstance() {
        return memCache;
    }

    public MemCache() {
        memCacheService = MemCacheServiceImpl.getInstance();
    }

    public Object get(String key) {
        try {
            if(memCacheService != null) {
                return memCacheService.get(key);
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return null;
    }

    public boolean set(String key, Object value) {
        try {
            if(memCacheService != null) {
                return memCacheService.set(key, value);
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return false;
    }

    public Map<String, Object> getBulk(Set<String> keys) {
        try {
            if(memCacheService != null) {
                return memCacheService.getBulk(keys);
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return null;
    }

    public boolean remove(String key) {
        try {
            if(memCacheService != null) {
                return memCacheService.remove(key);
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return false;
    }

    /**
     * 
     * @param key
     * @param value
     * @param exp 緩存時長使用second為機關
     * @return
     */
    public boolean set(String key, Object value, int exp) {
        try {
            if(memCacheService != null) {
                return memCacheService.set(key, value, exp);
            }
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return false;
    }
}
           

4、MemCacheServiceImpl 方法

import net.spy.memcached.AddrUtil;
import net.spy.memcached.DefaultConnectionFactory;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.OperationFactory;
import net.spy.memcached.internal.BulkFuture;
import net.spy.memcached.internal.GetFuture;
import net.spy.memcached.internal.OperationFuture;
import net.spy.memcached.transcoders.SerializingTranscoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.joinsoft.memcached.PropertyUtil;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


public class MemCacheServiceImpl implements MemCacheService{
    public static final String CACHE_PROP_FILE = "memcachedconfig";
    public static final String ENCODING = "UTF-8";

    private static Log log = LogFactory.getLog(MemCacheServiceImpl.class);
    private static ConcurrentHashMap<String, MemCacheService> flyweights = new ConcurrentHashMap<String, MemCacheService>();
    private MemcachedClient mc1 = null;
    private MemcachedClient mc2 = null;
    public static final int DEFAULT_MEMCACHED_TIMEOUT = 1;
    private int opTimeout = 1;
    public static final int DEFAULT_MEMCACHED_TIMEOUT_BATCH = 3;
    private int opTimeoutBulk = 3;
    public static final int DEFAULT_READBUF_SIZE = 16384;
    private int readBufSize = 16384;
    public static final int DEFAULT_OPQ_SIZE = 16384;
    private int opQueueLens = 3;
    public static final int DEFAULT_MEMCACHED_EXP_HOURS = 24;
    private int expHour = 24;
    public static final int DEFAULT_MEMCACHED_RETRY = 3;
    private int retry = 3;

    public static MemCacheService getInstance(String prop_file) {
        if(!flyweights.containsKey(prop_file)) {
            synchronized(prop_file) {
                flyweights.put(prop_file, new MemCacheServiceImpl(prop_file));
            }
        }

        return flyweights.get(prop_file);
    }

    public MemCacheServiceImpl() {
        String prop_file = CACHE_PROP_FILE;
        if(!flyweights.containsKey(prop_file)) {
            synchronized(prop_file) {
                flyweights.put(prop_file, new MemCacheServiceImpl(prop_file));
            }
        }

    }

    /**
     * 
     * @return
     */
    public static MemCacheService getInstance() {
    	PropertyUtil propertyUtil = PropertyUtil.getInstance(CACHE_PROP_FILE);
    	String isUse = propertyUtil.getProperty("isUse");//是否使用緩存,為1表使用,為0表不使用
    	
        return "1".equalsIgnoreCase(isUse) ? getInstance(CACHE_PROP_FILE) : null;
    }

    private MemCacheServiceImpl(String prop_file) {
    	PropertyUtil propertyUtil = PropertyUtil.getInstance(prop_file);
        String server1 = propertyUtil.getProperty("server1");
        String server2 = propertyUtil.getProperty("server1");
        try {
            this.opTimeout = 3;
            this.opTimeoutBulk = 3;
            this.retry = 1;
            this.readBufSize = 16384;
            this.opQueueLens = 16384;
            this.expHour = 24;
        } catch (Exception var7) {
            log.error("loading properties fail, use default config!");
        }

        try {
            this.mc1 = new MemcachedClient(new DefaultConnectionFactory() {
                public long getOperationTimeout() {
                    return (long)(MemCacheServiceImpl.this.opTimeout * 1000);
                }

                public int getReadBufSize() {
                    return MemCacheServiceImpl.this.readBufSize;
                }

                public OperationFactory getOperationFactory() {
                    return super.getOperationFactory();
                }

                public int getOpQueueLen() {
                    return MemCacheServiceImpl.this.opQueueLens;
                }

                public boolean isDaemon() {
                    return true;
                }
            }, AddrUtil.getAddresses(server1));
            this.mc2 = new MemcachedClient(new DefaultConnectionFactory() {
                public long getOperationTimeout() {
                    return (long)(MemCacheServiceImpl.this.opTimeout * 1000);
                }

                public int getReadBufSize() {
                    return MemCacheServiceImpl.this.readBufSize;
                }

                public OperationFactory getOperationFactory() {
                    return super.getOperationFactory();
                }

                public int getOpQueueLen() {
                    return this.opQueueLen;
                }

                public boolean isDaemon() {
                    return true;
                }
            }, AddrUtil.getAddresses(server2));
        } catch (IOException var6) {
            log.error("DefaultConnectionFactory memcache error:", var6);
        }

        SerializingTranscoder x1 = (SerializingTranscoder)this.mc1.getTranscoder();
        x1.setCharset("UTF-8");
        SerializingTranscoder x2 = (SerializingTranscoder)this.mc2.getTranscoder();
        x2.setCharset("UTF-8");
    }

    public Object get(String key) {
        Object result = null;

        try {
            for(int ex = 0; ex < this.retry; ++ex) {
                result = this._get(key);
                if(result != null) {
                    break;
                }
            }

            if(result == null) {
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return result;
    }

    private Object _get(String key) {
        Object myObj = null;

        try {
            GetFuture e = this.mc1.asyncGet(key);

            try {
                myObj = e.get((long)this.opTimeout, TimeUnit.SECONDS);
            } catch (TimeoutException var9) {
                var9.printStackTrace();
                e.cancel(false);
            } catch (InterruptedException var10) {
                var10.printStackTrace();
                e.cancel(false);
            } catch (ExecutionException var11) {
                var11.printStackTrace();
                e.cancel(false);
            }

            if(myObj == null) {
                GetFuture f2 = this.mc2.asyncGet(key);

                try {
                    myObj = f2.get((long)this.opTimeout, TimeUnit.SECONDS);
                } catch (TimeoutException var6) {
                    var6.printStackTrace();
                    f2.cancel(false);
                } catch (InterruptedException var7) {
                    var7.printStackTrace();
                    f2.cancel(false);
                } catch (ExecutionException var8) {
                    var8.printStackTrace();
                    f2.cancel(false);
                }
            }
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        if(myObj != null) {
        }

        return myObj;
    }

    public Map<String, Object> getBulk(Set<String> keys) {
        log.info("[ACCESS]begin to get info from cache in bulk...");
        Map ret = null;

        try {
            BulkFuture i$ = this.mc1.asyncGetBulk(keys);

            try {
                ret = (Map)i$.get((long)this.opTimeoutBulk, TimeUnit.SECONDS);
            } catch (TimeoutException var9) {
                log.info("[FAIL]time out when getting objects from cache server1...");
                i$.cancel(false);
            } catch (InterruptedException var10) {
                log.info("[FAIL]thread been interrupted while waiting when getting object from cache server1...");
                i$.cancel(false);
            } catch (ExecutionException var11) {
                log.info("[FAIL]exception when getting object from cache server1...");
                i$.cancel(false);
            }

            if(ret == null) {
                BulkFuture key = this.mc2.asyncGetBulk(keys);

                try {
                    ret = (Map)key.get((long)this.opTimeoutBulk, TimeUnit.SECONDS);
                } catch (TimeoutException var6) {
                    log.info("[FAIL]time out when getting objects from cache server2...");
                    key.cancel(false);
                } catch (InterruptedException var7) {
                    log.info("[FAIL]thread been interrupted while waiting when getting object from cache server2...");
                    key.cancel(false);
                } catch (ExecutionException var8) {
                    log.info("[FAIL]exception when getting object from cache server2...");
                    key.cancel(false);
                }
            }
        } catch (Exception var12) {
            log.error("[ERROR]other exception when getting objects from fengchao cache...", var12);
        }

        if(ret != null) {
            Iterator i$1 = keys.iterator();

            while(i$1.hasNext()) {
                String key1 = (String)i$1.next();
                if(ret.get(key1) != null) {
                    log.info("[GET]SHOOTED\tKey=" + key1 + "\tValue=" + ret.get(key1).toString());
                }
            }
        }

        return ret;
    }

    public boolean set(String key, Object value) {
        boolean result = false;

        for(int i = 0; i < this.retry; ++i) {
            result = this._set(key, value);
            if(result) {
                break;
            }

            log.info("set info into cache failed begin to retry " + i);
        }

        if(!result) {
            log.error("[FAIL] completely failed when setting info into cache after " + this.retry + " times");
        }

        return result;
    }

    private boolean _set(String key, Object value) {
        boolean ret = false;
        OperationFuture f = this.mc1.set(key, this.expHour * 60 * 60, value);
        OperationFuture f2 = this.mc2.set(key, this.expHour * 60 * 60, value);

        try {
            boolean e = ((Boolean)f.get((long)this.opTimeout, TimeUnit.SECONDS)).booleanValue();
            boolean fs2 = ((Boolean)f2.get((long)this.opTimeout, TimeUnit.SECONDS)).booleanValue();
            ret = e || fs2;
            if(!e) {
                log.info("[FAIL]CACHE SET FAIL:server1 set failed: Key=" + key + "\tValue=" + value.toString());
            } else if(!fs2) {
                log.info("[FAIL]CACHE SET FAIL:server2 set failed: Key=" + key + "\tValue=" + value.toString());
            }
        } catch (TimeoutException var8) {
            log.info("[FAIL]time out when getting objects from cache server2...");
            f.cancel(false);
            f2.cancel(false);
        } catch (InterruptedException var9) {
            log.error("[ERROR]exception when setting fengchao cache - thread been interrupted...", var9);
            f.cancel(false);
            f2.cancel(false);
        } catch (ExecutionException var10) {
            log.error("[ERROR]exception when setting fengchao cache - exception when getting status...", var10);
            f.cancel(false);
            f2.cancel(false);
        } catch (Exception var11) {
            log.error("[ERROR]exception when setting fengchao cache - other exceptions...", var11);
            f.cancel(false);
            f2.cancel(false);
        }

        if(value != null) {
            log.info("MemCacheServiceImpl.set,key=" + key + ",value=" + value.getClass());
        } else {
            log.info("MemCacheServiceImpl.set,key=" + key + ",value=null");
        }

        return ret;
    }

    public boolean remove(String key) {
        boolean ret = false;
        OperationFuture f = this.mc1.delete(key);
        OperationFuture f2 = this.mc2.delete(key);

        try {
            ret = ((Boolean)f.get((long)this.opTimeout, TimeUnit.SECONDS)).booleanValue() && ((Boolean)f2.get((long)this.opTimeout, TimeUnit.SECONDS)).booleanValue();
        } catch (TimeoutException var6) {
            log.info("[FAIL]time out when getting objects from cache server2...");
            f.cancel(false);
            f2.cancel(false);
        } catch (InterruptedException var7) {
            log.error("[ERROR]exception when deleting fengchao cache - thread been interrupted...", var7);
            f.cancel(false);
            f2.cancel(false);
            ret = false;
        } catch (ExecutionException var8) {
            log.error("[ERROR]exception when deleting fengchao cache - exception when getting status...", var8);
            f.cancel(false);
            f2.cancel(false);
            ret = false;
        } catch (Exception var9) {
            log.error("[ERROR]exception when deleting fengchao cache - other exceptions...", var9);
            f.cancel(false);
            f2.cancel(false);
            ret = false;
        }

        log.info("[REMOVE]" + ret + "\tKey=" + key);
        return ret;
    }

    /**
     * @param key
     * @param value
     * @param exp
     */
    public boolean set(String key, Object value, int exp) {
        if(value == null) {
            return false;
        } else {
            boolean ret = false;
            OperationFuture f = this.mc1.set(key, exp, value);
            OperationFuture f2 = this.mc2.set(key, exp, value);

            try {
                boolean e = ((Boolean)f.get((long)this.opTimeout, TimeUnit.SECONDS)).booleanValue();
                boolean fs2 = ((Boolean)f2.get((long)this.opTimeout, TimeUnit.SECONDS)).booleanValue();
                ret = e || fs2;
                if(!e) {
                    log.info("[FAIL]CACHE SET FAIL:server1 set failed: Key=" + key + ",Value=" + value.toString());
                } else if(!fs2) {
                    log.info("[FAIL]CACHE SET FAIL:server2 set failed: Key=" + key + ",Value=" + value.toString());
                }
            } catch (Exception var9) {
                if(!"LOGIN_IP".equalsIgnoreCase(key)) {
                    log.info("MemCacheServiceImpl.set,key=" + key + ",value=" + value + ",Exception");
                }

                var9.printStackTrace();
                f.cancel(false);
                f2.cancel(false);
            }

            log.info("MemCacheServiceImpl.set,key=" + key + ",value=" + value.getClass());
            return ret;
        }
    }
}
           

5、MemCacheUtil 工具類

public class MemCacheUtil {
    private static MemCache memCache = MemCache.getInstance();



    public MemCacheUtil() {
    }


    /**
     * 擷取對象
     * @param key
     * @return
     */
    public static Object get(String key) {
        try {
            return memCache.get(key);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return null;
    }

    /**
     * 設定對象
     * @param key
     * @param value
     */
    public static void set(String key, Object value) {
        try {
            memCache.set(key,value);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }

    /**
     * 清除對象
     * @param key
     * @return
     */
    public static boolean remove(String key) {
        try {
            memCache.remove(key);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return false;
    }
    
    /**
     * 尚未實作
     * 根據緩存名稱以endWith為結尾的規則删除緩存
     * @param endWith
     * @return
     */
    public static int removeByEndWith(String endWith){
    	return 0;
    }
    
    /**
     * 尚未實作
     * 根據正規表達式規則删除緩存
     * @param regex
     * @return
     */
    public static int removeByRegex(String regex){
    	return 0;
    }

    /**
     * 未實作清除全部
     * @return
     */
    public static boolean removeAll() {
        try {

        } catch (Exception var1) {
            var1.printStackTrace();
        }

        return false;
    }

    /**
     * 
     * @param key
     * @param value
     * @param exp 緩存時長,使用second為機關
     */
    public static void set(String key, Object value, int exp) {
        try {
            memCache.set(key,value,exp);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }

}
           

6、依賴 Jar包

       spymemcached-2.10.3.jar

7、開啟 MemCache服務,如果服務沒開啟,啟動時會報錯。

memcached-win64-1.4.4-14.rar下載下傳位址:https://download.csdn.net/download/qq_33285292/11304407