spring boot使用redis

1.导入redis包

pom.xml文件中添加依赖

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

2.RedisTemplate

分为StringRedisTemplate和RedisTemplate,主要学习记录后者

在Test类中,使用RedisTemplate保存查询得到的员工信息

	@Autowired
	EmployeeMapper employeeMapper;
	
	@Autowired
	RedisTemplate<String, Object> redisTemplate;
	
	@Test
	public void contextLoads() {
		// redis中存放对象
		String key = "emp";
		Employee empById = employeeMapper.getEmpById(1);
		redisTemplate.opsForValue().set(key, empById);
		Employee employee = (Employee)  redisTemplate.opsForValue().get(key);
		System.out.println(employee);
	}

发生错误,redisTemplate中默认的序列化没有把empById对象转换成功,需要自定义模板对象,修改默认的JdkSerializationRedisSerializer

@Configuration
public class MyRedisConfig {
	
	@Bean
	public RedisTemplate<Object, Employee> empRedisTemplate(RedisConnectionFactory redisConnectionFactory)
			throws UnknownHostException {
		RedisTemplate<Object, Employee> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
		template.setDefaultSerializer(serializer);
		return template;
	}
	
}

在自定义的Configuration中修改了默认的序列化方法并且指定了需要序列化的自定义类型Employee,Test类中的方法被修改为

	@Autowired
	RedisTemplate<Object, Employee> empRedisTemplate;

	@Test
	public void contextLoads() {
		// redis中存放对象
		String key = "emp";
		Employee empById = employeeMapper.getEmpById(1);
		empRedisTemplate.opsForValue().set(key, empById);
		Employee employee = empRedisTemplate.opsForValue().get(key);
		System.out.println(employee);
	}

执行成功,控制台打印了一号员工的信息
在这里插入图片描述

redis中保存了数据
在这里插入图片描述
这里有一个问题,redis的key带上了引号,很费解

还可以在MyRedisConfig中修改需要序列化的自定义对象新增Bean,再注入时,严格指定RedisTemplate中的范型,即可得到不同的结果,否则自动注入会报错

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.pc.springbootcache.SpringbootcacheApplicationTests’: Unsatisfied dependency expressed through field ‘redisTemplate’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.Object>’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这个错误是一开始时,我没有删掉最开始的“RedisTemplate<String, Object> redisTemplate;”导致的,消除这个错误,可以把这个注解删除或者将第一个范型的String改为Object

3.RedisCacheManager

引入redis的starter后spring boot自带cacheManager变成RedisCacheManager,使用的就是RedisTemplate<Object,Object>,使用的是默认的JDK序列化机制。
为方便使用,自定义CacheManager

@Configuration
public class MyRedisConfig {
	
	@Bean
	@Primary
	public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
		return RedisCacheManager.create(redisConnectionFactory);
	}

	@Bean
	public RedisCacheManager empCacheManager(RedisConnectionFactory redisConnectionFactory) {
		// RedisCacheWriter
		RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
		// CacheManager的值序列化方式为json序列化
		Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
		SerializationPair<Employee> pair = RedisSerializationContext.SerializationPair.fromSerializer(serializer);
		RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
				.serializeValuesWith(pair);
		// RedisCacheManager
		return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
	}
}

值得注意的是,自定义CacheManager指定范型会导致其他非Employee对象使用缓存,能够正常添加缓存,但是取出后再转换至原数据类型时报错,因此,需要将默认的RedisCacheManager写入Configuration中,并将其设置为@Primary。

	@Cacheable(cacheNames = "emp",cacheManager = "empCacheManager")
	public Employee getEmpById(Integer id) {
		System.out.println("查询" + id + "号员工");
		Employee employee = employeeMapper.getEmpById(id);
		return employee;
	}

使用Cacheable注解,将员工查询的缓存管理器(cacheManager)指定为empCacheManager。
同理,还可以自定义其他范型的cacheManager。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值