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