Ehcache is a cache framework used to configure and manage cache. We analyze from its ehcache. xml file how it inserts data into memory and hard disk.
<?xml version="1.0" encoding="UTF-8"?><ehcache><diskStore path="java.io.tmpdir" /><defaultCache maxElementsInMemory="500" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="1200" overflowToDisk="true" /><cache name="testcache" maxElementsInMemory="150" eternal="false" timeToLiveSeconds="36000" timeToIdleSeconds="3600" overflowToDisk="true"/> </ehcache>
The above
<diskStore path="java.io.tmpdir" />
This is to configure the path for storing cached files to the hard disk. It points to a temporary file storage path of the operating system. You can obtain the path information through system. getproperty ("Java. Io. tmpdir.
Maxelementsinmemory = 500 this is the maximum number of objects configured in the memory
Overflowtodisk = "true" when it exceeds 500, it will be saved to the path configured in the hard disk.
You can write a code segment to change the number of cycles: 100,400,200 0 test. check whether there are any files under the directory:
Package COM. xxinfo. ylzcache; import Org. apache. log4j. logger; import net. SF. ehcache. cache; import net. SF. ehcache. cachemanager; import net. SF. ehcache. element; public class ehcachetest {Private Static final logger = logger. getlogger (ehcachetest. class); Private Static cache samplecache = NULL; public static void main (string [] ARGs) {Init (); test ();} Private Static void test () {logger.info (samplecache. getmemorystoreevictionpolicy (); For (INT I = 0; I <2000; I ++) {// write cache samplecache. put (new element (I, "V" + I); // print all logger.info (samplecache. getkeys (); // read the cache Element E = samplecache. get (I); logger.info (E. getvalue ();} // print the logger.info (samplecache. getstatistics (); cachemanager. getinstance (). shutdown ();} Private Static void Init () {cachemanager manager = cachemanager. create (); samplecache = manager. getcache ("testcache ");}}