Spring abstracts the CacheManager interface for various cache implementations, and users use this interface to process caches without caring about the underlying implementation. It can also be conveniently changed to the specific implementation of the cache without modifying the business code. Here is a brief introduction to using cache in springboot:
1. Add dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
2. Turn on cache in the configuration class, as shown in the figure below:
3. Add annotations to the methods that need to be used, as follows:
@Override //@CachePut This annotation will cache the return value of the method, where the cache name is people and the key of the data is the id of the person @CachePut(value = "people", key = "#person.id") public Person save(Person person) { Person p = personRepository.save(person); System.out.println("For id, key is:"+p.getId()+"Data cached"); return p; } @Override //@CacheEvict This annotation will delete the data with the key id in the people cache @CacheEvict(value = "people", key = "#id") public void remove(Long id) { System.out.println("Remove the data cache with id and key "+id+""); //The actual deletion operation is not performed here} @Override //@Cacheable This annotation will determine whether the cache with the key #person.id in the cache people exists when the method is executed. If it exists, it will directly return the data in the cache. If it does not exist, the database will be checked and the return result will be cached. @Cacheable(value = "people", key = "#person.id") public Person findOne(Person person) { Person p = personRepository.findOne(person.getId()); System.out.println("For id, key is:"+p.getId()+"Data cached"); return p; }The above parts have completed the cache, but the current cache is memory-based by default and has not been persisted. The following is a specific implementation of redis as cache, as follows:
4. Add dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
5. Add redis configuration in the configuration file
redis.hostname=localhost redis.port=6379
6. Configure redis in spring container
@Configuration public class RedisConfig extends CachingConfigurerSupport{ private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class); @Autowired private Environment env; @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName(env.getProperty("redis.hostname")); redisConnectionFactory.setPort(Integer.parseInt(env.getProperty("redis.port"))); return redisConnectionFactory; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(cf); return redisTemplate; } @Bean public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(600); return cacheManager; } }OK, it's done, there's no need to change anything else, isn't it very convenient?
In addition, the classes to be cached must be serialized.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.