JavaEE--redis缓存
redis
参考https://blog.csdn.net/guokezhongdeyuzhou/article/details/79789629
Spring Boot Redis RedisTemplate 相关API介绍:https://my.oschina.net/sdlvzg/blog/1488890
高并发秒杀——SpringBoot集成redis
blog2.0--Springboot添加redis缓存(注解方式)
Redis常见的五大数据类型
* String(字符串)、List(列表)、Set(集合)、Hash(散列)、ZSet(有序集合)
* stringRedisTemplate.opsForValue()[String(字符串)]
* stringRedisTemplate.opsForList()[List(列表)]
* stringRedisTemplate.opsForSet()[Set(集合)]
* stringRedisTemplate.opsForHash()[Hash(散列)]
* stringRedisTemplate.opsForZSet()[ZSet(有序集合)]
/默认如果保存对象,使用jdk序列化机制,序列化后的数据保存到redis中
//redisTemplate.opsForValue().set("emp-01",empById);
//1、将数据以json的方式保存
(1)自己将对象转为json
(2)redisTemplate默认的序列化规则;改变默认的序列化规则;
需要注意的一点是:
RedisCacheConfiguration类为2.x新增的配置类,增加了几个配置项。这里比较奇怪的是调用它的配置方法每一次都会重新生成一个配置对象,而不是在原对象上直接修改参数值
之前在网上看到的配置Redis时,再写RedisConfig类时,在写RedisCacheManager的Bean时,下面new的RedisCacheManager总是报错,说是没有这种构造器,但是网上的代码都是这样写的,所以就很懵,直到今天,下到人家的项目,也是如下定义RedisConfig的,当时找到他的RedisCacheConfiguration类才发现版本不对
@Bean public RedisCacheManager redisCacheManager(RedisTemplate<Object, Object> myredisTemplate){ RedisCacheManager redisCacheManager = new RedisCacheManager(myredisTemplate); redisCacheManager.setUsePrefix(true); return redisCacheManager; }
别人的版本,可以运行
自己的版本,不能运行(没有那个构造器)
要吐血了,搞了好多天。晕死
RedisConfig类如下
@Configuration public class RedisConfig { /*@Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class); template.setDefaultSerializer(serializer); return template; }*/ @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } @Bean RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) { //初始化一个RedisCacheWriter RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); //设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key,JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现 //ClassLoader loader = this.getClass().getClassLoader(); //JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader); //RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jdkSerializer); //RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair); RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig(); //设置默认超过期时间是30秒 defaultCacheConfig.entryTtl(Duration.ofSeconds(30)); //初始化RedisCacheManager RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig); return redisCacheManager; } /*@Bean public RedisCacheManager redisCacheManager(RedisTemplate<Object, Object> myredisTemplate){ RedisCacheManager redisCacheManager = new RedisCacheManager(myredisTemplate); redisCacheManager.setUsePrefix(true); return redisCacheManager; }*/ }
其中 RedisCacheManager redisCacheManager 用来管理缓存
RedisTemplate<String, String> redisTemplate 是用来定义序列化规则的,使用JSON缓存,可以在缓存中直接看到结果
CacheManager===Cache 缓存组件来实际给缓存中存取数据
* 1)、引入redis的starter,容器中保存的是 RedisCacheManager;
* 2)、RedisCacheManager 帮我们创建 RedisCache 来作为缓存组件;RedisCache通过操作redis缓存数据的
* 3)、默认保存数据 k-v 都是Object;利用序列化保存;如何保存为json
* 1、引入了redis的starter,cacheManager变为 RedisCacheManager;
* 2、默认创建的 RedisCacheManager 操作redis的时候使用的是 RedisTemplate<Object, Object>
* 3、RedisTemplate<Object, Object> 是 默认使用jdk的序列化机制,所以不是我们需要的序列化机制,要自定义Cachemanager
不使用注解,使用编码的方式
public User login(User register) { if(register == null || register.getPassword() == null || register.getUserName() == null){ throw new RuntimeException("用户信息不全"); } User user = userDao.query(register); redisTemplate.opsForValue().set("u1", user); Object o = redisTemplate.opsForValue().get("u1"); System.out.println(o); return user; }