EhCache is a pure Java in-process caching framework, with fast and lean features, and is the default CacheProvider in Hibernate.
ehcache provides a variety of caching strategies, mainly divided into memory and disk levels, so there is no need to worry about capacity issues.
spring-boot is a fast integration framework designed to simplify the initial construction and development process of new Spring applications. The framework uses a specific way to configure it, so that developers no longer need to define boilerplate configurations.
After the user logs in, almost any page displayed after displaying it, you need to display user information. You can cache the user information after the user logs in successfully, and then directly retrieve the cached data.
Write in front
For SpringBoot, it has its own cache framework built-in, namely:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId></dependency>
How to use this framework for caching, there is a lot of information online. However, most of them describe how to cache at the service layer, and then directly get the cached data when fetching data from the database, and no longer conduct secondary queries. But my current requirement is to cache logged in users, and I will use the code to retrieve it next time. Instead of adjusting the service method again! It seems that any information was found! Maybe no one has encountered it.
Explore the plan
In previous projects, EHcache has been used, and of course there is also the corresponding EHCacheUtils method. Let’s talk about the integration of springboot and ehcache.
After real operation, I found that integrating ehcache is very simple, and there are three steps:
Importing maven package
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.1</version></dependency>
Enable cache mechanism in startup class
image.png
Add the ehcache configuration file and create a new ehcache.xml file under resources. The file configuration is as follows:
<?xml version="1.0" encoding="UTF-8" ?><ehcache> <cache name="objectCache" maxElementsInMemory="1000"></cache></ehcache>
Use cache
At this time, springboot has generated a bean for ehcacheManager. We can inject directly where we need it. Below is my simple utils file and the place to store and retrieve data, for reference only. If there is something wrong, please correct me.
Util is as follows, only set and get methods are provided:
public class EHCacheUtils { /** * Set cache object* @param cacheManager * @param key * @param object */ public static void setCache(CacheManager cacheManager,String key,Object object){ Cache cache = cacheManager.getCache("objectCache"); Element element = new Element(key,object); cache.put(element); } /** * Remove object from cache* @param cacheManager * @param key * @return */ public static Object getCache(CacheManager cacheManager,String key){ Object object = null; Cache cache = cacheManager.getCache("objectCache"); if(cache.get(key)!=null && !cache.get(key).equals("")){ object = cache.get(key).getObjectValue(); } return object; }}The method of depositing cache is as follows:
@Autowiredprivate CacheManager cacheManager;//Some key codes EHCacheUtils.setCache(cacheManager,"op", searchOP);
The method of removing the cache is as follows:
@Autowiredprivate CacheManager cacheManager;Operator searchOP = (Operator) EHCacheUtils.getCache(cacheManager,"op");
Written at the end
Personally, I think that the annotation-based method provided by springboot is more suitable for interceptor processing. The service method with a cache mechanism is used to set up a service method for the specified request. The service will directly retrieve the cache or query the database according to the actual situation.
Summarize
The above is the practical plan of springboot integrating EHCache introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!