![](https://oscimg.oschina.net/oscnet/78e1ae13-d719-4a16-82a5-0404e61d88b7.gif)
-
The logic of using the cache is very general. Basically, the cache is checked first, and if there is any, it is returned directly without checking the DB, and then placed in the cache. This general logic is scattered throughout the system, violating the principle of high cohesion and low coupling. -
Caching code and business logic code are deeply coupled together, which not only reduces the readability of the code, but also increases the complexity of the system. -
If you want to switch the cache (MDB->LDB) or upgrade the API, all involved codes need to be changed. -
If you want to solve common problems such as cache penetration, cache penetration, cascading cache, etc., you need to solve them through the framework.
![](https://oscimg.oschina.net/oscnet/6e5d0ab0-0742-4001-a888-68fb5dece5ab.png)
Read the cache data first, and return directly if there is data. If no data is read, read the DB data, and then update the cache after the data is returned.
This scenario is very common in daily coding and is too simple, but the actual code is indeed very different. Here are some examples:
▐Traditional writing
Whatever cache is used, use it directly and embed it into the business code. This kind of code is something I don’t want to see whether it’s for code review or when future generations learn business code. The reason is very simple and has nothing to do with actual business functions. I don’t want to know what cache you use or how you code the cache code.
▐A more advanced way of writing
Compared with the traditional writing method, in order to solve the problem of caching various data formats (List, Map, etc.) and various object serialization (java, json), the team can encapsulate the caching into a simple API for everyone to use. It is easier to use, but the code is still embedded in the business code and has not been stripped out.
![](https://oscimg.oschina.net/oscnet/124e61e9-e9d7-4386-ab5b-f5b1f1cdebd9.png)
▐How to write annotations
![](https://oscimg.oschina.net/oscnet/154cd560-267b-4ea1-a8c6-3dfd788eec63.png)
![](https://oscimg.oschina.net/oscnet/35959258-5f85-4dd3-ac4e-c1839daacfb0.png)
Spring cache uses dynamic proxy to process cache-related operations in the proxy class, and at the same time calls methods in the proxy class, so that the code that operates the cache and the business code can be separated, and when the cache capability needs to be strengthened later, only Just modify the method in the proxy class.
▐Code directory
![](https://oscimg.oschina.net/oscnet/fdeeaf07-1701-4ae9-8c6e-af2cb60ec362.png)
▐Annotation Map
▐Annotation usage example
@Cacheable(value = "user_cache", unless = "#result == null")
public User getUserById(Long id) {
return userMapper.getUserById(id);
}
@CachePut(value = "user_cache", key = "#user.id", unless = "#result == null")
public User updateUser(User user) {
userMapper.updateUser(user);
return user;
}
@CacheEvict(value = "user_cache", key = "#id")
public void deleteUserById(Long id) {
userMapper.deleteUserById(id);
}
▐Program analysis
-
Multi-level cache; -
The cache is refreshed regularly; -
list cache; -
Cache cpp protection mechanism; -
Cache count.
![](https://oscimg.oschina.net/oscnet/a62929d0-832f-45ca-aa02-c4fcdafd5fb2.png)
![](https://oscimg.oschina.net/oscnet/19101a43-02bc-46ac-b767-a85f61e2b1a6.png)
Learn the spring cache framework solution and implement a custom cache framework, which not only retains the advantages of the spring cache framework, but also realizes many missing capabilities of spring cache, such as cache breakdown, cache penetration protection, multi-level cache, etc.
▐Annotation code example
![](https://oscimg.oschina.net/oscnet/99881df2-a99b-40f1-8ef7-40b0f468b6ce.png)
▐Project structure
![](https://oscimg.oschina.net/oscnet/7a140a68-c3b1-45a3-9df6-dcb516771f8a.png)
![](https://oscimg.oschina.net/oscnet/94212b79-92ff-4fd0-b493-4aef08511f62.png)
With the help of spring cache implementation, we build a custom cache framework and extend many annotations, such as counting, cache refresh, list cache, distributed lock, multi-level cache, etc., which not only realizes the separation of cache code and business code, but also expands the spring The ability to cache greatly improves the readability of the code and reduces the efficiency of cached code maintenance.
![](https://oscimg.oschina.net/oscnet/46c3e541-d542-41b4-a643-e93d6a0f11d9.png)
The mission of Tmall Automotive Technology Team is to experience the ultimate life of people and cars, reshape the automotive industry, and be a caring car steward around you. They are all building consumers' minds for online car viewing, buying and maintaining cars, digitizing and vertically integrating the automotive industry, and through Model breakthroughs leverage the integration of product and effect, improve industry efficiency, and create industry dividends.
Server-side technology | Technical quality | Data algorithm
This article is shared from the WeChat public account - Big Taobao Technology (AlibabaMTT).
If there is any infringement, please contact [email protected] for deletion.
This article participates in the " OSC Source Creation Plan ". You who are reading are welcome to join and share together.