导入依赖
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sunyuqi</groupId>
<artifactId>memchaeddemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.4.5</version>
</dependency>
</dependencies>
</project>
配置类
package com.sunyuqi.config;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.io.IOException;
@Configuration
@Profile("test")
public class MemcachedConfig {
@Bean
public MemcachedClient memcachedClient() throws IOException {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses("192.168.130.128:11211"));
// 设置多个("server1:11211 server2:11211")
// 宕机报警
builder.setFailureMode(true);
/**
* 设置连接池大小,即客户端个数
*/
builder.setConnectionPoolSize(10);
return builder.build();
}
}
当我们使用memcached集群时,默认的客户端计算就是 key的哈希值模以连接数。我们可以选择一致性hash算法
builder.setSessionLocator(new KetamaMemcachedSessionLocator());
User对象
package com.sunyuqi.pojo;
import java.io.Serializable;
public class User implements Serializable {
private String userId;
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
'}';
}
}
引导类
package com.sunyuqi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootApplicationDemo {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationDemo.class,args);
}
}
测试类
package com.sunyuqi;
import com.sunyuqi.pojo.User;
import net.rubyeye.xmemcached.MemcachedClient;
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.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootApplicationDemo.class)
@ActiveProfiles("test")
public class MemcachedTest {
@Autowired
MemcachedClient memcachedClient;
@Test
public void testSet() throws Exception {
memcachedClient.set("test_key",0,"test_value");
}
@Test
public void testSetUser() throws Exception{
User user = new User();
user.setUserId("1");
user.setUserName("张三");
memcachedClient.set("user",0,user);
}
@Test
public void testGet() throws Exception{
User user = memcachedClient.get("user");
String value = memcachedClient.get("test_key");
System.out.println(user);
System.out.println(value);
}
}
我们向memcached中存入了一个字符串和一个对象
测试Get
成功取到缓存