本文复制其他播客,有好的技术文章希望各位大神能告知... 谢谢。

如何使用Java操作Memcached实例:

代码一:

package com.ghj.packageoftool;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
 * Memcached工具类
 * 
 * @author GaoHuanjie
 */
public class MemcachedUtils {

  private static MemCachedClient memCachedClient;
  static {
         /************************************配置Memcached**************************************/
         SockIOPool sockIOPool = SockIOPool.getInstance();

         sockIOPool.setServers(new String[]{"127.0.0.1:11211"});//设置memcached服务器地址
         sockIOPool.setWeights(new Integer[]{3}); 				//设置每个MemCached服务器权重 
         sockIOPool.setFailover(true);  						//当一个memcached服务器失效的时候是否去连接另一个memcached服务器.
         sockIOPool.setInitConn(10);    						//初始化时对每个服务器建立的连接数目
         sockIOPool.setMinConn(10);     						//每个服务器建立最小的连接数
         sockIOPool.setMaxConn(100);    						//每个服务器建立最大的连接数
         sockIOPool.setMaintSleep(30);  						//自查线程周期进行工作,其每次休眠时间
         sockIOPool.setNagle(false);    						//Socket的参数,如果是true在写数据时不缓冲,立即发送出去。Tcp的规则是在发送一个包之前,包的发送方会等待远程接收方确认已收到上一次发送过来的包;这个方法就可以关闭套接字的缓存——包准备立即发出。
         sockIOPool.setSocketTO(3000);  						//Socket阻塞读取数据的超时时间
         sockIOPool.setAliveCheck(true);						//设置是否检查memcached服务器是否失效
         sockIOPool.setMaxIdle(1000*30*30); 					// 设置最大处理时间 
         sockIOPool.setSocketConnectTO(0);  					//连接建立时对超时的控制

         sockIOPool.initialize();  								// 初始化连接池
         if (memCachedClient == null){
        	 memCachedClient = new MemCachedClient();
        	 memCachedClient.setPrimitiveAsString(true);		//是否将基本类型转换为String方法
         }
  }

  private MemcachedUtils() {
  }

  /**
   * 向缓存添加键值对。注意:如果键已经存在,则之前的键对应的值将被替换。
   * 
   * @author GaoHuanjie
   */
  public static boolean set(String key, Object value) {
    try {
      return memCachedClient.set(key, value);
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached set方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 向缓存添加键值对并为该键值对设定逾期时间(即多长时间后该键值对从Memcached内存缓存中删除,比如: new Date(1000*10),则表示十秒之后从Memcached内存缓存中删除)。注意:如果键已经存在,则之前的键对应的值将被替换。
   * 
   * @author GaoHuanjie
   */
  public static boolean set(String key, Object value, Date expire) {
    try {
      return memCachedClient.set(key, value, expire);
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached set方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 向缓存添加键值对。注意:仅当缓存中不存在键时,才会添加成功。
   * 
   * @author GaoHuanjie
   */
  public static boolean add(String key, Object value) {
    try {
      if (get(key) != null) {
        MemcachedLogUtils.writeLog("Memcached add方法报错,key值:" + key + "\r\n" + exceptionWrite(new Exception("Memcached内存缓存中已经存在该键值对")));
        return false;
      }else{
        return memCachedClient.add(key, value);
      }
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached add方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 向缓存添加键值对并为该键值对设定逾期时间(即多长时间后该键值对从Memcached内存缓存中删除,比如: new Date(1000*10),则表示十秒之后从Memcached内存缓存中删除)。注意:仅当缓存中不存在键时,才会添加成功。
   * 
   * @author GaoHuanjie
   */
  public static boolean add(String key, Object value, Date expire) {
    try {
      if (get(key) != null) {
        MemcachedLogUtils.writeLog("Memcached add方法报错,key值:" + key + "\r\n" + exceptionWrite(new Exception("Memcached内存缓存中已经存在该键值对")));
        return false;
      }else{
        return memCachedClient.add(key, value, expire);
      }
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached add方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 根据键来替换Memcached内存缓存中已有的对应的值。注意:只有该键存在时,才会替换键相应的值。
   * 
   * @author GaoHuanjie
   */
  public static boolean replace(String key, Object newValue) {
    try {
      return memCachedClient.replace(key, newValue);
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached replace方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 根据键来替换Memcached内存缓存中已有的对应的值并设置逾期时间(即多长时间后该键值对从Memcached内存缓存中删除,比如: new Date(1000*10),则表示十秒之后从Memcached内存缓存中删除)。注意:只有该键存在时,才会替换键相应的值。
   * 
   * @author GaoHuanjie
   */
  public static boolean replace(String key, Object newValue, Date expireDate) {
    try {
      return memCachedClient.replace(key, newValue, expireDate);
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached replace方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 根据键获取Memcached内存缓存管理系统中相应的值
   * 
   * @author GaoHuanjie
   */
  public static Object get(String key) {
    try {
      return memCachedClient.get(key);
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached get方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return null;
    }
  }

  /**
   * 根据键删除memcached中的键/值对
   * 
   * @author GaoHuanjie
   */
  public static boolean delete(String key) {
    try {
      return memCachedClient.delete(key);
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached delete方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 根据键和逾期时间(例如:new Date(1000*10):十秒后过期)删除 memcached中的键/值对
   * 
   * @author GaoHuanjie
   */
  public static boolean delete(String key, Date expireDate) {
    try {
      return memCachedClient.delete(key, expireDate);
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached delete方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 清理缓存中的所有键/值对
   * 
   * @author GaoHuanjie
   */
  public static boolean flashAll() {
    try {
      return memCachedClient.flushAll();
    } catch (Exception e) {
      MemcachedLogUtils.writeLog("Memcached flashAll方法报错\r\n" + exceptionWrite(e));
      return false;
    }
  }

  /**
   * 返回String类型的异常栈信息
   * 
   * @author GaoHuanjie
   */
  private static String exceptionWrite(Exception exception) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    exception.printStackTrace(printWriter);
    printWriter.flush();
    return stringWriter.toString();
  }

  /**
   * Memcached日志记录工具
   * 
   * @author GaoHuanjie
   */
  private static class MemcachedLogUtils {

    private static FileWriter fileWriter;
    private static BufferedWriter logWrite;
    private final static String PID = ManagementFactory.getRuntimeMXBean().getName();// 通过找到对应的JVM进程获取PID

    /**
     * 初始化Memcached日志写入流
     * 
   * @author GaoHuanjie
   */
static {
try {
  String osName = System.getProperty("os.name");
if (osName.contains("Windows")) {
  fileWriter = new FileWriter("D:\\memcached.log", true);
  } else {
  fileWriter = new FileWriter("/usr/local/logs/memcached.log", true);
  }
  logWrite = new BufferedWriter(fileWriter);
  } catch (IOException iOException) {
  iOException.printStackTrace();
try {
if (fileWriter != null) {
  fileWriter.close();
  }
if (logWrite != null) {
  logWrite.close();
  }
  } catch (Exception exception) {
  exception.printStackTrace();
  }
  }
  }
/**
   * 写入日志信息
   * 
   * @author GaoHuanjie
   */
public static void writeLog(String logContent) {
try {
  logWrite.write("[" + PID + "] " + "- [" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "]\r\n" + logContent);
  logWrite.newLine();
  logWrite.flush();
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  }
}

代码二:

package com.ghj.packageofclient;

import java.util.Date;

import junit.framework.TestCase;

import com.ghj.packageoftool.MemcachedUtils;

public class Client extends TestCase{

  /**
   * 测试MemcachedUtils类的set方法。
   * 
   * @author GaoHuanjie
   */
  public static void testSet1() {
    MemcachedUtils.set("set1Description", "调用MemcachedUtils类的set方法,没有设置键值对的存在时长");
    System.out.println(MemcachedUtils.get("set1Description").toString());
  }

  /**
   * 测试MemcachedUtils类的set方法。
   * 
   * @author GaoHuanjie
   */
  public static void testSet2() {
    MemcachedUtils.set("set2Description", "调用MemcachedUtils类的set方法,设置了键值对的存在时长——存在60秒", new Date(1000*60));
    System.out.println(MemcachedUtils.get("set2Description").toString());
  }

  /**
   * 测试MemcachedUtils类的add方法。
   * 
   * @author GaoHuanjie
   */
  public static void testAdd1() {
    MemcachedUtils.add("add1Description", "调用MemcachedUtils类的add方法,没有设置键值对的存在时长");
    System.out.println(MemcachedUtils.get("add1Description").toString());
  }

  /**
   * 测试MemcachedUtils类的add方法。
   * 
   * @author GaoHuanjie
   */
  public static void testAdd2() {
    MemcachedUtils.add("add2Description", "调用MemcachedUtils类的add方法,设置了键值对的存在时长——存在60秒", new Date(1000*60));
    System.out.println(MemcachedUtils.get("add2Description").toString());
  }

  /**
   * 测试MemcachedUtils类的replace方法。
   * 
   * @author GaoHuanjie
   */
  public static void testReplace1() {
    MemcachedUtils.add("replace1Description", "调用MemcachedUtils类的replace方法,没有设置键值对的存在时长");
    MemcachedUtils.replace("replace1Description", "值改变了!!!");
    System.out.println(MemcachedUtils.get("replace1Description").toString());
  }

  /**
   * 测试MemcachedUtils类的replace方法。
   * 
   * @author GaoHuanjie
   */
  public static void testReplace2() {
    MemcachedUtils.add("replace2Description", "调用MemcachedUtils类的replace方法,设置了键值对的存在时长——存在60秒", new Date(1000*60));
    MemcachedUtils.replace("replace2Description", "值改变了!!!", new Date(1000*60));
    System.out.println(MemcachedUtils.get("replace2Description").toString());
  }

  /**
   * 测试MemcachedUtils类的get方法。
   * 
   * @author GaoHuanjie
   */
  public static void testGet() {
    MemcachedUtils.add("getDescription", "调用MemcachedUtils类的get方法,没有设置键值对的存在时长");
    System.out.println(MemcachedUtils.get("getDescription").toString());
  }

  /**
   * 测试MemcachedUtils类的delete方法。
   * 
   * @author GaoHuanjie
   */
  public static void testDelete1() {
    MemcachedUtils.add("delete1Description", "调用MemcachedUtils类的delete方法,没有设置键值对的逾期时长");
    MemcachedUtils.delete("delete1Description");
    assertEquals(null, MemcachedUtils.get("delete1Description"));
  }

  /**
   * 测试MemcachedUtils类的delete方法。
   * 
   * @author GaoHuanjie
   */
  public static void testDelete2() {
    MemcachedUtils.set("delete2Description1", "调用MemcachedUtils类的delete方法,设置键值对的逾期时长", new Date(600*1000));
    MemcachedUtils.delete("delete2Description1", new Date(1000*600));
    assertEquals(null, MemcachedUtils.get("delete2Description1"));
  }

  /**
   * 测试MemcachedUtils类的flashAll方法。
   * 
   * @author GaoHuanjie
   */
  public static void testFlashAll() {
    MemcachedUtils.add("flashAllDescription", "调用MemcachedUtils类的delete方法,没有设置键值对的预期时长");
    MemcachedUtils.flashAll();
    assertEquals(null, MemcachedUtils.get("flashAllDescription"));
  }
}
 posted on 2016-04-14 17:00  _萨瓦迪卡  阅读(316)  评论(0编辑  收藏  举报