Get the App
SLTechnology News&Howtos  ›  Development  › 

SpringBoot 2.x how to integrate Spring-cache

Shulou Source: shulou.com Published: 2022-06-03 17:24:23 11月13日 Update

This article will explain in detail how SpringBoot 2.X integrates Spring-cache, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

Introduction to Spring Cache

Spring 3.1 introduces annotation-based caching, which is essentially an abstraction of cache usage that can be achieved by adding a small number of annotations it defines to existing code.

Spring Cache interface is defined by cache component specification, including various operation sets of cache, and provides various xxxCache implementations, such as RedisCache, EhCacheCache,ConcurrentMapCache, etc.;

After the project integrates Spring Cache, every time a method that requires caching functionality is called, Spring checks whether the specified target method has been called for the specified parameter, and if so, obtains the result directly from the cache. If not, it calls the method and puts the result into the cache.

II. Introduction to cache annotation

For cache declarations, Spring's cache provides a set of java annotations:

@CacheConfig: Sets some common cache settings shared at the class level.

@Cacheable: Trigger cache writes.

@CacheEvict: Trigger cache cleanup.

@Caching groups multiple caching operations

@CachePut: Update cache (does not affect method execution).

@CacheConfig

@CacheConfig(cacheNames = "user")@Servicepublic class UserServiceImpl implements UserService {}

@Cacheable

If the key does not exist, query db and update the result to the cache.

If the key exists, query the cache directly.

//Add data to cache after querying database @Override @Cacheable(cacheNames = "cacheManager", key = "'USER:'+#id", unless = "#result == null") public getUser(Integer id) { return repository.getUser(id); }

@CachePut

//Update cache after modifying data @Override @CachePut(cacheNames = "cacheManager", key ="'USER:'+#updateUser.id", unless = "#result == null") public User updateUser(User updateUser) { return repository.save(updateUser); }

@CacheEvict

//Clear a cache, key is the data to be emptied @Override @CacheEvict(cacheNames = "cacheManager", key = "USER:'+#id") public void deleteUser(Integer id) { repository.deleteById(id); }

Spring Boot+Cache

1, pom.xml introduced jar package

org.springframework.boot spring-boot-starter-cache org.springframework.boot spring-boot-starter-data-redis

2. Add @EnableCaching annotation to startup class

@EnableCaching annotation is an annotation-driven cache management feature in the spring framework. When you use @EnableCaching annotation on a configuration class (@Configuration), it triggers a post processor that scans each spring bean to see if there is already a cache corresponding to the annotation. If found, a proxy is automatically created to intercept the method call and perform processing using the cached bean.

The startup code is as follows:

3. Configure database and redis connection

The application.properties section is configured as follows:

#Configure data source information spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://192.168.1.1:3306/testspring. datasource.username=rootspring.datasource.password=1234#configure jpaspring.jpa.hibernate.ddl-auto= updatepring. jpa.show-sql=truespring. jackl son.serialization.indent_output=true# Redis server address spring.redis.host= 192.168.1.1# databasespring.redis.database = 1# Redis server connection port Use default port 6379 can omit configuration spring.redis.port=@379#Redis server connection password (default empty) spring.redis.password=1234#connection pool maximum connections (if configured

Tags: Cache data annotations configuration methods queries databases updates services integration objects servers articles results parts code aspects functions sequences screenshots Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information Linux Redmi vpn