天天看点

Java MemCached 缓存实现

首先创建MemCached 缓存管理类,此代码测试需要添加 java_memcached-release_2.6.3.jar,commons-pool-1.5.6.jar,slf4j-api-1.6.1.jar,slf4j-simple-1.6.1.jar 这几个jar包

import java.util.Date;

import org.apache.commons.net.telnet.TelnetClient;

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

/**
 * 缓存管理类
 * 
 * @author  @ 2014-4-24
 */
public class MemCachedManager {

    /**
     * 记录日志
     */
    private static LoggerUtil log = LoggerUtil.getInstance(MemCachedManager.class);

    /**
     * 创建缓存客户端
     */
    public static MemCachedClient mcc = new MemCachedClient();

    /**
     * 缓存管理对象
     */
    private static MemCachedManager memCached = null;

    private static boolean isSuccess = true;
    // 设置与缓存服务器的连接池
    static {
        
        //判断缓存是否服务是否开启
        TelnetClient telnet = new TelnetClient();
        try {
            String[] sers="192.168.15.1:11211".split(":");
            telnet.connect(sers[0],Integer.parseInt(sers[1]));
        } catch (Exception e) {
            log.error("缓存初始化错误", e);
            isSuccess = false;
        }

        // 服务器列表和其权重
        String[] servers = {"192.168.15.1:11211"};
        Integer[] weights = {3};

        // 获取socke连接池的实例对象
        SockIOPool pool = SockIOPool.getInstance();

        // 设置服务器信息
        pool.setServers(servers);
        pool.setWeights(weights);

        // 设置初始连接数、最小和最大连接数以及最大处理时间
        pool.setInitConn(5);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaxIdle(1000 * 60 * 60 * 6);

        // 设置主线程的睡眠时间
        pool.setMaintSleep(30);

        // 设置TCP的参数,连接超时等
        pool.setNagle(false);
        pool.setSocketTO(1000);
        pool.setSocketConnectTO(0);

        // 初始化连接池
        pool.initialize();

    }

    /**
     * 私有构造方法,不允许实例化!
     * 
     */
    private MemCachedManager() {
    }

    /**
     * 获取唯一实例.
     * 
     * @return 缓存管理对象
     */
    public static MemCachedManager getInstance() {
        if (memCached == null) {
            memCached = new MemCachedManager();
        }

        return memCached;

    }

    /**
     * 添加一个指定的值到缓存中.
     * 
     * @param key
     *            主键
     * @param value
     *            值
     * @return 是否成功
     */
    public boolean add(String key, Object value) {
        if (isSuccess) {
            return mcc.add(key, value);
        } else {
            return false;
        }
    }

    /**
     * 添加一个指定的值到缓存中.
     * 
     * @param key
     *            主键
     * @param value
     *            值
     * @param expiry
     *            失效时间
     * @return 是否成功
     */
    public boolean add(String key, Object value, Date expiry) {
        if (isSuccess) {
            return mcc.add(key, value, expiry);
        } else {
            return false;
        }
    }

    /**
     * 替换缓存
     * 
     * @param key
     *            主键
     * @param value
     *            值
     * @return 是否成功
     */
    public boolean replace(String key, Object value) {
        if (isSuccess) {
            return mcc.replace(key, value);
        } else {
            return false;
        }
    }

    /**
     * 替换缓存
     * 
     * @param key
     *            主键
     * @param value
     *            值
     * @param expiry
     *            失效时间
     * @return 是否成功
     */
    public boolean replace(String key, Object value, Date expiry) {
        if (isSuccess) {
            return mcc.replace(key, value, expiry);
        } else {
            return false;
        }
    }

    /**
     * 根据指定的关键字获取对象.
     * 
     * @param key
     *            主键
     * @return 缓存对象的值
     */
    public Object get(String key) {
        if (isSuccess) {
            return mcc.get(key);
        } else {
            return null;
        }
    }
    
    /**删除缓存
     * @param key 主键
     * @return 是否删除成功
     */
    public boolean delete(String key)
    {
        if (isSuccess) {
            return mcc.delete(key);
        } else {
            return false;
        }
        
    }

   /**判断缓存是否存在
     * @param key 主键
     * @return 是否存在
     */    
    public boolean keyExists(String key) {
        if (isSuccess) {
            return mcc.keyExists(key);
        } else {
            return false;
        }
    }

}
           

  测试:

  

    MemCachedManager cache = MemCachedManager.getInstance();
        long startDate = System.currentTimeMillis();

              
        //cache.delete("test2");
      //设置失效时间15s, 默认为0,永不失效,此时间最大不能超过30天 
        //cache.add("test2", "ffff",new Date(15000));
        long endDate = System.currentTimeMillis();
        long nowDate = (endDate - startDate);
        System.out.println(nowDate);
        
        System.out.print(" get value : " + cache.get("test2"));      

转载于:https://www.cnblogs.com/Adhere/p/3781609.html