Clover icon

Magnolia Module EhCache3 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:54:38 CET
  2. Package info.magnolia.module.cache.ehcache3

File EhCache3Factory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
55% of files have more coverage

Code metrics

10
31
7
1
197
131
13
0.42
4.43
7
1.86
27.3% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
EhCache3Factory 66 31 27.3% 13 5
0.895833389.6%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.cache.ehcache3;
35   
36    import info.magnolia.cms.core.Path;
37    import info.magnolia.init.MagnoliaConfigurationProperties;
38    import info.magnolia.init.MagnoliaInitPaths;
39    import info.magnolia.module.cache.Cache;
40    import info.magnolia.module.cache.CacheFactory;
41    import info.magnolia.module.cache.CacheModule;
42    import info.magnolia.module.cache.ehcache3.configuration.EhCache3Expiry;
43   
44    import java.io.Serializable;
45    import java.util.ArrayList;
46    import java.util.HashMap;
47    import java.util.List;
48    import java.util.Map;
49   
50    import javax.inject.Inject;
51   
52    import org.ehcache.CacheManager;
53    import org.ehcache.Status;
54    import org.ehcache.config.Builder;
55    import org.ehcache.config.CacheConfiguration;
56    import org.ehcache.config.builders.CacheConfigurationBuilder;
57    import org.ehcache.config.builders.CacheManagerBuilder;
58    import org.ehcache.config.builders.ResourcePoolsBuilder;
59    import org.ehcache.config.units.EntryUnit;
60    import org.slf4j.Logger;
61    import org.slf4j.LoggerFactory;
62   
63    /**
64    * A CacheFactory based on ehcache and which wraps cache instances.
65    */
 
66    public class EhCache3Factory implements CacheFactory {
67   
68    private static final Logger log = LoggerFactory.getLogger(EhCache3Factory.class);
69   
70    static final String MAGNOLIA_PROPERTY_CACHE_MANAGER_ID = "magnolia.cache.manager.id";
71   
72    private Map<String, Builder<? extends CacheConfiguration>> caches = new HashMap<>();
73    private final Map<String, CacheConfiguration> cachesConfiguration = new HashMap<>();
74    private CacheManager cacheManager;
75    // 0 == do not timeout ever, set to 10s by default.
76    private int blockingTimeout = 10000;
77    private String diskStorePath;
78   
79    private final CacheModule cacheModule;
80    private final MagnoliaInitPaths magnoliaInitPaths;
81    private final MagnoliaConfigurationProperties magnoliaConfigurationProperties;
82   
 
83  7 toggle @Inject
84    public EhCache3Factory(CacheModule cacheModule, MagnoliaInitPaths magnoliaInitPaths, MagnoliaConfigurationProperties magnoliaConfigurationProperties) {
85  7 this.cacheModule = cacheModule;
86  7 this.magnoliaInitPaths = magnoliaInitPaths;
87  7 this.magnoliaConfigurationProperties = magnoliaConfigurationProperties;
88  7 this.diskStorePath = Path.getCacheDirectoryPath() + getCacheManagerIdentifier();
89  7 getCaches().put(DEFAULT_CACHE_NAME,
90    CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
91    ResourcePoolsBuilder.newResourcePoolsBuilder()
92    .heap(10000, EntryUnit.ENTRIES)
93    ).withExpiry(new EhCache3Expiry<>())
94    );
95    }
96   
 
97  7 toggle public void init() {
98  7 this.cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
99    .with(CacheManagerBuilder.persistence(diskStorePath))
100    .build();
101    }
102   
 
103    toggle @Override
104    public void start(boolean isRestart) {
105    cacheManager.init();
106    }
107   
 
108    toggle @Override
109    public void stop(boolean isRestart) {
110    cacheManager.close();
111    }
112   
 
113    toggle @Override
114    public List<String> getCacheNames() {
115    return new ArrayList<>(cacheManager.getRuntimeConfiguration().getCacheConfigurations().keySet());
116    }
117   
 
118  7 toggle @Override
119    public Cache getCache(String name) {
120  7 Cache cache = null;
121  7 if (cacheManager.getStatus() == Status.AVAILABLE) {
122  7 final CacheConfiguration cacheConfiguration = getCacheConfiguration(name);
123  7 final org.ehcache.Cache ehcache = cacheManager.getCache(name, cacheConfiguration.getKeyType(), cacheConfiguration.getValueType());
124    // cacheManager will return null if there is no explicit cache configuration for this name
125  7 if (ehcache == null) {
126    // then we need to explicitly create/register it
127  3 createCache(name);
128  3 cache = getCache(name);
129    } else {
130  4 cache = wrap(name, ehcache);
131    }
132    } else {
133  0 log.warn("Cache manager status is {{}} while retrieving cache named {{}}. Returning null.", cacheManager.getStatus(), name);
134    }
135  7 return cache;
136    }
137   
 
138  3 toggle private void createCache(String name) {
139  3 synchronized (this.getClass()) {
140  3 cacheManager.createCache(name, getCacheConfiguration(name));
141    }
142    }
143   
 
144  10 toggle private CacheConfiguration getCacheConfiguration(String name) {
145  10 CacheConfiguration cacheConfiguration;
146  10 if (cachesConfiguration.containsKey(name)) {
147  7 cacheConfiguration = cachesConfiguration.get(name);
148    } else {
149  3 try {
150  3 cacheConfiguration = getCaches().containsKey(name) ? getCaches().get(name).build() : getCaches().get(DEFAULT_CACHE_NAME).build();
151    } catch (IllegalArgumentException e) {
152  0 cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Serializable.class, Serializable.class, ResourcePoolsBuilder.heap(10000).build()).build();
153  0 log.error("Cache {{}} is misconfigured: {{}}. Returning fallback config", name, e.getMessage());
154    }
155  3 cachesConfiguration.put(name, cacheConfiguration);
156    }
157  10 return cacheConfiguration;
158    }
159   
160   
 
161  4 toggle private EhCache3Wrapper wrap(String name, org.ehcache.Cache ehcache) {
162  4 return new EhCache3Wrapper(name, cacheModule, getBlockingTimeout(), ehcache);
163    }
164   
 
165  7 toggle private String getCacheManagerIdentifier() {
166  7 if (magnoliaConfigurationProperties.hasProperty(MAGNOLIA_PROPERTY_CACHE_MANAGER_ID)) {
167  2 return magnoliaConfigurationProperties.getProperty(MAGNOLIA_PROPERTY_CACHE_MANAGER_ID);
168    } else {
169  5 return magnoliaInitPaths.getContextPath();
170    }
171    }
172   
 
173    toggle public Map<String, Builder<? extends CacheConfiguration>> getCaches() {
174    return caches;
175    }
176   
 
177    toggle public void setCaches(Map<String, Builder<? extends CacheConfiguration>> caches) {
178    this.caches = caches;
179    }
180   
 
181    toggle public String getDiskStorePath() {
182    return diskStorePath;
183    }
184   
 
185    toggle public void setDiskStorePath(String diskStorePath) {
186    this.diskStorePath = diskStorePath;
187    }
188   
 
189    toggle public int getBlockingTimeout() {
190    return blockingTimeout;
191    }
192   
 
193    toggle public void setBlockingTimeout(int blockingTimeout) {
194    this.blockingTimeout = blockingTimeout;
195    }
196   
197    }