1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
59
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
70
71 @Deprecated
72 public CacheModule(CacheMonitor cacheMonitor) {
73 this(cacheMonitor, Components.getComponentWithAnnotation(EventBus.class, Components.named(SystemEventBus.NAME)));
74 }
75
76 @Inject
77 public CacheModule(CacheMonitor cacheMonitor, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
78 super(cacheMonitor);
79 systemEventBus.addHandler(ModulesStartedEvent.class, new ModulesStartedEvent.Handler() {
80 @Override
81 public void onModuleStartupCompleted(ModulesStartedEvent event) {
82 start();
83 }
84 });
85 }
86
87
88
89
90 @Deprecated
91 public CacheModule(ModuleManager moduleManager, CacheMonitor cacheMonitor) {
92 this(cacheMonitor);
93 }
94
95
96
97
98 @Override
99 public CacheFactory getCacheFactory() {
100 return cacheFactory;
101 }
102
103 public void setCacheFactory(CacheFactory cacheFactory) {
104 this.cacheFactory = cacheFactory;
105 }
106
107 public Map<String, ContentCachingConfiguration> getContentCaching() {
108 return contentCaching;
109 }
110
111
112
113
114 @Deprecated
115 public ContentCachingConfiguration addConfiguration(String name, CacheConfiguration cacheConfiguration) {
116 DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.CacheModule#setContentCaching instead.");
117 return contentCaching.put(name, cacheConfiguration);
118 }
119
120 public void setContentCaching(Map<String, ContentCachingConfiguration> contentCaching) {
121 this.contentCaching = contentCaching;
122 }
123
124 public void setCompression(ContentCompression compression) {
125 this.compression = compression;
126 }
127
128 public ContentCompression getCompression() {
129 return compression;
130 }
131
132 public void register(CacheModuleLifecycleListener listener) {
133 listeners.add(listener);
134 }
135
136
137
138
139 @Deprecated
140 public ContentCachingConfiguration getConfiguration(String name) {
141 DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.CacheModule#getContentCaching instead.");
142 return this.getContentCaching(name);
143 }
144
145 @Override
146 public ContentCachingConfiguration getContentCaching(String name) {
147 return this.getContentCaching().get(name) != null ? this.getContentCaching().get(name) : this.getContentCaching().get(DEFAULT_CACHE_CONFIG);
148 }
149
150 @Override
151 public void start(ModuleLifecycleContext moduleLifecycleContext) {
152 cacheFactory.start(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
153 this.getCacheMonitor().countStart();
154 if (ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase()) {
155 start();
156 for (CacheModuleLifecycleListener listener : listeners) {
157 listener.onCacheModuleStart();
158 }
159 }
160 }
161
162 @Override
163 public void stop(ModuleLifecycleContext moduleLifecycleContext) {
164 this.getCacheMonitor().stop();
165 for (ContentCachingConfiguration cfg : contentCaching.values()) {
166 final String name = cfg.getCacheName();
167 this.stopCache(name);
168 }
169 cacheFactory.stop(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
170 }
171
172 private void start() {
173
174 for (ContentCachingConfiguration cfg : contentCaching.values()) {
175 final String name = cfg.getCacheName();
176 this.startCache(name);
177 }
178 }
179 }