本文共 3930 字,大约阅读时间需要 13 分钟。
org.springframework.boot spring-boot-starter-data-redis
spring: redis: host: localhost port: 6379
package com.example.config;import org.springframework.context.annotation.Configuration;import java.net.UnknownHostException;import java.time.Duration;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.context.annotation.Bean;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;@Configurationpublic class MyRedisConfg extends CachingConfigurerSupport { /** * 配置redisCacheManager存放缓存的格式 * @param factory * @return */ @Bean("redisCacheManager") public RedisCacheManager redisCacheManager(RedisConnectionFactory factory){ System.out.println("加载Redis.."); RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofDays(1)) .disableCachingNullValues() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build(); } /** * 配置RedisTemplate存放缓存的格式 * @param factory * @return */ @Bean public RedisTemplate
@Autowired RedisCacheManager deptCacheManager; public Department getDeptById(Integer id){ System.out.println("查询部门"+id); Department department = departmentMapper.getDeptById(id); //获取某个缓存 Cache dept = deptCacheManager.getCache("dept"); dept.put("dept:1",department); return department; }
@Autowired @Qualifier("redisCacheManager") RedisCacheManager redisCacheManager; @Autowired StringRedisTemplate stringRedisTemplate; @Test public void method(){ //获取value Cache cache = redisCacheManager.getCache("category"); //添加缓存 cache.put("cc", categoryDao.getOne(1)); //很奇怪, 取不出来? //因为 redisCacheManager 取不出来缓存, 所以我用 stringRedisTemplate 去取, 取完 //需要将其转化成对象 System.out.println(stringRedisTemplate.opsForValue().get("category::cc")); }
注意:User对象在储存时要实现序列化, 否则可能会报错。
@Cacheable(cacheNames = { "emp"}, key="#id"/*, keyGenerator="cacheKey", condition = "#id>1 and #root.methodName eq 'getEmp' ", unless = "#id==2"*/) public Employee getEmp(int id){ System.out.println("查询..."); return employeeMapper.get(id); }
运行一下被缓存注解标记的方法, 得到的缓存也会储存在Redis数据库中。
转载地址:http://yyern.baihongyu.com/