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

alibaba / jetcache / #431

28 Apr 2025 05:27AM UTC coverage: 88.727% (-0.08%) from 88.802%
#431

push

areyouok
ci: change version to 2.7.9-SNAPSHOT

4754 of 5358 relevant lines covered (88.73%)

0.89 hits per line

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

89.91
/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);
1✔
29

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

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

35
    private CacheBuilderTemplate cacheBuilderTemplate;
36

37
    public SimpleCacheManager() {
1✔
38
    }
1✔
39

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

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

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

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

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

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

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

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

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

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

121

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

139

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

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

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

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

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

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

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

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

196
        if (config.getLocalLimit() != null && config.getLocalLimit() > 0) {
1✔
197
            cacheBuilder.setLimit(config.getLocalLimit());
1✔
198
        }
199
        if (config.getCacheType() == CacheType.BOTH &&
1✔
200
                config.getLocalExpire() != null && config.getLocalExpire().toMillis() > 0) {
1✔
201
            cacheBuilder.expireAfterWrite(config.getLocalExpire().toMillis(), TimeUnit.MILLISECONDS);
1✔
202
        } else if (config.getExpire() != null && config.getExpire().toMillis() > 0) {
1✔
203
            cacheBuilder.expireAfterWrite(config.getExpire().toMillis(), TimeUnit.MILLISECONDS);
1✔
204
        }
205
        if (config.getKeyConvertor() != null) {
1✔
206
            cacheBuilder.getConfig().setKeyConvertor(config.getKeyConvertor());
1✔
207
        }
208
        cacheBuilder.setCacheNullValue(config.getCacheNullValue() != null ?
1✔
209
                config.getCacheNullValue() : DEFAULT_CACHE_NULL_VALUE);
1✔
210
        return cacheBuilder.buildCache();
1✔
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