memcached客户端java连接代码

Java代码:

package com.itspace.thirdTechnology.memcached;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yun_lian on 2016/7/13.
 * 连接memcached服务端
 */
@Component
public class MemCacheServer {
    /**
     * 创建一个 memcached 客户端对象
     */
    private static MemCachedClient client = new MemCachedClient();
    /**
     * memcache服务端
     */
    private static MemCacheServer memCacheServer = new MemCacheServer();

    // 创建  memcached连接池
    static {
        System.err.println("~~~~~~~~~~~~~~~~~~~~~初始化内地测试缓存链接~~~~~~~~~~~~~~~~~~~~~~~");
//        String[] addr = {"192.168.199.149:11211"};// 指定memcached服务地址
//       String[] addr = {"192.168.199.251:11211"};// 指定memcached服务地址
        String[] addr = {"localhost:11211"};// 指定memcached服务地址
        Integer[] weights = {10};// 指定memcached服务器负载量
        SockIOPool pool = SockIOPool.getInstance();// 从连接池获取一个连接实例
        // 设置服务器和服务器负载量
        pool.setServers(addr);
        pool.setWeights(weights);
        // 设置一些基本的参数
        //设置初始连接数5   最小连接数 5   最大连接数 250
        //设置一个连接最大空闲时间6小时
        pool.setInitConn(5);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaxIdle(1000 * 30 * 30*6);
        // 设置主线程睡眠时间
        // 每隔30秒醒来  然后
        // 开始维护 连接数大小
        pool.setMaintSleep(30);
        // 设置tcp 相关的树形
        // 关闭nagle算法
        //  不设置连接超时
        pool.setNagle(false);
        pool.setSocketTO(30);
        pool.setSocketConnectTO(0);
        // 开始初始化 连接池
        pool.initialize();
    }
    public MemCacheServer(){}

//    /**
//     * getInstance:方法返回memCached. <br/>
//     *
//     * @return memCached的实例
//     * @since JDK 1.6
//     */
//    public static MemCacheServer getInstance(){
//        return memCacheServer;
//    }

    /**
     * set:对Memcahed的set 命令的封装用于向缓存添加新的键值对。如果键已经
     * 在,则之前的值将被替换. <br/>
     *
     * @param key
     * @param value
     * @return
     * @since JDK 1.6
     */
    public static boolean set(String key, Object value) {
        return client.set(key, value);
    }

    /**
     * set:对Memcahed的set 命令的封装用于向缓存添加新的键值对。如果键已经
     * 在,则之前的值将被替换. <br/>
     *
     * @param key
     * @param expireTime 单位为:ms
     * @param value
     * @return
     * @since JDK 1.6
     */
    public static boolean set(String key,int expireTime,Object value) {
        return client.set(key,value,new Date(expireTime));
//        return client.set(key, value);
    }

    /**
     * add:对Memcahed的add命令仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。如果缓存中已经存在键,则之前的值将仍然保持相同,并且您将获得响应 NOT_STORED. <br/>
     *
     * @param key
     * @param value
     * @param expiry 在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)
     * @return
     * @since JDK 1.6
     */
    public static boolean add(String key, Object value, Date expiry) {
        return client.add(key, value, expiry);
    }

    /**
     * add:对Memcahed的add命令的封装仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。如果缓存中已经存在键,则之前的值将仍然保持相同,并且您将获得响应 NOT_STORED. <br/>
     *
     * @param key
     * @param value
     * @return
     * @since JDK 1.6
     */
    public static boolean add(String key, Object value) {
        return client.add(key, value);
    }

    /**
     * add:对Memcahed的add命令的封装仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。如果缓存中已经存在键,则之前的值将仍然保持相同,并且您将获得响应 NOT_STORED. <br/>
     *
     * @param key
     * @param value
     * @return
     * @since JDK 1.6
     */
    public static boolean replace(String key, Object value) {
        return client.replace(key, value);
    }

    /**
     * replace:对Memcahed的replace命令仅当键已经存在时,replace 命令才会替换缓存中的键。如果缓存中不存在键,那么您将从 memcached 服务器接受到一条 NOT_STORED 响应. <br/>
     *
     * @param key
     * @param value
     * @param expiry
     * @return
     * @since JDK 1.6
     */
    public static boolean replace(String key, Object value, Date expiry) {
        return client.replace(key, value, expiry);
    }

    /**
     * delete:对Memcahed的delete命令的封装。delete 命令用于删除 memcached 中的任何现有值。您将使用一个键调用delete,如果该键存在于缓存中,则删除该值。. <br/>
     *
     * @param key
     * @return
     * @since JDK 1.6
     */
    public static boolean delete(String key) {
        return client.delete(key);
    }

    /**
     * get:get对Memcahed的get命令的封装 命令用于检索与之前添加的键值对相关的值. <br/>
     *
     * @param key
     * @return
     * @since JDK 1.6
     */
    public static Object get(String key) {
        return client.get(key);

    }

    /**
     * gets:gets 命令的功能类似于基本的 get 命令。两个命令之间的差异在于,gets 返回的信息稍微多一些:64 位的整型值非常像名称/值对的 “版本” 标识符. <br/>
     *
     * @param key
     * @return
     * @since JDK 1.6
     */
    public static Object gets(String key) {

        return client.gets(key);
    }


    /**
     * flushAll:用于清理缓存中的所有名称/值对。如果您需要将缓存重置到干净的状态,则 flush_all 能提供很大的用处. <br/>
     *
     * @return
     * @since JDK 1.6
     */
    public static boolean flushAll() {

        return client.flushAll();

    }

    /**
     * 测试代码
     * @param args
     */
    public static void main(String[] args) {
//        MemCacheServer cache = MemCacheServer.getInstance();
//        cache.set("zf", 18);
//        System.out.println("zf get value : " + cache.get("zf"));
//        cache.replace("zf", 20);
//        cache.set("zf1", 19);
//        // cache.delete("zf");
//        System.out.println("zf get value : " + cache.get("zf"));
//        System.out.println("zf get value : " + cache.get("zf1"));
//            /*cache.flushAll();
//            System.out.println(1);
//            System.out.println("zf get value : " + cache.get("zf"));
//            System.out.println("zf get value : " + cache.get("zf1"));*/
////        System.out.println("zf get value" + cache.gets("zf"));
//        cache.set("zf2", "沄莲");
//        cache.set("zf3", "沄莲来咯");
//        cache.set("zf4", "我在测试咯");
//        System.out.println("zf get value : " + cache.get("zf2"));
//        System.out.println("zf get value : " + cache.get("zf3"));
//        System.out.println("zf get value : " + cache.get("zf4"));


//        MemCacheServer cache = MemCacheServer.getInstance();
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<5;i++){
            list.add(i+1);
        }
        MemCacheServer.set("age", "27岁");
        MemCacheServer.set("list",list);
        System.out.println("age get value : " + MemCacheServer.get("age"));
        System.out.println("age get value : " + MemCacheServer.get("list"));
        MemCacheServer.flushAll();
        System.out.println("age get value : " + MemCacheServer.get("age"));

        //todo 设置失效时间  时间的单位为 ms
        List<Integer> integerList = list;
        MemCacheServer.set("integerList",5000,integerList);
        System.out.println("integerList get value before: " + MemCacheServer.get("integerList"));
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("integerList get value after: " + MemCacheServer.get("integerList"));
    }
}

猜你喜欢

转载自yunlian0621.iteye.com/blog/2338286
今日推荐