Spring itself does not implement a caching solution, but it provides declarative support for cache management capabilities that can be integrated with a variety of popular cache implementations .
The core idea of the Spring cache, which works on methods (which cannot be interpreted as annotations only), is that when we call a caching method, the method parameter and the return result are stored in the cache as a key value . The method is not executed until the next time the method is called with the same parameters, but instead it is fetched directly from the cache. So when using the spring cache we have to make sure that our caching methods have the same return result for the same method parameters .
1. Data that is suitable and not suitable for storage to level two cache
① fit
[1] often queried, rarely or hardly modified [2] is not particularly important, allowing occasional concurrency problems [3] that will not be modified by other applications
② not suitable for
[1] often modified [2] is particularly important and does not allow for any concurrency problems, such as: financial data [3] may be modified by other applications
2.Survey project suitable for storing data in level two cache ① the Res object queried when verifying permissions
Res Getresbyservletpath (String servletpath);
② participation in survey related methods
engageservice.pageinfo<survey> getsurveypage (integer userId, Boolean completed, Integer pagenum); Engageservice.survey getsurveydeeply (Integer Surveyid);
3. Using the spring-provided caching abstraction mechanism to integrate the Ehcache① of pseudo-code to demonstrate how a cache slice works
try {//1. Attempt to get data from cache Object value = Cachemap.get (key); 2. Determine if value is null if (value = = null) {//3. Actual execution of the target method value = target object. Target method (); 4. Cache the results of the target method execution Cachemap.put (key,value); }//5. Returns value return value;} catch (Exceptin e) {}
②spring cache abstraction Considerations [1] This mechanism cannot be used if the output of a method is not necessarily the same in the same case as the input.
int count = Userservice.getregistusercount (boolean active);
[2] The target method must be actually called every time, then it is not possible to use the mechanism using the caching abstraction mechanism after the target method will only be executed once the cache abstraction
4. Steps to use
① Creating a key generator class to implement the Org.springframework.cache.interceptor.KeyGenerator interface
The
corresponding bean needs to be configured in the Spring configuration file
② Introduction of Ehcache Environment
[1] Adding a jar package [2] introduces Ehcache's own configuration file, while creating a named cache area
③ Configuring cache abstraction for Ehcache integration in spring configuration files
Using the spring-provided caching abstraction mechanism to consolidate ehcache to provide level two caching for projects