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