In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use spring cache cache for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Use of spring cache cache
Add schema and spring support for cache annotations to the spring configuration file:
Add the cache manager to the spring configuration file:
Then add the following comments to the impl layer of the service of the code to cache the data:
@ Cacheable (value= "bannerCache")
@ Cacheable means that spring will cache the data obtained by this method (caching is based on key-value), key is the parameter of the method, and value is the returned data. When you access the method continuously, you will find that you will only access the database for the first time. The other times are just query caches. It relieves the pressure on the database.
When you update the data in the database and need to invalidate the cache, use the following comments:
This annotation invalidates all data cached by appCache.
@ CacheEvict (value = "appCache", allEntries = true)
Springcache configure cache time to live
Spring Cache @ Cacheable itself does not support the setting of key expiration. The following code can customize the expiration that implements Spring Cache for Redis and SpringBoot2.0.
Go directly to the code:
@ Service@Configurationpublic class CustomCacheMng {private Logger logger = LoggerFactory.getLogger (this.getClass ()); / / specify the bean name @ Cacheable of the custom cacheManager (value = "test", key = "'obj1'", cacheManager = "customCacheManager") public User cache1 () {User user = new User (). SetId (1); logger.info ("1"); return user } @ Cacheable (value = "test", key = "'obj2'") public User cache2 () {User user = new User () .setId (1); logger.info ("2"); return user } / / Custom cacheManager to survive for 2 days @ Bean (name = "customCacheManager") public CacheManager cacheManager (RedisTemplate redisTemplate) {RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter (redisTemplate.getConnectionFactory ()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig () .entryTtl (Duration.ofDays (2)); return new RedisCacheManager (writer, config) } / / provides a default cacheManager, which applies to the global @ Bean @ Primary public CacheManager defaultCacheManager (RedisTemplate redisTemplate) {RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter (redisTemplate.getConnectionFactory ()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig (); return new RedisCacheManager (writer, config) }} this is the end of the article on "how to use spring cache cache". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.