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.factory;
35
36 import info.magnolia.module.cache.Cache;
37 import info.magnolia.module.cache.CacheFactory;
38
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.List;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47
48
49 public class DelegatingCacheFactory implements CacheFactory {
50
51 private final Logger log = LoggerFactory.getLogger(DelegatingCacheFactory.class);
52
53 private List<CacheFactory> delegateFactories = new ArrayList<>();
54
55 @Override
56 public Cache getCache(String name) {
57 for (CacheFactory cacheFactory : delegateFactories) {
58 if (cacheFactory.getCache(name) != null) {
59 return cacheFactory.getCache(name);
60 }
61 }
62 return new NullCache();
63 }
64
65 @Override
66 public void start(boolean isRestart) {
67 for (CacheFactory cacheFactory : delegateFactories) {
68 cacheFactory.start(isRestart);
69 }
70 }
71
72 @Override
73 public void stop(boolean isRestart) {
74 for (CacheFactory cacheFactory : delegateFactories) {
75 cacheFactory.stop(isRestart);
76 }
77 }
78
79 @Override
80 public List<String> getCacheNames() {
81 List<String> cacheNames = new ArrayList<>();
82 for (CacheFactory cacheFactory : delegateFactories) {
83 cacheNames.addAll(cacheFactory.getCacheNames());
84 }
85 return cacheNames;
86 }
87
88 public void init() {
89 if (delegateFactories.isEmpty()) {
90 log.warn("No cache factory configured. Please install a cache implementation module");
91 }
92 }
93
94 public List<CacheFactory> getDelegateFactories() {
95 return delegateFactories;
96 }
97
98 public void setDelegateFactories(List<CacheFactory> delegateFactories) {
99 this.delegateFactories = delegateFactories;
100 }
101
102
103
104
105 class NullCache implements Cache {
106
107 @Override
108 public boolean hasElement(Object key) {
109 return false;
110 }
111
112 @Override
113 public void put(Object key, Object value) {
114 }
115
116 @Override
117 public void put(Object key, Object value, int timeToLiveInSeconds) {
118 }
119
120 @Override
121 public Object get(Object key) {
122 return null;
123 }
124
125 @Override
126 public Object getQuiet(Object key) {
127 return null;
128 }
129
130 @Override
131 public void remove(Object key) {
132 }
133
134 @Override
135 public void clear() {
136 }
137
138 @Override
139 public String getName() {
140 return null;
141 }
142
143 @Override
144 public int getSize() {
145 return 0;
146 }
147
148 @Override
149 public Collection<Object> getKeys() {
150 return null;
151 }
152 }
153 }