最近学习了一下springboot集成redis的知识,做了下笔记,现在写成博客,一来希望对看到此博客的学习者一点帮助,二来自己将来可以作为复习。下面我将springboot集成redis的步骤和代码都记录了下来,只要按步骤来,一定可以复现。 此外说明:我在下文中测试时有些地方调用了自己的数据库mybatis操作,如Student类,因为无关本主题,就没把相关的代码写出来了。还有在yml配置的时候我只写出了redis的配置,其他的yml配置都省略了,望周知。 redis的大型网站技术介绍: 大型网站系统中的安全和并发设计; 大型网站的缓存设计; 新浪微博排行榜; 电商系统购物车; 分布式系统session共享; 分布式系统的消息发布和订阅;
--------------------------------------------------------------------------------------------
springboo集成redis关键步骤: 1.在pom.xml中配置相关maven依赖:spring-boot-starter-data-redis spring-boot-starter-cache
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2.在springbboot的yml配置文件配置redis连接信息。(根据你的实际情况写。另外其他yml配置这里均省去)
spring:
redis: #配置redis连接信息
host: 127.0.0.1
port: 6379
timeout: 1000
3.配置完上述步骤,springboot将自动redis template,在需要操作redis的类中注入redisTemplate 在使用的类中注入: @Autowired private RedisTemplate<String,String> redisTemplate; @Autowired private RedisTemplate<Object,Object> redisTemplate;
案例:TestController
redis常见数据结构: String(字符串) List(列表) Set(集合) Hash ZSet(有序集合)
package com.cqupt.job.redis_demo.Controller;
import com.cqupt.job.dao.IStudentDao;
import com.cqupt.job.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("test")
public class TestController {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Autowired
IStudentDao iStudentDao;
//value数据类型
@RequestMapping("/student")
@ResponseBody
public List<Student> showStu(){
List<Student> students = iStudentDao.displayStu();
redisTemplate.opsForValue().set("1", students);
return (List<Student>) redisTemplate.opsForValue().get("1");
}
//测试list数据类型,列表方式,压入三个元素进队列redis,然后弹出一个,redis中将只有两个元素
@RequestMapping("/redisList")
@ResponseBody
public String redisList(){
String key = "redis-list-key";
String value = String.valueOf(System.currentTimeMillis()+1);
redisTemplate.opsForList().leftPush(key, value);
redisTemplate.opsForList().leftPush(key, value);
redisTemplate.opsForList().leftPush(key, value);
return (String) redisTemplate.opsForList().leftPop(key);
}
//测试set数据类型,类比java的hashset,set里的元素不能重复
@RequestMapping("/redisSet")
@ResponseBody
public String redisSet(){
String key = "redis-set-key";
String value = String.valueOf(System.currentTimeMillis()+1);
redisTemplate.opsForSet().add(key, value);
redisTemplate.opsForSet().add(key, value+"-1");
redisTemplate.opsForSet().add(key, value+"-2");
return (String) redisTemplate.opsForSet().pop(key);
}
}
Redis缓存序列化处理: Springboot的redisTemplate用来操作key-value对象类型,默认采用JDK序列化类型,JDK序列化性能差,而且存储到 Redis服务端是二进制不便查询,JDK序列化要求实体实现Serializable接口,修改RedisTemplate的序列化配置方式如下: (StringRedisTemplatem默认已经做了序列化处理,所以无需额外配置) 1.重写RedisTemplate Bean配置(核心); 2.新增实现Serializable接口的实体(实体类); 3.RedisTemplate获取的对象需要类型转换(Controller);
package com.cqupt.job.utils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**redis缓存序列化处理**/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
//配置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
//使用JacksonRedisSerializer序列化和反序列化redis的value值
//(默认使用的是JDK的序列化方式)
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
//使用指定要序列化的域 filed get set 以及修饰符范围
//ANY是都有包括private额和public
objectMapper.setVisibility(PropertyAccessor.ALL,
JsonAutoDetect.Visibility.ANY);
//指定序列化输入的类型,类必须是非final修饰的,否则会异常
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//值采用json序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//使用StringRedisSerializer序列化和反序列化redis的key值
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置hash key 和value序列化模式
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
做了上述配置后,无需改变其他地方,就可以把数据以json的格式在redis中存储了!
补充:
springboot redis cache注解技术(没有细研究) spring高版本引入了cache的注解技术,该技术是一种规范。Redis的cache技术,底层使用的是spring data redis, 需要掌握的注解有@EnableCaching,@Cacheable,@CacheEvict,@CachePut,@Caching,@CacheConfig. @EnableCaching作用springboot项目的配置类,用来打开springboot的chache自动装配。 @Cacheable 方法被调用前先从缓存中查找返回,缓存中未命中则调用方法(数据库查询)返回。(缓存中有了不会更新) @CacheEvict 方法执行后删除缓存,可以通过设置beforeInvocation=true提前执行删除缓存操作。 @CachePut 方法执行后更新缓存,旧的值会被覆盖。注意此注解和@Cacheable注解不同,@Cacheable注解是如果缓存 中有值则不执行方法,而后者是标记的方法一定会执行. @Caching 多个缓存的复合操作 @CacheConfig 类上注解,对类中的缓存统一配置.