Spring Boot --整合redis进行缓存注解开发

2 篇文章 0 订阅
2 篇文章 0 订阅

1.引入redis依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.配置redis:

spring:
  redis:
    host: localhost

3.启动redis服务端

4.注入redis对象

springboot简化了redis开发,提供这两个对象.所以我们可以在类里注入这两个对象

@Autowired
StringRedisTemplate stringRedisTemplate;  //k-v字符串的操作,

@Autowired
RedisTemplate redisTemplate;      //k-v对象的操作

5.使用两个对象

对象的方法可以到redis中文网查询:

stringRedisTemplate.opsForValue();	//String类型
stringRedisTemplate.opsForList();		//list集合类型
stringRedisTemplate.opsForHash();	//Hash散列表类型
stringRedisTemplate.opsForSet();		//set集合类型
stringRedisTemplate.opsForZSet();		//有序set集合类型
...

两个对象可以调用的方法相同,这里我们调用了字符串操作的对象.

1)字符串操作插入数据示例:
stringRedisTemplate.opsForValue().append("hello","helloworld");		//插入String类型数据,hello-helloworld
String s = stringRedisTemplate.opsForValue().get("hello");		//取出key为hello的String类型数据
stringRedisTemplate.opsForList().leftPush("mylist", "zhangsan");		//插入list类型数据,mylist-zhangsan
2)对象操作插入数据示例:
Employee employee = employeeMapper.selectById(1);
//默认如果保存对象,会用jar序列化器序列化方式存入redis中
redisTemplate.opsForValue().set("emp",employee);

这样保存之后会发现,redis数据库里的数据是一堆jar序列化器序列化之后的结果,我们可以自定义序列化器将其转化为json数据类型更加直观方便开发进行
创建redis配置类MyRedisConfig.java自定义序列化器:

package com.id0304.cache.config;

import com.id0304.cache.pojo.Employee;
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 java.net.UnknownHostException;

@Configuration
public class MyRedisConfig {
    /**
     * 缓存管理器
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //初始化一个RedisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        //设置CacheManager的值序列化方式为json序列化
        RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
        RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
                .fromSerializer(jsonSerializer);
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(pair);
        //设置默认超过期时间是60秒
        defaultCacheConfig.entryTtl(Duration.ofSeconds(60));
        //初始化RedisCacheManager
        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
    }
}

6.当然,我们之后更多使用注解进行缓存开发

关于注解开发缓存我在上一篇博客有提到过:springboot注解缓存开发

这里我们配置了redis之后,所进行的缓存开发springboot会自动配置到redis,即CacheManager会选定为redis,默认的CacheManager会不使用.

7.我们发现这个自定义的template和manager只适用于employee对象,一旦出现了多个pojo,如department.java的话从redis取出json数据反序列化的时候就会报错了,因此针对多个pojo我们要配置多组template和manager.当然配置完以后我们还要让对应使用缓存的类识别出来要用哪一个manager,这时@CacheConfig(cacheManager=“employeeCacheManager”)就派上用场了.

注!如果配置了多个CacheManager的话一定要指定一个为主配置,打上@Primary注解,被标上该注解的manager会充当默认的manager.当然实际开发过程中最好使用Object对象作为默认的manager,可以反序列化多种对象.

这里我用了EmployeeService.java使用redis为示例:

@Service
@CacheConfig(cacheNames = "emp",cacheManager = "employeeCacheManager")
public class EmployeeService {

关于springboot的cache注解使用方法详情还是见我上一篇博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值