From 236620cacd945207dbfd5dc33059500332915f9e Mon Sep 17 00:00:00 2001
From: luoxiang <2806718453@qq.com>
Date: Tue, 1 Jan 2019 15:44:04 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20spring=20boot=20memcached?=
=?UTF-8?q?=20=E7=94=A8=E4=BE=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
spring-boot/spring-boot-memcached/pom.xml | 57 ++++++++++++++++
.../SpringBootMemcachedApplication.java | 14 ++++
.../springboot/bean/Programmer.java | 26 +++++++
.../springboot/config/MemcacheConfig.java | 68 +++++++++++++++++++
.../src/main/resources/application.properties | 0
.../springboot/MemcacheObjectTest.java | 36 ++++++++++
.../heibaiying/springboot/MemcacheTest.java | 33 +++++++++
7 files changed, 234 insertions(+)
create mode 100644 spring-boot/spring-boot-memcached/pom.xml
create mode 100644 spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/SpringBootMemcachedApplication.java
create mode 100644 spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/bean/Programmer.java
create mode 100644 spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/config/MemcacheConfig.java
create mode 100644 spring-boot/spring-boot-memcached/src/main/resources/application.properties
create mode 100644 spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheObjectTest.java
create mode 100644 spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheTest.java
diff --git a/spring-boot/spring-boot-memcached/pom.xml b/spring-boot/spring-boot-memcached/pom.xml
new file mode 100644
index 0000000..8dff5fc
--- /dev/null
+++ b/spring-boot/spring-boot-memcached/pom.xml
@@ -0,0 +1,57 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.1.1.RELEASE
+
+
+ com.heibaiying
+ spring-boot-memcached
+ 0.0.1-SNAPSHOT
+ spring-boot-memcached
+ Memcached project for Spring Boot
+
+
+ 1.8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.projectlombok
+ lombok
+ true
+
+
+
+
+ com.googlecode.xmemcached
+ xmemcached
+ 2.4.5
+
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/SpringBootMemcachedApplication.java b/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/SpringBootMemcachedApplication.java
new file mode 100644
index 0000000..2aa56e2
--- /dev/null
+++ b/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/SpringBootMemcachedApplication.java
@@ -0,0 +1,14 @@
+package com.heibaiying.springboot;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class SpringBootMemcachedApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SpringBootMemcachedApplication.class, args);
+ }
+
+}
+
diff --git a/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/bean/Programmer.java b/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/bean/Programmer.java
new file mode 100644
index 0000000..fde730c
--- /dev/null
+++ b/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/bean/Programmer.java
@@ -0,0 +1,26 @@
+package com.heibaiying.springboot.bean;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @author : heibaiying
+ * @description :
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class Programmer implements Serializable {
+
+ private String name;
+
+ private int age;
+
+ private float salary;
+
+ private Date birthday;
+}
diff --git a/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/config/MemcacheConfig.java b/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/config/MemcacheConfig.java
new file mode 100644
index 0000000..f69175b
--- /dev/null
+++ b/spring-boot/spring-boot-memcached/src/main/java/com/heibaiying/springboot/config/MemcacheConfig.java
@@ -0,0 +1,68 @@
+package com.heibaiying.springboot.config;
+
+import net.rubyeye.xmemcached.MemcachedClient;
+import net.rubyeye.xmemcached.XMemcachedClientBuilder;
+import net.rubyeye.xmemcached.command.TextCommandFactory;
+import net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator;
+import net.rubyeye.xmemcached.transcoders.SerializingTranscoder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author : heibaiying
+ */
+@Configuration
+public class MemcacheConfig {
+
+ /*
+ * Memcached 单机版本简单配置
+ */
+ @Bean
+ public MemcachedClient memcachedClient() {
+ XMemcachedClientBuilder builder = new XMemcachedClientBuilder("192.168.0.108:11211");
+ MemcachedClient memcachedClient = null;
+ try {
+ memcachedClient = builder.build();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return memcachedClient;
+ }
+
+ /*
+ * Memcached 集群版本配置
+ */
+
+ /*@Bean*/
+ public MemcachedClient memcachedClientForCluster() {
+
+ List addressList = new ArrayList<>();
+ addressList.add(new InetSocketAddress("192.168.0.108", 11211));
+ addressList.add(new InetSocketAddress("192.168.0.108", 11212));
+ // 赋予权重
+ int[] weights = {1, 2};
+ XMemcachedClientBuilder builder = new XMemcachedClientBuilder(addressList, weights);
+ // 设置连接池大小
+ builder.setConnectionPoolSize(10);
+ // 协议工厂
+ builder.setCommandFactory(new TextCommandFactory());
+ // 分布策略,一致性哈希KetamaMemcachedSessionLocator或者ArraySessionLocator(默认)
+ builder.setSessionLocator(new KetamaMemcachedSessionLocator());
+ // 设置序列化器
+ builder.setTranscoder(new SerializingTranscoder());
+ MemcachedClient memcachedClient = null;
+ try {
+ memcachedClient = builder.build();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return memcachedClient;
+ }
+
+
+}
\ No newline at end of file
diff --git a/spring-boot/spring-boot-memcached/src/main/resources/application.properties b/spring-boot/spring-boot-memcached/src/main/resources/application.properties
new file mode 100644
index 0000000..e69de29
diff --git a/spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheObjectTest.java b/spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheObjectTest.java
new file mode 100644
index 0000000..268632f
--- /dev/null
+++ b/spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheObjectTest.java
@@ -0,0 +1,36 @@
+package com.heibaiying.springboot;
+
+import com.heibaiying.springboot.bean.Programmer;
+import net.rubyeye.xmemcached.MemcachedClient;
+import net.rubyeye.xmemcached.exception.MemcachedException;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.Date;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * @author : heibaiying
+ * @description :Memcached 序列化与反序列化
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class MemcacheObjectTest {
+
+ @Autowired
+ private MemcachedClient memcachedClient;
+
+ @Test
+ public void operate() throws InterruptedException, MemcachedException, TimeoutException {
+ memcachedClient.set("programmer", 0, new Programmer("xiaoming", 12, 5000.21f, new Date()));
+ Programmer programmer = memcachedClient.get("programmer");
+ System.out.println("hello ," + programmer.getName());
+ memcachedClient.delete("programmer");
+ programmer = memcachedClient.get("programmer");
+ Assert.assertNull(programmer);
+ }
+}
diff --git a/spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheTest.java b/spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheTest.java
new file mode 100644
index 0000000..a4b9271
--- /dev/null
+++ b/spring-boot/spring-boot-memcached/src/test/java/com/heibaiying/springboot/MemcacheTest.java
@@ -0,0 +1,33 @@
+package com.heibaiying.springboot;
+
+import net.rubyeye.xmemcached.MemcachedClient;
+import net.rubyeye.xmemcached.exception.MemcachedException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.concurrent.TimeoutException;
+
+/**
+ * @author : heibaiying
+ * @description : Memcached 操作基本对象
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class MemcacheTest {
+
+ @Autowired
+ private MemcachedClient memcachedClient;
+
+ @Test
+ public void operate() throws InterruptedException, MemcachedException, TimeoutException {
+ memcachedClient.set("hello", 0, "Hello,cluster xmemcached");
+ String value = memcachedClient.get("hello");
+ System.out.println("hello=" + value);
+ memcachedClient.delete("hello");
+ value = memcachedClient.get("hello");
+ System.out.println("hello=" + value);
+ }
+}