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*/@Slf4jpublic class Demo {MemcachedClient memcachedClient;@Beforepublic void init() throws IOException {MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.47.129:11211"));memcachedClient = builder.build();}@Afterpublic void close() throws IOException {//close memcached clientmemcachedClient.shutdown();}@Testpublic void testSet() throws InterruptedException, MemcachedException, TimeoutException {boolean r = memcachedClient.set("a", 5, "Hello,xmemcached");log.info("set 操作:{}",r);}@Testpublic void testGet() throws InterruptedException, MemcachedException, TimeoutException {String value = memcachedClient.get("a");log.info("get {}",value);}@Testpublic 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);}@Testpublic void testTouch() throws InterruptedException, MemcachedException, TimeoutException {boolean hello = memcachedClient.touch("a", 5);// memcachedClient.getAndTouch("a",11); 注意 GAT is only supported by binary protocollog.info("touch a={}",hello);}}
3、参考:
https://blog.csdn.net/yangkai_hudong/article/details/38017553
