• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jetcache-core/src/main/java/com/alicp/jetcache/SimpleCacheManager.java
1
/**
2
 * Created on 2019/2/1.
3
 */
4
package com.alicp.jetcache;
5

6
import com.alicp.jetcache.anno.CacheType;
7
import com.alicp.jetcache.embedded.EmbeddedCacheBuilder;
8
import com.alicp.jetcache.external.ExternalCacheBuilder;
9
import com.alicp.jetcache.support.BroadcastManager;
10
import com.alicp.jetcache.template.CacheBuilderTemplate;
11
import com.alicp.jetcache.template.CacheMonitorInstaller;
12
import com.alicp.jetcache.template.QuickConfig;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

16
import java.util.Objects;
17
import java.util.concurrent.ConcurrentHashMap;
18
import java.util.concurrent.TimeUnit;
19
import java.util.function.Supplier;
20

21
/**
22
 * @author huangli
23
 */
24
public class SimpleCacheManager implements CacheManager, AutoCloseable {
25

26
    private static final boolean DEFAULT_CACHE_NULL_VALUE = false;
27

28
    private static final Logger logger = LoggerFactory.getLogger(SimpleCacheManager.class);
×
29

30
    // area -> cacheName -> Cache
31
    private final ConcurrentHashMap<String, ConcurrentHashMap<String, Cache>> caches = new ConcurrentHashMap<>();
×
32

33
    private final ConcurrentHashMap<String, BroadcastManager> broadcastManagers = new ConcurrentHashMap();
×
34

35
    private CacheBuilderTemplate cacheBuilderTemplate;
36

37
    public SimpleCacheManager() {
×
38
    }
×
39

40
    @Override
41
    public void close() {
42
        broadcastManagers.forEach((area, bm) -> {
×
43
            try {
44
                bm.close();
×
45
            } catch (Exception e) {
×
46
                logger.error("error during close broadcast manager", e);
×
47
            }
×
48
        });
×
49
        broadcastManagers.clear();
×
50
        caches.forEach((area, areaMap) -> {
×
51
            areaMap.forEach((cacheName, cache) -> {
×
52
                try {
53
                    cache.close();
×
54
                } catch (Exception e) {
×
55
                    logger.error("error during close Cache", e);
×
56
                }
×
57
            });
×
58
        });
×
59
        caches.clear();
×
60
    }
×
61

62
    private ConcurrentHashMap<String, Cache> getCachesByArea(String area) {
63
        return caches.computeIfAbsent(area, (key) -> new ConcurrentHashMap<>());
×
64
    }
65

66
    @Override
67
    public Cache getCache(String area, String cacheName) {
68
        ConcurrentHashMap<String, Cache> areaMap = getCachesByArea(area);
×
69
        return areaMap.get(cacheName);
×
70
    }
71

72
    @Override
73
    public void putCache(String area, String cacheName, Cache cache) {
74
        ConcurrentHashMap<String, Cache> areaMap = getCachesByArea(area);
×
75
        areaMap.put(cacheName, cache);
×
76
    }
×
77

78
    @Override
79
    public BroadcastManager getBroadcastManager(String area) {
80
        return broadcastManagers.get(area);
×
81
    }
82

83
    @Override
84
    public void putBroadcastManager(String area, BroadcastManager broadcastManager) {
85
        broadcastManagers.put(area, broadcastManager);
×
86
    }
×
87

88
    public CacheBuilderTemplate getCacheBuilderTemplate() {
89
        return cacheBuilderTemplate;
×
90
    }
91

92
    public void setCacheBuilderTemplate(CacheBuilderTemplate cacheBuilderTemplate) {
93
        this.cacheBuilderTemplate = cacheBuilderTemplate;
×
94
    }
×
95

96
    @Override
97
    public <K, V> Cache<K, V> getOrCreateCache(QuickConfig config) {
98
        if (cacheBuilderTemplate == null) {
×
99
            throw new IllegalStateException("cacheBuilderTemplate not set");
×
100
        }
101
        Objects.requireNonNull(config.getArea());
×
102
        Objects.requireNonNull(config.getName());
×
103
        ConcurrentHashMap<String, Cache> m = getCachesByArea(config.getArea());
×
104
        Cache c = m.get(config.getName());
×
105
        if (c != null) {
×
106
            return c;
×
107
        }
108
        return m.computeIfAbsent(config.getName(), n -> create(config));
×
109
    }
110

111
    private Cache create(QuickConfig config) {
112
        Cache cache;
113
        if (config.getCacheType() == null || config.getCacheType() == CacheType.REMOTE) {
×
114
            cache = buildRemote(config);
×
115
        } else if (config.getCacheType() == CacheType.LOCAL) {
×
116
            cache = buildLocal(config);
×
117
        } else {
118
            Cache local = buildLocal(config);
×
119
            Cache remote = buildRemote(config);
×
120

121

122
            boolean useExpireOfSubCache = config.getLocalExpire() != null;
×
123
            cache = MultiLevelCacheBuilder.createMultiLevelCacheBuilder()
×
124
                    .expireAfterWrite(remote.config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS)
×
125
                    .addCache(local, remote)
×
126
                    .useExpireOfSubCache(useExpireOfSubCache)
×
127
                    .cacheNullValue(config.getCacheNullValue() != null ?
×
128
                            config.getCacheNullValue() : DEFAULT_CACHE_NULL_VALUE)
×
129
                    .buildCache();
×
130
        }
131
        if (config.getRefreshPolicy() != null) {
×
132
            cache = new RefreshCache(cache);
×
133
        } else if (config.getLoader() != null) {
×
134
            cache = new LoadingCache(cache);
×
135
        }
136
        cache.config().setRefreshPolicy(config.getRefreshPolicy());
×
137
        cache.config().setLoader(config.getLoader());
×
138

139

140
        boolean protect = config.getPenetrationProtect() != null ? config.getPenetrationProtect()
×
141
                : cacheBuilderTemplate.isPenetrationProtect();
×
142
        cache.config().setCachePenetrationProtect(protect);
×
143
        cache.config().setPenetrationProtectTimeout(config.getPenetrationProtectTimeout());
×
144

145
        for (CacheMonitorInstaller i : cacheBuilderTemplate.getCacheMonitorInstallers()) {
×
146
            i.addMonitors(this, cache, config);
×
147
        }
×
148
        return cache;
×
149
    }
150

151
    private Cache buildRemote(QuickConfig config) {
152
        ExternalCacheBuilder cacheBuilder = (ExternalCacheBuilder) cacheBuilderTemplate
×
153
                .getCacheBuilder(1, config.getArea());
×
154
        if (cacheBuilder == null) {
×
155
            throw new CacheConfigException("no remote cache builder: " + config.getArea());
×
156
        }
157

158
        if (config.getExpire() != null && config.getExpire().toMillis() > 0) {
×
159
            cacheBuilder.expireAfterWrite(config.getExpire().toMillis(), TimeUnit.MILLISECONDS);
×
160
        }
161

162
        String prefix;
163
        if (config.getUseAreaInPrefix() != null && config.getUseAreaInPrefix()) {
×
164
            prefix = config.getArea() + "_" + config.getName();
×
165
        } else {
166
            prefix = config.getName();
×
167
        }
168
        if (cacheBuilder.getConfig().getKeyPrefixSupplier() != null) {
×
169
            Supplier<String> supplier = cacheBuilder.getConfig().getKeyPrefixSupplier();
×
170
            cacheBuilder.setKeyPrefixSupplier(() -> supplier.get() + prefix);
×
171
        } else {
×
172
            cacheBuilder.setKeyPrefix(prefix);
×
173
        }
174

175
        if (config.getKeyConvertor() != null) {
×
176
            cacheBuilder.getConfig().setKeyConvertor(config.getKeyConvertor());
×
177
        }
178
        if (config.getValueEncoder() != null) {
×
179
            cacheBuilder.getConfig().setValueEncoder(config.getValueEncoder());
×
180
        }
181
        if (config.getValueDecoder() != null) {
×
182
            cacheBuilder.getConfig().setValueDecoder(config.getValueDecoder());
×
183
        }
184

185
        cacheBuilder.setCacheNullValue(config.getCacheNullValue() != null ?
×
186
                config.getCacheNullValue() : DEFAULT_CACHE_NULL_VALUE);
×
187
        return cacheBuilder.buildCache();
×
188
    }
189

190
    private Cache buildLocal(QuickConfig config) {
191
        EmbeddedCacheBuilder cacheBuilder = (EmbeddedCacheBuilder) cacheBuilderTemplate.getCacheBuilder(0, config.getArea());
×
192
        if (cacheBuilder == null) {
×
193
            throw new CacheConfigException("no local cache builder: " + config.getArea());
×
194
        }
195

196
        if (config.getLocalLimit() != null && config.getLocalLimit() > 0) {
×
197
            cacheBuilder.setLimit(config.getLocalLimit());
×
198
        }
199
        if (config.getCacheType() == CacheType.BOTH &&
×
200
                config.getLocalExpire() != null && config.getLocalExpire().toMillis() > 0) {
×
201
            cacheBuilder.expireAfterWrite(config.getLocalExpire().toMillis(), TimeUnit.MILLISECONDS);
×
202
        } else if (config.getExpire() != null && config.getExpire().toMillis() > 0) {
×
203
            cacheBuilder.expireAfterWrite(config.getExpire().toMillis(), TimeUnit.MILLISECONDS);
×
204
        }
205
        if (config.getKeyConvertor() != null) {
×
206
            cacheBuilder.getConfig().setKeyConvertor(config.getKeyConvertor());
×
207
        }
208
        cacheBuilder.setCacheNullValue(config.getCacheNullValue() != null ?
×
209
                config.getCacheNullValue() : DEFAULT_CACHE_NULL_VALUE);
×
210
        return cacheBuilder.buildCache();
×
211
    }
212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc