In the previous chapter, I explained Spring Boot integrates Spring Cache Spring Cache has completed the implementation of various caches, including EhCache, RedisCache, ConcurrentMapCache, etc.
In this section, let's take a look at Spring Cache using RedisCache.
I. demonstration of RedisCache
Redis is a key value storage system, which is widely used in web applications. It is not described too much here.
An example of this chapter is in Spring Boot integrates Spring Cache Based on the source code of the transformation. Source address: https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache
Using RedisCache as the cache, we first introduce related dependencies.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--redis rely on--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
Then configure redis in the spring boot configuration file.
server: port: 10900 spring: profiles: active: dev redis: host: localhost #redis server address port: 6379 #redis port password: 1234 #redis password timeout: 60000 #Connection timeout database: 0 #Database index, default is 0
Redis is used in spring boot, which can be realized by Spring Cache annotation or RedisTemplate. In most cases, because annotation has some limitations and is not flexible enough, RedisTemplate is generally used in actual development.
Attach CacheConfig to inject RedisTemplate. If you don't need to use RedisTemplate, simply add @ EnableCaching annotation to the SpringBoot startup class.
@Configuration @EnableCaching public class CacheConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); // Use Jackson2JsonRedisSerializer to serialize and deserialize the value value of redis (JDK is used by default) Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper); redisTemplate.setValueSerializer(serializer); // Use StringRedisSerializer to serialize and deserialize the key value of redis redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
In this way, RedisCache can be used to test the code and Spring Boot integrates Spring Cache Agreement.
The CacheApi interface calls the class, which is convenient to call for testing.
@RestController @RequestMapping("cache") public class CacheApi { @Autowired private CacheService cacheService; @GetMapping("get") public User get(@RequestParam int id){ return cacheService.get(id); } @PostMapping("set") public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){ User u = new User(code, name); return cacheService.set(id, u); } @DeleteMapping("del") public void del(@RequestParam int id){ cacheService.del(id); } }
CacheService cache business processing class, add cache, update cache and delete.
@Slf4j @Service public class CacheService { private Map<Integer, User> dataMap = new HashMap <Integer, User>(){ { for (int i = 1; i < 100 ; i++) { User u = new User("code" + i, "name" + i); put(i, u); } } }; // get data @Cacheable(value = "cache", key = "'user:' + #id") public User get(int id){ log.info("adopt id{}Query acquisition", id); return dataMap.get(id); } // Update data @CachePut(value = "cache", key = "'user:' + #id") public User set(int id, User u){ log.info("To update id{}data", id); dataMap.put(id, u); return u; } //Delete data @CacheEvict(value = "cache", key = "'user:' + #id") public void del(int id){ log.info("delete id{}data", id); dataMap.remove(id); } }
Start the service for testing, you can see that the cache function is normal, and open redis for viewing, and you can also see the corresponding cache data.
Source address: https://github.com/imyanger/springboot-project/tree/master/p21-springboot-cache-redis