In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use SpringCache to cache database queries. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
SpringCache queries the cache database. 1. Add the annotation @ EnableCaching to the startup class of SpringBoot
Enable SpringCache cache support
@ SpringBootApplication// enables SpringCache cache support @ EnableCachingpublic class GatheringApplication {public static void main (String [] args) {SpringApplication.run (GatheringApplication.class, args) 2. Add the corresponding note to the service method / * according to the ID query * * @ param id * @ return * / / use SpringCache to query the cache database @ Cacheable (value = "gathering", key = "# id") public Gathering findById (String id) {return gatheringDao.findById (id). Get () } / * modify * * @ param gathering * / / delete @ CacheEvict (value = "gathering", key = "# gathering.id") public void update (Gathering gathering) {gatheringDao.save (gathering) in redis after modifying database data } / * * Delete * * @ param id * / / after deleting database data, you need to delete @ CacheEvict (value = "gathering", key = "# id") public void deleteById (String id) {gatheringDao.deleteById (id) in redis;} SpringCache database consistency problems cache and database inconsistency problems update the database first, and then update the cache
First update the database successfully, but failed to update redis, which caused the data in the database and Redis to be inconsistent.
Solution.
First update the cache, then update the database, when updating the cache, delete the cache first, then update the database, and then add the cache so that even if the cache update fails, the data in the cache will also be deleted. If deleting the cache fails, the database will not be updated and the exception will be thrown directly. At this time, if the latter one fails to add the cache, it will only add an operation to query the database (add him to the cache the next time you check this data)
Inconsistency between redis and database under high concurrency
If these two operations happen to be in the middle of the dotted line, then the data in the cache will be different from the data in the database. At this time, we have to introduce distributed locks to solve this problem.
Thank you for reading! This is the end of this article on "how to use SpringCache for cached database query". 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, you can share it out 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.