本篇博客使用上一节的Spring Boot环境。
关系型数据库在性能上总是存在一些缺陷,在较高并发的场景下,频繁的数据库读写操作往往会对服务器造成很大的负担。所以,我们一般在使用传统关系型数据库时,会与具有高效存取功能的缓存系统结合使用,以提高系统的访问性能。Redis是一种可持久存储的缓存系统,是一个高性能的key-value数据库,使用键值对来存储数据。
在POM文件中增加如下配置,这里使用Gson来进行json的序列化:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
为了能正确的使用Redis,我们需要对其进行一系列的初始化工作,主要是对它存取的字符串进行一个JSON格式的系列化初始化配置。首先,在application.yml加入redis的配置信息:
redis:
host: xxx.xxx.xxx.xxx #你的redis的主机ip地址
port: 6379
timeout: 2000s
创建redis的配置类,进行相关配置:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@Configuration
public class RedisConfig{
@Autowired
private RedisConnectionFactory connectionFactory;
@Bean
public RedisTemplate<String, String> redisTemplate(){
StringRedisTemplate template = new StringRedisTemplate(connectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(mapper);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
对于Redis,Spring Boot并没有提供像JPA那样相应的资源库接口,所以只能自定义编写Redis的操作服务类,下面使用redis来操作上一章的用户类信息。
创建Redis的服务类:
import com.example.springbootjpa.pojo.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Repository
public class UserRedis {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public void add(String key, Long time, User user){
Gson gson = new Gson();
redisTemplate.opsForValue().set(key, gson.toJson(user), time, TimeUnit.MINUTES);
}
public void add(String key, Long time, List<User> users){
Gson gson = new Gson();
redisTemplate.opsForValue().set(key, gson.toJson(users), time, TimeUnit.MINUTES);
}
public User get(String key){
Gson gson = new Gson();
User user = null;
String userJson = redisTemplate.opsForValue().get(key);
if(!StringUtils.isEmpty(userJson)){
user = gson.fromJson(userJson, User.class);
}
return user;
}
public List<User> getList(String key){
Gson gson = new Gson();
List<User> ts = null;
String listJson = redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(listJson)){
ts = gson.fromJson(listJson, new TypeToken<User>(){}.getType());
}
return ts;
}
public void delete(String key){
redisTemplate.opsForValue().getOperations().delete(key);
}
}
编写测试用例:
import com.example.springbootjpa.pojo.Department;
import com.example.springbootjpa.pojo.Role;
import com.example.springbootjpa.pojo.User;
import com.example.springbootredis.UserRedis;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
private static Logger logger = LoggerFactory.getLogger(RedisTest.class);
@Autowired
private UserRedis userRedis;
@Before
public void setUp(){
Department department = new Department();
department.setName("开发部");
Role role = new Role();
role.setName("admin");
User user = new User();
user.setName("user");
user.setCreateDate(new Date());
user.setDepartment(department);
List<Role> roles = new ArrayList<>();
roles.add(role);
user.setRoles(roles);
userRedis.delete(this.getClass().getName()+":userByName:"+user.getName());
userRedis.add(this.getClass().getName()+":userByName:"+user.getName(), 10L, user);
}
@Test
public void get(){
User user = userRedis.get(this.getClass().getName()+":userByName:user");
logger.info("====user==== user name:{}, department name:{}, role name:{}", user.getName(),
user.getDepartment().getName(), user.getRoles().get(0).getName());
}
}
运行测试用例可得到如下结果: