请先安装集群或者单台。
参考:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-redis;
https://blog.csdn.net/qq_24076549/article/details/79536447
application.yml配置
spring:
redis:
database: 1
password: guest
cluster:
max-redirects: 3
nodes:
- 192.168.131.139:6379
- 192.168.131.140:6379
- 192.168.131.141:6379
- 192.168.131.142:6379
- 192.168.131.143:6379
- 192.168.131.144:6379
lettuce:
pool:
max-active: 100 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 100 # 连接池中的最大空闲连接
min-idle: 50 # 连接池中的最小空闲连接
max-wait: 6000 # 连接池最大阻塞等待时间(使用负值表示没有限制)
timeout: 1000
XML配置
4.0.0
favorites
redis
0.0.1-SNAPSHOT
jar
redis
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-redis
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.dataformat
jackson-dataformat-yaml
net.sf.json-lib
json-lib
2.4
jdk15
org.springframework.boot
spring-boot-maven-plugin
RedisConfig配置
package com.redis.config;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
@Configuration
@EnableCaching // 开启缓存支持
public class RedisConfig extends CachingConfigurerSupport {
@Resource
private LettuceConnectionFactory lettuceConnectionFactory;
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuffer sb = new StringBuffer();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
// 缓存管理器
@Bean
public CacheManager cacheManager() {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(lettuceConnectionFactory);
@SuppressWarnings("serial")
Set cacheNames = new HashSet() {
{
add("codeNameCache");
}
};
builder.initialCacheNames(cacheNames);
return builder.build();
}
/**
* RedisTemplate配置
*/
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
// 设置序列化
Jackson2JsonRedisSerializer
model:User
package com.redis.model;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private int age;
public User() {
super();
}
public User(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
UserService
package com.redis.service;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import com.redis.model.User;
public interface UserService {
@Cacheable(key="'user_'+#id",value="'user'+#id")
User getUser(String id);
@CacheEvict(key="'user_'+#id", value="users", condition="#id!=1")
void deleteUser(String id);
}
UserServiceImpl
package com.redis.service.impl;
import org.springframework.stereotype.Service;
import com.redis.model.User;
import com.redis.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Override
public User getUser(String id) {
System.out.println(id+"进入实现类获取数据!");
User user = new User();
user.setId(id);
user.setName("香菇,难受");
user.setAge(18);
return user;
}
@Override
public void deleteUser(String id) {
System.out.println(id+"进入实现类删除数据!");
}
}
RedisApplicationTests
package com.redis;
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.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import com.redis.model.User;
import com.redis.service.UserService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {
@Autowired
private StringRedisTemplate template;
@Autowired
private UserService userService;
@Test
public void get() {
//template.opsForValue().set("test", "我是地瓜");
System.out.println(template.opsForValue().get("test"));
}
@Test
public void contextLoads() {
User user = new User();
user.setId("1");
user.setAge(12);
user.setName("张三");
template.opsForValue().append("date", user.toString());
System.out.println(template.opsForValue().get("date"));
}
@Test
public void getUser() {
for (int i = 0; i < 10; i++) {
User user = userService.getUser(String.valueOf(i));
System.out.println(user);
}
}
}
我的目录结构:
由于这篇是之前写的,后面我又进行了更新,可以参考我的GitHubdemo
GitHub项目地址:https://github.com/LX1309244704/SpringBoot-master/tree/master/springboot-redis