EhCache is a pure Java in-process caching framework, which is fast and capable, and is the default Cacheprovider in Hibernate.
We use the Ehcache cache framework mainly to determine the duplicate URL, each crawl a page, the URL is stored in the cache, and each crawl a page before the cache to search under, if there is, we do not crawl this page, does not exist, we crawl down the page, crawl success, This URL is stored in the cache, the reason is that the cache framework is fast, relative to the traditional database;
The main features are:
1. Fast
2. Simple
3. Multiple Cache policies
4. Cache data has two levels: memory and disk, so there is no need to worry about capacity issues
5. Cache data is written to disk during virtual machine restart
6. Distributed caching can be done via RMI, pluggable APIs, etc.
7. Listening interface with cache and cache manager
8. Support multiple cache manager instances, and more than one instance of the cache area
9. Provide Hibernate cache implementation
Project file Structure
Download Jar Package---paste Jar Package--Introduction jar Package
Ehcache.xml
<?xml version= "1.0" encoding= "UTF-8"?> <ehcache> <!-- disk storage: Transfer objects temporarily unused in the cache to the hard disk, Virtual memory similar to Windows system path : Specifies the path to the storage object on the hard disk- <diskstore path= "D:\ehcache"/> <!-- Defaultcache: Default cache configuration information, if not specified, all objects are processed according to this configuration item maxelementsinmemory: Sets the upper limit of the cache and stores the maximum number of record objects Eternal: Represents whether an object never expires Overflowtodisk: When the number of element in memory reaches Maxelementsinmemory, Ehcache will write the element to disk- < Defaultcache maxelementsinmemory= "eternal=" true " overflowtodisk=" true "/> <cache name= "A" maxelementsinmemory= "eternal=" true " overflowtodisk=" true "/> </ehcache >
The code is as follows:
Package Com.zhi.ehcache;import Net.sf.ehcache.cache;import Net.sf.ehcache.cachemanager;import Net.sf.ehcache.element;public class Demo {public static void main (string[] args) {//Create cache manager based on ehcache.xml configuration file CacheManager manager=cachemanager.create ("Ehcache.xml"); Cache C=manager.getcache ("a"); Gets the specified cache element e=new element ("Oracle", "Zhi");//Instantiate an element C.put (e);//Add an element to the cache in element e2= C.get ("Oracle"); Gets the cache element System.out.println (E2) based on key; System.out.println (E2.getobjectvalue ()); C.flush (); Flush Cache Manager.shutdown ();//close Cache Manager}}
Use of the Ehcache cache framework