A brief introduction of cache preheating mechanism in Ehcache
Ehcache does not immediately load the data on disk into memory when the program is started, but loads it when the data is used (lazy load). Therefore, there is no data inside the cache when it is started. What should we do if we want to use these data before they are all loaded into memory?
Ehcache provides a bootstrapcacheloader mechanism to solve this problem, which will be run before the cache is activated. And there are two modes: synchronous and asynchronous. In the case of synchronous mode, the load will be completed before Cachemana starts, and in the case of asynchronous mode, the load will continue in the background when the CacheManager is started, rather than waiting for the required data to be required.
Specific implementation
We just need to implement the interface Bootstrapcacheloader define our own loader mybootstrapcacheloader, Inherit bootstrapcacheloaderfactory implement a concrete loading factory mybootstrapcacheloaderfactory can realize the preheating of data.
Mybootstrapcacheloader is responsible for implementing how the data is loaded into the cache, and we can personalize the implementation. Mybootstrapcacheloaderfactory is a concrete loading factory, responsible for creating loader instances, and we need to implement some abstract methods.
Here's a look at the specific code implementation (Java).
Mybootstrapcacheloader.java:
/** * * Copyright (c) 2004-2014 all rights Reserved. */package com. Test.encache;import Java.util.list;import Net.sf.ehcache.cacheexception;import Net.sf.ehcache.Ehcache;import Net.sf.ehcache.element;import Net.sf.ehcache.bootstrap.bootstrapcacheloader;import Org.slf4j.Logger;import org.slf4j.loggerfactory;/** * * @author * @version $Id: Custombootstrapcacheloader.java, v 0.1 October 18, 2014 a.m. 10:57:26 E XP $ */public Class Mybootstrapcacheloader implements Bootstrapcacheloader {private static final Logger Logger = Logge Rfactory. GetLogger (Mybootstrapcacheloaderfactory.class); Statesdao Statesdao; Boolean asynchronous; /** * @see net.sf.ehcache.bootstrap.bootstrapcacheloader#load (net.sf.ehcache.Ehcache) */public void load (EHCA Che cache) throws Cacheexception {logger.info ("load your cache with whatever you want ....."); List keys = Cache.getkeys (); for (int i = 0; I < keys.size (); i++) {logger.info ("keys->" + keys.get (i)); try {list<string> dataList = Getstatesdao (). Findallstates (); Cache.put (New Element (Cacheconstants.key_array[0], datalist.get (0))); Cache.put (New Element (Cacheconstants.key_array[1], Datalist.get (1))); } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); } logger.info ("Load End ..."); }/** * @see net.sf.ehcache.bootstrap.bootstrapcacheloader#isasynchronous () */public Boolean Isasynchronou S () {return asynchronous; }/** * @see java.lang.object#clone () */@Override public Object Clone () throws Clonenotsupportedexcepti On {return super.clone (); } public Statesdao Getstatesdao () {return statesdao; } public void Setstatesdao (Statesdao statesdao) {This.statesdao = Statesdao; }/** * Setter method For property <tt>asynchronous</tt>. * * @param asynchronous value to being assigned to property asynchronous */public void setasynchronous (Boolean a Synchronous) {this.asynchronous = asynchronous; }}
Mybootstrapcacheloaderfactory.java
/** * * Copyright (c) 2004-2014 all rights Reserved. */package Com.test.encache;import Java.util.properties;import Net.sf.ehcache.bootstrap.BootstrapCacheLoader; Import Net.sf.ehcache.bootstrap.bootstrapcacheloaderfactory;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import org.springframework.beans.factory.annotation.autowired;/** * Bootstrap Cache Loader * * @author * @version $Id: Mybootstrapcacheloaderfactory.java, v 0.1 October 17, 2014 PM 8:02:55 Exp $ */public Class Mybootstr Apcacheloaderfactory extends Bootstrapcacheloaderfactory {private final String Asynchronous_property_key = "Asynchrono US "; @Autowired private Statesdao Statesdao; Public Mybootstrapcacheloaderfactory () {super (); TODO auto-generated Constructor stub} private static final Logger Logger = loggerfactory . GetLogger (Mybootstrapcacheloaderfactory.class); @Override public Bootstrapcacheloader Createbootstrapcacheloader (PrOperties properties) {logger.info ("mybootstrapcacheloaderfactory:create a Bootstrapcacheloader"); Mybootstrapcacheloader loader = new Mybootstrapcacheloader (); Statesdao = new Statesdao (); Loader.setstatesdao (Statesdao); Loader.setasynchronous (Getasyncfromproperty (properties)); return loader; } Private Boolean Getasyncfromproperty (Properties properties) {String asynchronous = Properties.getproperty (ASY Nchronous_property_key); Return boolean.valueof (asynchronous); }}
Using the cache's method of reading the disk data in the Statesdao class, we just simulated it and read the data from the database.
Statesdao.java
/** * * Copyright (c) 2004-2014 All rights Reserved. */package com.test.encache;import Java.util.arraylist;import JAV A.util.list;import com.googlecode.ehcache.annotations.cacheable;/** * * @author * @version $Id: Statesdao.java, v 0.1 October 17, 2014 PM 8:07:05 Exp $ */public class Statesdao { //annotation based caching and the NA Me of the cache should is defined in Ehcache.xml. @Cacheable (cachename = "Statecache") public list<string> findallstates () { list<string> dataList = new arraylist<string> (); Your call to database a returns a list of objects Datalist.add ("value1"); Datalist.add ("value2"); return dataList;} }
Cache configuration Myehcache.xml:
<ehcache><diskstore path= "D://localcache"/><cache name= "Statecache" maxEntriesLocalHeap= "10000" Maxentrieslocaldisk= "eternal=" false "diskspoolbuffersizemb=" timetoidleseconds= "timetoliveseconds=" "Memorystoreevictionpolicy=" LFU "transactionalmode=" Off "><bootstrapcacheloaderfactory class=" Com.test.encache.MyBootstrapCacheLoaderFactory " properties=" Asynchronous=true "/></cache><cache Name= "StateCache2" maxentrieslocalheap= "10000" maxentrieslocaldisk= "" "Eternal=" false "diskspoolbuffersizemb=" "Timetoidleseconds=" "timetoliveseconds=" "memorystoreevictionpolicy=" LFU "transactionalmode=" Off "> <bootstrapcacheloaderfactory class= "com.test.encache.MyBootstrapCacheLoaderFactory" properties= " Asynchronous=false "/></cache></ehcache>
CacheManager Configuration Applicationcontext.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "Xmlns:ehcache=" http://ehcache-spring-annotations.googlecode.com/svn/ Schema/ehcache-spring "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/ Schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ Ehcache-spring-1.1.xsd "default-autowire=" Bytype "> <ehcache:annotation-driven cache-manager=" EhCacheManager "/> <bean id=" Ehcachemanager "class=" Org.springframework.cache.ehcache.EhCacheManagerFactoryBean "> < ProperTy name= "configlocation" > <value>src/config/myehcache.xml</value> </property></bean> </beans>
Finally, the test code: Bootstrapcacheloadertest.java:
/** * * Copyright (c) 2004-2014 all rights Reserved. */package Com.test.encache;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.util.properties;import Net.sf.ehcache.cache;import Net.sf.ehcache.CacheManager; Import Net.sf.ehcache.element;import Org.apache.log4j.propertyconfigurator;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.context.applicationcontext;import org.springframework.context.support.filesystemxmlapplicationcontext;/** * * @author * @version $Id: Bootstrapcacheloader.java, v 0.1 October 18, 2014 morning 11:31:06 Exp $ */public class Bootstrapcacheloadertest {private static String log4jfilename = "Src/config/log4j.properties"; private static String XMLfileName = "Src/config/applicationcontext.xml"; private static String Ehcachexmlfilename = "Src/config/myehcache.xml"; private static final Logger Logger = loggerfactory . GetLogger (Bootstrapcacheloadertest.class); private static CacheManager Ehcachemanager; public static void Main (string[] args) {configproperty (); Xmlload (Ehcachexmlfilename); string[] cachenamesstrings = Ehcachemanager.getcachenames (); Logger.info ("The number of caches in Ehcachemanager:" + cachenamesstrings.length); Cache cache = Ehcachemanager.getcache (cacheconstants.cache_name1); Element element = Cache.get (cacheconstants.key_array[0]); Logger.info ("The element of key" + cacheconstants.key_array[0] + "is" + element); }/** * Config properties * */private static void Configproperty () {Properties Properties = NE W Properties (); FileInputStream IStream; try {istream = new FileInputStream (log4jfilename); Properties.load (IStream); Istream.close (); } catch (FileNotFoundException e) {logger.error ("File not Found ", e); } catch (IOException e) {logger.error ("Load file Erroe", e); } finally {}//properties.setproperty ("Log4j.appender.file.File", logFile); Propertyconfigurator.configure (properties); Logger.info ("Config properties success."); } private static void Xmlload (String fileName) {ApplicationContext ctx = new Filesystemxmlapplicationcontext (XM Lfilename); Ehcachemanager = (CacheManager) ctx.getbean ("Ehcachemanager"); }}
Output Result:
2014-10-18 15:17:45 INFO bootstrapcacheloadertest:71-config Properties success.
2014-10-18 15:17:45 INFO filesystemxmlapplicationcontext:513-refreshing org.[email protected]687b6889:startup Date [S At Oct 15:17:45 CST 2014]; Root of context Hierarchy
2014-10-18 15:17:45 INFO xmlbeandefinitionreader:316-loading XML Bean definitions from file [D:\code\test\encache\src\c Onfig\applicationcontext.xml]
2014-10-18 15:17:45 INFO ehcachemanagerfactorybean:136-initializing EhCache CacheManager
2014-10-18 15:17:46 INFO Mybootstrapcacheloaderfactory:38-mybootstrapcacheloaderfactory:create A Bootstrapcacheloader
2014-10-18 15:17:46 INFO Mybootstrapcacheloaderfactory:38-mybootstrapcacheloaderfactory:create A Bootstrapcacheloader
2014-10-18 15:17:46 INFO mybootstrapcacheloaderfactory:34-load your cache with whatever you want ....
2014-10-18 15:17:46 INFO mybootstrapcacheloaderfactory:51-load end ....
2014-10-18 15:17:46 INFO mybootstrapcacheloaderfactory:34-load your cache with whatever you want ....
2014-10-18 15:17:46 INFO mybootstrapcacheloaderfactory:51-load end ....
2014-10-18 15:17:46 INFO bootstrapcacheloadertest:43-the number of caches in Ehcachemanager:2
2014-10-18 15:17:46 INFO bootstrapcacheloadertest:46-the element of key KEY1 is [key = KEY1, value=value1, Version=1, Hitcount=1, CreationTime = 1413616666238, LastAccessTime = 1413616666302]
Resources
Ehcache official documentation on the preheating mechanism
Load EhCache diskstore content into memory
How to load data to Ehcache when the application starts
Cache warm-up mechanism and code implementation of Java cache Ehcache-ehcache (cache warming for multi-tier Caches)