zoukankan      html  css  js  c++  java
  • Memcachedclientutils类

    0.个人标签

    我的CSDN博客地址: http://blog.csdn.net/caicongyang 

    1.依赖的jar包

    commons-pool-1.5.6.jar

    slf4j-api-1.6.1.jar

    java_memcached-release_2.6.6.jar

    slf4j-simple-1.6.1.jar

    2.DangaMemCacheClient.java

    package com.ccy.comm.utils;
    
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.locks.ReentrantLock;
    
    import com.danga.MemCached.MemCachedClient;
    import com.danga.MemCached.SockIOPool;
    
    /**
     * <p> 
     * Title: DangaMemCacheClient.java 
     * Package  
     * </p>
     * <p>
     * Description: MemCacheClient Utils类 
     * <p>
     * @author Tom.Cai
     * @created 2015-7-16 下午3:49:24 
     * @version V1.0 
     *
     */
    public class DangaMemCacheClient {
    	private static DangaMemCacheClient instance = null;
    	private static ReentrantLock lock = new ReentrantLock();
    	private MemCachedClient mc;
    	private static int expTime = 3600 * 24 * 30;
    	private static String address="192.168.253.132";
    	private static String port="12000";
    
    	public static DangaMemCacheClient getInstance() {
    		if (instance == null) {
    			lock.lock();
    			try {
    				if (instance == null) {
    					instance = new DangaMemCacheClient();
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			} finally {
    				lock.unlock();
    			}
    		}
    		return instance;
    	}
    
    	private DangaMemCacheClient() {
    		try {
    			mc = new MemCachedClient();
    			String[] servers = { address+":"+port };
    			Integer[] weights = { 10 };
    
    			SockIOPool pool = SockIOPool.getInstance();
    
    			pool.setServers(servers);
    			pool.setWeights(weights);
    
    			pool.setInitConn(5);
    			pool.setMinConn(5);
    			pool.setMaxConn(250);
    			pool.setMaxIdle(expTime);
    
    			pool.setMaintSleep(30);
    
    			pool.setNagle(false);
    			pool.setSocketTO(30);
    			pool.setSocketConnectTO(0);
    
    			pool.initialize();
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public Object getObject(String key) {
    		return mc.get(key);
    	}
    
    	public boolean setObject(String key, Object value) {
    		return mc.set(key, value);
    	}
    
    	public boolean setObject(String key, Object value, long time) {
    		return mc.set(key, value, new Date(time));
    	}
    
    	public boolean removeObject(String key) {
    		return mc.delete(key);
    	}
    	
    	/**
    	 * 循环获取全部Memcache中的全部key
    	 * @return
    	 */
    	public List<String> getKeys() {
    		List<String> keyList = new ArrayList<String>();
    		Map slabs = mc.statsItems();
    		Iterator itemsItr = slabs.keySet().iterator();
    		while (itemsItr.hasNext()) {
    			String serverInfo1 = itemsItr.next().toString();
    			// 取得這個server的各種 status [itemname:number:field=value]
    			Map itemNames = (Map) slabs.get(serverInfo1);
    			Iterator itemNameItr = itemNames.keySet().iterator();
    			// 以status key值去迴圈
    			while (itemNameItr.hasNext()) {
    				String itemName = itemNameItr.next().toString();
    				// 拆解status 欄位
    				// itemAtt[0] = itemname
    				// itemAtt[1] = CacheDump的参數
    				// itemAtt[2] = field:number or age
    				String[] itemAtt = itemName.split(":");
    				if (itemAtt[2].startsWith("number")) {
    					Map chcheDump = mc.statsCacheDump(Integer.parseInt(itemAtt[1]), 0);
    					Iterator itr = chcheDump.keySet().iterator();
    					int i = 0;
    					while (itr.hasNext()) {
    						String serverInfo2 = itr.next().toString();
    						Map items = (Map) chcheDump.get(serverInfo2);
    						Iterator keyItr = items.keySet().iterator();
    						while (keyItr.hasNext()) {
    							String key = keyItr.next().toString();
    							String memKey = key;
    							i++;
    							try {
    								key = URLDecoder.decode(key, "UTF-8");
    								String value = ((String) items.get(memKey));
    								value = value.substring(value.indexOf(";") + 2, value.indexOf(" s"));
    								Date date = new Date();
    								date.setTime(Long.valueOf(value + "000"));
    								keyList.add(key);
    							} catch (Exception ex) {
    							}
    						}
    						
    					}
    				}
    			}
    		}
    		return keyList;
    	}
    
    	public static void main(String[] args) throws UnsupportedEncodingException {
    		DangaMemCacheClient dmcc = DangaMemCacheClient.getInstance();
    		dmcc.setObject("ccy0", "test0");
    		dmcc.setObject("ccy1", "test1");
    		System.out.println((String)dmcc.getObject("ccy0"));
    		System.out.println((String)dmcc.getObject("ccy1"));
    
    		List<String> list = dmcc.getKeys();
    		for(String key : list){
    			System.out.println(key);
    		}
    		
    	}
    }
    

    3.说明

    本人将其写成一个工具来适用于仅仅缓存不存数据库的情况,当然能够写一个service在供spring来调用!



    我的CSDN博客地址: http://blog.csdn.net/caicongyang 



  • 相关阅读:
    Android:Unable to find explicit activity class报错
    家庭记账本2
    家庭记账本开发
    Android Studio中TableLayout(表格布局)
    Android Studio中View与ViewGroup的概念
    Android Studio反编译APK获取代码&资源
    Android程序签名打包
    Android Studio工程相关解析(各种文件,资源访问)
    Android Studio 的SDK更新不了问题解决
    模块
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7269450.html
Copyright © 2011-2022 走看看