1、依赖



com.googlecode.xmemcached
xmemcached
2.4.7

2、使用

  1. package org.example;
  2. import lombok.extern.slf4j.Slf4j;
  3. import net.rubyeye.xmemcached.MemcachedClient;
  4. import net.rubyeye.xmemcached.MemcachedClientBuilder;
  5. import net.rubyeye.xmemcached.XMemcachedClientBuilder;
  6. import net.rubyeye.xmemcached.exception.MemcachedException;
  7. import net.rubyeye.xmemcached.utils.AddrUtil;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import java.io.IOException;
  12. import java.util.concurrent.TimeoutException;
  13. /**
  14. * @Author: 李孟帅
  15. * @CreateTime: 2022/3/14 18:48:47
  16. * @Description: TODO
  17. */
  18. @Slf4j
  19. public class Demo {
  20. MemcachedClient memcachedClient;
  21. @Before
  22. public void init() throws IOException {
  23. MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.47.129:11211"));
  24. memcachedClient = builder.build();
  25. }
  26. @After
  27. public void close() throws IOException {
  28. //close memcached client
  29. memcachedClient.shutdown();
  30. }
  31. @Test
  32. public void testSet() throws InterruptedException, MemcachedException, TimeoutException {
  33. boolean r = memcachedClient.set("a", 5, "Hello,xmemcached");
  34. log.info("set 操作:{}",r);
  35. }
  36. @Test
  37. public void testGet() throws InterruptedException, MemcachedException, TimeoutException {
  38. String value = memcachedClient.get("a");
  39. log.info("get {}",value);
  40. }
  41. @Test
  42. public void testDelete() throws InterruptedException, MemcachedException, TimeoutException {
  43. boolean hello = memcachedClient.delete("a");
  44. log.info("delete 操作:{}",hello);
  45. Object value = memcachedClient.get("a");
  46. System.out.println("hello=" + value);
  47. }
  48. @Test
  49. public void testTouch() throws InterruptedException, MemcachedException, TimeoutException {
  50. boolean hello = memcachedClient.touch("a", 5);
  51. // memcachedClient.getAndTouch("a",11); 注意 GAT is only supported by binary protocol
  52. log.info("touch a={}",hello);
  53. }
  54. }

3、参考:

https://blog.csdn.net/yangkai_hudong/article/details/38017553