In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.