Initial cache solution (2)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.