Cache — — get and set of Cache instances

Posted by stebut05 on Fri, 10 May 2019 06:28:03 +0200

Ehcache is configured in Spring, created by EhCache Manager FactoryBean and enabled a Cache Manager instance to manage Cache. How does Cache Manager manage Cache? Who created Cache?

Org. spring framework. cache. CacheManager: source code

public interface CacheManager {
    @Nullable
    Cache getCache(String var1);

    Collection<String> getCacheNames();
}

We can see that the Cache Manager created in Spring contains only two methods, and there is no way to create cache instances. Where was the cache created?

Where is the net.sf.ehcache.CacheManager class used?

Simple Inheritance Structure Diagram: Only Partial Methods and Classes

(1) From the inheritance structure diagram above, we can see that Spring loads the Eh CacheManager class and thus obtains an instance of the Cache Manager interface, which can be obtained by Cache getCache(String var1) method.

For example:

public class DemoTest {
    private  net.sf.ehcache.Cache customizedTTLCache;

    @Autowired
    public LoginServiceImpl(CacheManager customizedTTLCache) {
      
        this.customizedTTLCache =
                (net.sf.ehcache.Cache) ((EhCacheCache)
                         customizedTTLCache.getCache(CacheConstant.CUSTOMIZED_TTL)).getNativeCache();
}

(2) The implementation of Cache getCache (String var1) method is in the AbstractCache Manager class, and AbstractCache Manager implements the InitializingBean interface, which automatically executes afterPropertiesSet() when the class is initialized.

public void afterPropertiesSet() {
        this.initializeCaches();
    }
public void initializeCaches() {
        Collection<? extends Cache> caches = this.loadCaches();
        ConcurrentMap var2 = this.cacheMap;
        synchronized(this.cacheMap) {
            this.cacheNames = Collections.emptySet();
            this.cacheMap.clear();
            Set<String> cacheNames = new LinkedHashSet(caches.size());
            Iterator var4 = caches.iterator();

            while(var4.hasNext()) {
                Cache cache = (Cache)var4.next();
                String name = cache.getName();
                this.cacheMap.put(name, this.decorateCache(cache));
                cacheNames.add(name);
            }

            this.cacheNames = Collections.unmodifiableSet(cacheNames);
        }
    }
 protected abstract Collection<? extends Cache> loadCaches();

In initializeCaches(), the implementation of the template method pattern (design pattern) - loadCaches() method is handed over to specific subclasses, which roughly means that we need to get the set of Cache, where the set of Cache comes from, and whatever the specific implementation class of Cache is.

(3) EhCache Cache Manager implements the Collection < Cache > loadCaches () method

In this case, net.sf.ehcache.CacheManager is used. Is Cache created in the CacheManager class?

protected Collection<Cache> loadCaches() {
        CacheManager cacheManager = this.getCacheManager();
        Assert.state(cacheManager != null, "No CacheManager set");
        Status status = cacheManager.getStatus();
        if (!Status.STATUS_ALIVE.equals(status)) {
            throw new IllegalStateException("An 'alive' EhCache CacheManager is required - current cache is " + status.toString());
        } else {
            String[] names = this.getCacheManager().getCacheNames();
            Collection<Cache> caches = new LinkedHashSet(names.length);
            String[] var5 = names;
            int var6 = names.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                String name = var5[var7];
                caches.add(new EhCacheCache(this.getCacheManager().getEhcache(name)));
            }

            return caches;
        }
    }

In the loadCaches() method, two methods in the CacheManager class, getCacheNames() and getEhcache(name), are used, but get only the existing cache in the next cacheManager. How did that existing cache be created?

(4) The init (Configuration Initial Configuration, String Configuration FileName, URL Configuration URL, InputStream Configuration InputStream) method of the CacheManager class executes and initializes the encapsulated properties when the bean loads, and calls the

The focus of the doInit(Configuration configuration) method is here, under layer after layer of wrapping, finally found here!!!

An instance of Configuration Helper is created and used in the doInit() method: Here we finally see the call to new Cache().

There are several methods to create Cache instances in the Configuration Helper class, and they are interoperable.

Ultimately, it was created by Ehcache CreateCache (Cache Configuration Cache Configuration).

Topics: Ehcache Spring