Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache

File CacheModule.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
71% of files have more coverage

Code metrics

4
25
11
1
179
103
13
0.52
2.27
11
1.18
23.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
CacheModule 61 25 23.1% 13 32
0.220%
 

Contributing tests

This file is covered by 19 tests. .

Source view

1    /**
2    * This file Copyright (c) 2008-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;
35   
36    import info.magnolia.cms.util.DeprecationUtil;
37    import info.magnolia.event.EventBus;
38    import info.magnolia.event.SystemEventBus;
39    import info.magnolia.module.ModuleLifecycleContext;
40    import info.magnolia.module.ModuleManager;
41    import info.magnolia.module.ModulesStartedEvent;
42    import info.magnolia.module.cache.factory.DelegatingCacheFactory;
43    import info.magnolia.module.cache.mbean.CacheMonitor;
44    import info.magnolia.objectfactory.Components;
45   
46    import java.util.Collections;
47    import java.util.HashMap;
48    import java.util.HashSet;
49    import java.util.Map;
50    import java.util.Set;
51   
52    import javax.inject.Inject;
53    import javax.inject.Named;
54   
55    /**
56    * The CacheModule holds several named ContentCachingConfiguration instances and a CacheFactory.
57    *
58    * @see ContentCachingConfiguration
59    * @see CacheFactory
60    */
 
61    public class CacheModule extends AbstractCacheModule {
62   
63    private final Set<CacheModuleLifecycleListener> listeners = Collections.synchronizedSet(new HashSet<CacheModuleLifecycleListener>());
64    private CacheFactory cacheFactory = new DelegatingCacheFactory();
65    private Map<String, ContentCachingConfiguration> contentCaching = new HashMap<String, ContentCachingConfiguration>();
66    private ContentCompression compression;
67   
68    /**
69    * @deprecated since 5.4.5 - use {@link CacheModule#CacheModule(CacheMonitor, EventBus)} instead.
70    */
 
71  0 toggle @Deprecated
72    public CacheModule(CacheMonitor cacheMonitor) {
73  0 this(cacheMonitor, Components.getComponentWithAnnotation(EventBus.class, Components.named(SystemEventBus.NAME)));
74    }
75   
 
76  42 toggle @Inject
77    public CacheModule(CacheMonitor cacheMonitor, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
78  42 super(cacheMonitor);
79  42 systemEventBus.addHandler(ModulesStartedEvent.class, new ModulesStartedEvent.Handler() {
 
80  0 toggle @Override
81    public void onModuleStartupCompleted(ModulesStartedEvent event) {
82  0 start();
83    }
84    });
85    }
86   
87    /**
88    * @deprecated since 5.4. Use {@link #CacheModule(info.magnolia.module.cache.mbean.CacheMonitor)} intead.
89    */
 
90  0 toggle @Deprecated
91    public CacheModule(ModuleManager moduleManager, CacheMonitor cacheMonitor) {
92  0 this(cacheMonitor);
93    }
94   
95    /**
96    * Inject {@link info.magnolia.module.cache.inject.CacheFactoryProvider}} instead.
97    */
 
98    toggle @Override
99    public CacheFactory getCacheFactory() {
100    return cacheFactory;
101    }
102   
 
103    toggle public void setCacheFactory(CacheFactory cacheFactory) {
104    this.cacheFactory = cacheFactory;
105    }
106   
 
107    toggle public Map<String, ContentCachingConfiguration> getContentCaching() {
108    return contentCaching;
109    }
110   
111    /**
112    * @deprecated since 5.4 use {@link #setContentCaching(java.util.Map)}}
113    */
 
114  0 toggle @Deprecated
115    public ContentCachingConfiguration addConfiguration(String name, CacheConfiguration cacheConfiguration) {
116  0 DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.CacheModule#setContentCaching instead.");
117  0 return contentCaching.put(name, cacheConfiguration);
118    }
119   
 
120    toggle public void setContentCaching(Map<String, ContentCachingConfiguration> contentCaching) {
121    this.contentCaching = contentCaching;
122    }
123   
 
124    toggle public void setCompression(ContentCompression compression) {
125    this.compression = compression;
126    }
127   
 
128    toggle public ContentCompression getCompression() {
129    return compression;
130    }
131   
 
132  23 toggle public void register(CacheModuleLifecycleListener listener) {
133  23 listeners.add(listener);
134    }
135   
136    /**
137    * @deprecated since 5.4 use {@link #getContentCaching(String)}
138    */
 
139  0 toggle @Deprecated
140    public ContentCachingConfiguration getConfiguration(String name) {
141  0 DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.CacheModule#getContentCaching instead.");
142  0 return this.getContentCaching(name);
143    }
144   
 
145  47 toggle @Override
146    public ContentCachingConfiguration getContentCaching(String name) {
147  47 return this.getContentCaching().get(name) != null ? this.getContentCaching().get(name) : this.getContentCaching().get(DEFAULT_CACHE_CONFIG);
148    }
149   
 
150  0 toggle @Override
151    public void start(ModuleLifecycleContext moduleLifecycleContext) {
152  0 cacheFactory.start(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
153  0 this.getCacheMonitor().countStart();
154  0 if (ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase()) {
155  0 start();
156  0 for (CacheModuleLifecycleListener listener : listeners) {
157  0 listener.onCacheModuleStart();
158    }
159    }
160    }
161   
 
162  0 toggle @Override
163    public void stop(ModuleLifecycleContext moduleLifecycleContext) {
164  0 this.getCacheMonitor().stop();
165  0 for (ContentCachingConfiguration cfg : contentCaching.values()) {
166  0 final String name = cfg.getCacheName();
167  0 this.stopCache(name);
168    }
169  0 cacheFactory.stop(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
170    }
171   
 
172  0 toggle private void start() {
173    // Monitor and start flush policies for every configured pageCache
174  0 for (ContentCachingConfiguration cfg : contentCaching.values()) {
175  0 final String name = cfg.getCacheName();
176  0 this.startCache(name);
177    }
178    }
179    }