1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.caravan.pipeline.impl.cache;
21
22 import io.wcm.caravan.pipeline.cache.CachePersistencyOptions;
23 import io.wcm.caravan.pipeline.cache.spi.CacheAdapter;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import rx.Observable;
33
34
35
36
37
38
39 public class MultiLayerCacheAdapter implements CacheAdapter {
40
41 private static final Logger log = LoggerFactory.getLogger(MultiLayerCacheAdapter.class);
42
43
44
45
46 public static final String MULTILAYER_CACHE_KEY = "NO_CACHEADAPTER_AVAILABLE";
47
48 private List<CacheAdapter> cacheAdapters;
49
50
51
52
53
54
55 public MultiLayerCacheAdapter(List<CacheAdapter> cacheAdapters) {
56 this.cacheAdapters = new ArrayList<CacheAdapter>(cacheAdapters);
57
58 }
59
60
61
62
63
64
65
66
67
68 @Override
69 public Observable<String> get(String cacheKey, CachePersistencyOptions options) {
70 return Observable.create(subscriber -> {
71 Observable<String> result = Observable.empty();
72 CacheAdapter actualCacheAdapter = null;
73 for (int i = 0; i < cacheAdapters.size(); i++) {
74 CacheAdapter cacheAdapter = cacheAdapters.get(i);
75 log.debug("Trying to retrieve document from cache level {} : {} : ", i, cacheAdapter.getClass().getSimpleName(), cacheKey);
76 result = cacheAdapter.get(cacheKey, options).cache();
77 if (!result.isEmpty().toBlocking().first()) {
78 log.debug("Retrieved document from cache level {} : {} : {}", i, cacheAdapter.getClass().getSimpleName(), cacheKey);
79 actualCacheAdapter = cacheAdapter;
80 break;
81 }
82 }
83 if (actualCacheAdapter != null) {
84 String cachedValue = result.toBlocking().first();
85 subscriber.onNext(cachedValue);
86 put(cacheKey, cachedValue, options, actualCacheAdapter);
87 }
88 subscriber.onCompleted();
89 });
90 }
91
92
93
94
95 private void put(String cacheKey, String jsonString, CachePersistencyOptions options, CacheAdapter actualCacheAdapter) {
96 for (int i = 0; i < cacheAdapters.size(); i++) {
97 CacheAdapter cacheAdapter = cacheAdapters.get(i);
98 if (cacheAdapter != actualCacheAdapter) {
99 log.debug("Promoting document into cache level {} : {} : ", i, cacheAdapter.getClass().getSimpleName(), cacheKey);
100 cacheAdapter.put(cacheKey, jsonString, options);
101 }
102 else {
103 break;
104 }
105 }
106 }
107
108
109
110
111
112
113
114 @Override
115 public void put(String cacheKey, String jsonString, CachePersistencyOptions options) {
116 for (int i = 0; i < cacheAdapters.size(); i++) {
117 CacheAdapter cacheAdapter = cacheAdapters.get(i);
118 log.debug("Putting document into cache level {} : {} : {} ", i, cacheAdapter.getClass().getSimpleName(), cacheKey);
119 cacheAdapter.put(cacheKey, jsonString, options);
120 }
121 }
122
123
124
125
126 public int cachingLevels() {
127 return cacheAdapters.size();
128 }
129
130
131
132
133 public List<CacheAdapter> getCacheAdapters() {
134 return Collections.unmodifiableList(cacheAdapters);
135 }
136
137 }