Springboot整合memcached(牛刀小试)


Xmemcached官方介绍:

XMemcached是一个新java memcached client。也许你还不知道memcached是什么?简单来说,Memcached 是一个高性能的分布式内存对象的key-value缓存系统,用于动态Web应用以减轻数据库负载,现在也有很多人将它作为内存式数据库在使用,memcached通过它的自定义协议与客户端交互,而XMemcached就是它的一个java客户端实现。


Start coding...

1. 添加pom依赖

<dependency>
    <groupId>com.googlecode.xmemcached</groupId>
    <artifactId>xmemcached</artifactId>
    <version>2.4.6</version>
</dependency>

2.简单测试用例

package com.wcg.boot;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author Miracle.wcg
 * @Date 2019-07-24 00:18
 */
public class SimpleMemcache {
    public static void main(String[] args) {
        //连接服务
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.56.101:11211"));
        MemcachedClient memcachedClient = null;
        try {
            memcachedClient = builder.build();
            //存储数据是通过set方法,它有三个参数:
            //第一个是存储的key名称,
            //第二个是expire时间(单位秒),超过这个时间,memcached将这个数据替换出去,0表示永久存储(默认是一个月),
            //第三个参数就是实际存储的数据,可以是任意的java可序列化类型。
            memcachedClient.set("hello", 0, "Hello,xmemcached");
            //获取存储的数据
            String value = memcachedClient.get("hello");

            //等待3秒超时,如果3秒超时就抛出TimeutException,用户需要自己处理这个异常
            //通过调用CountDownLatch.await(timeout)方法实现等待
            //value = memcachedClient.get("hello", 3000);

            //更新缓存数据的超时时间5s
            //memcachedClient.touch("hello", 5000);

            //支持原子操作 > 1.3.6 verison
            //memcachedClient.getAndTouch("hello",5000);
            System.out.println("hello=" + value);

            //删除存储的数据
            memcachedClient.delete("hello");
            value = memcachedClient.get("hello");
            System.out.println("hello=" + value);
        } catch (MemcachedException e) {
            System.err.println("MemcachedClient operation fail");
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.err.println("MemcachedClient operation timeout");
            e.printStackTrace();
        } catch (InterruptedException e) {
            // ignore
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            //close memcached client
            memcachedClient.shutdown();
        } catch (IOException e) {
            System.err.println("Shutdown MemcachedClient fail");
            e.printStackTrace();
        }
    }
}

3.较全面测试用例

package com.wcg.boot;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.transcoders.StringTranscoder;
import net.rubyeye.xmemcached.utils.AddrUtil;

/**
 * @Author Miracle.wcg
 * @Date 2019-07-24 00:22
 */
public class FullMemcache {
    public static void main(String[] args) throws Exception {
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.56.101:11211"));
        MemcachedClient client = builder.build();
        client.flushAll();
        //存储hello对应的world字符串
        if (!client.set("hello", 0, "world")) {
            System.err.println("set error");
        }
        //添加
        if (client.add("hello", 0, "dennis")) {
            System.err.println("Add error,key is existed");
        }
        //替换
        if (!client.replace("hello", 0, "dennis")) {
            System.err.println("replace error");
        }
        client.append("hello", " good");
        client.prepend("hello", "hello ");
        String name = client.get("hello", new StringTranscoder());
        System.out.println(name);
        //删除数据并且告诉memcached不用返回应答,因此这个方法不会等待应答直接返回,特别适合于批量处理
        client.deleteWithNoReply("hello");
    }
}


暂无评论