1、依赖
2、使用
package org.example;
import lombok.extern.slf4j.Slf4j;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @Author: 李孟帅
* @CreateTime: 2022/3/14 18:48:47
* @Description: TODO
*/
@Slf4j
public class Demo {
MemcachedClient memcachedClient;
@Before
public void init() throws IOException {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.47.129:11211"));
memcachedClient = builder.build();
}
@After
public void close() throws IOException {
//close memcached client
memcachedClient.shutdown();
}
@Test
public void testSet() throws InterruptedException, MemcachedException, TimeoutException {
boolean r = memcachedClient.set("a", 5, "Hello,xmemcached");
log.info("set 操作:{}",r);
}
@Test
public void testGet() throws InterruptedException, MemcachedException, TimeoutException {
String value = memcachedClient.get("a");
log.info("get {}",value);
}
@Test
public void testDelete() throws InterruptedException, MemcachedException, TimeoutException {
boolean hello = memcachedClient.delete("a");
log.info("delete 操作:{}",hello);
Object value = memcachedClient.get("a");
System.out.println("hello=" + value);
}
@Test
public void testTouch() throws InterruptedException, MemcachedException, TimeoutException {
boolean hello = memcachedClient.touch("a", 5);
// memcachedClient.getAndTouch("a",11); 注意 GAT is only supported by binary protocol
log.info("touch a={}",hello);
}
}
3、参考:
https://blog.csdn.net/yangkai_hudong/article/details/38017553