Ehcache source code analysis:
First, check the cache class cachemanager.
Public class cachemanager {// This class reads ehcache under classpath by default. XML file, and a new cache class is created in singleton mode/*** keeps track of all known cachemanagers. used to check on conflicts. * cachemanagers shocould remove themselves from this list during shut down. */public static final list all_cache_managers = collections. synchronizedlist (New arraylist (); Private Static final log = logfactory. getlog (cachemanager. class. getname ();/*** the singleton instance. */Private Static cachemanager Singleton;/*** caches managed by this manager. */protected final map caches = new hashmap ();/*** default cache. */private ehcache defaultcache;
Cachemanager manager = new vachemanager ();
To create a new one, you must: manager. Create ();
public static CacheManager create() throws CacheException { synchronized (CacheManager.class) { if (singleton == null) { if (LOG.isDebugEnabled()) { LOG.debug("Creating new CacheManager with default config"); } singleton = new CacheManager(); } else { if (LOG.isDebugEnabled()) { LOG.debug("Attempting to create an existing singleton. Existing singleton returned."); } } return singleton; } }
By default, this method creates a singleton object cache Manager using the factory method.
You can also get the instance through getinstance ().
public static CacheManager getInstance() throws CacheException { return CacheManager.create(); }
Obtain the cache object based on the cache name
public synchronized Cache getCache(String name) throws IllegalStateException, ClassCastException { checkStatus(); return (Cache) caches.get(name); }
The cache stores data in a map set.
public synchronized Ehcache getEhcache(String name) throws IllegalStateException { checkStatus(); return (Ehcache) caches.get(name); }
To obtain ehcache
How is the cache stored? Check the following code.
public synchronized void addCache(String cacheName) throws IllegalStateException, ObjectExistsException, CacheException { checkStatus(); //NPE guard if (cacheName == null || cacheName.length() == 0) { return; } if (caches.get(cacheName) != null) { throw new ObjectExistsException("Cache " + cacheName + " already exists"); } Ehcache cache = null; try { cache = (Ehcache) defaultCache.clone(); } catch (CloneNotSupportedException e) { LOG.error("Failure adding cache. Initial cause was " + e.getMessage(), e); } if (cache != null) { cache.setName(cacheName); } addCache(cache); }
Use the addcache () method to determine whether the cachename already exists. If it does not exist, clone () to the cache in the default cache, that is, the map. If it exists, it will not be processed.
The above is my understanding of ehcache and will be further supplemented in the future.
Initial cache solution (2)