• 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/LoadingCache.java
1
package com.alicp.jetcache;
2

3
import com.alicp.jetcache.event.CacheEvent;
4
import java.util.HashMap;
5
import java.util.LinkedHashSet;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.function.Consumer;
9
import java.util.stream.Collectors;
10

11
/**
12
 * Created on 2017/5/17.
13
 *
14
 * @author huangli
15
 */
16
public class LoadingCache<K, V> extends SimpleProxyCache<K, V> {
17

18
    protected Consumer<CacheEvent> eventConsumer;
19

20
    protected CacheConfig<K, V> config;
21

22
    public LoadingCache(Cache<K, V> cache) {
23
        super(cache);
×
24
        this.config = config();
×
25
        eventConsumer = CacheUtil.getAbstractCache(cache)::notify;
×
26
    }
×
27

28
    @Override
29
    public V get(K key) throws CacheInvokeException {
30
        CacheLoader<K, V> loader = config.getLoader();
×
31
        if (loader != null) {
×
32
            return AbstractCache.computeIfAbsentImpl(key, loader,
×
33
                    config.isCacheNullValue() ,0, null, this);
×
34
        } else {
35
            return cache.get(key);
×
36
        }
37
    }
38

39
    protected boolean needUpdate(V loadedValue, CacheLoader<K, V> loader) {
40
        if (loadedValue == null && !config.isCacheNullValue()) {
×
41
            return false;
×
42
        }
43
        if (loader.vetoCacheUpdate()) {
×
44
            return false;
×
45
        }
46
        return true;
×
47
    }
48

49
    @Override
50
    public Map<K, V> getAll(Set<? extends K> keys) throws CacheInvokeException {
51
        CacheLoader<K, V> loader = config.getLoader();
×
52
        if (loader != null) {
×
53
            MultiGetResult<K, V> r = GET_ALL(keys);
×
54
            Map<K, V> kvMap;
55
            if (r.isSuccess() || r.getResultCode() == CacheResultCode.PART_SUCCESS) {
×
56
                kvMap = r.unwrapValues();
×
57
            } else {
58
                kvMap = new HashMap<>();
×
59
            }
60
            Set<K> keysNeedLoad = new LinkedHashSet<>();
×
61
            keys.forEach((k) -> {
×
62
                if (!kvMap.containsKey(k)) {
×
63
                    keysNeedLoad.add(k);
×
64
                }
65
            });
×
66
            if (!config.isCachePenetrationProtect()) {
×
67
                if (eventConsumer != null) {
×
68
                    loader = CacheUtil.createProxyLoader(cache, loader, eventConsumer);
×
69
                }
70
                Map<K, V> loadResult;
71
                try {
72
                    loadResult = loader.loadAll(keysNeedLoad);
×
73

74
                    CacheLoader<K, V> theLoader = loader;
×
75
                    Map<K, V> updateValues = new HashMap<>();
×
76
                    loadResult.forEach((k,v)->{
×
77
                        if (needUpdate(v, theLoader)){
×
78
                            updateValues.put(k, v);
×
79
                        }
80
                    });
×
81
                    // batch put
82
                    if (!updateValues.isEmpty()) {
×
83
                        PUT_ALL(updateValues);
×
84
                    }
85
                } catch (Throwable e) {
×
86
                    throw new CacheInvokeException(e);
×
87
                }
×
88
                kvMap.putAll(loadResult);
×
89
            } else {
×
90
                AbstractCache<K, V> abstractCache = CacheUtil.getAbstractCache(cache);
×
91
                loader = CacheUtil.createProxyLoader(cache, loader, eventConsumer);
×
92
                for(K key : keysNeedLoad) {
×
93
                    Consumer<V> cacheUpdater = (v) -> {
×
94
                        if(needUpdate(v, config.getLoader())) {
×
95
                            PUT(key, v);
×
96
                        }
97
                    };
×
98
                    V v = AbstractCache.synchronizedLoad(config, abstractCache, key, loader, cacheUpdater);
×
99
                    kvMap.put(key, v);
×
100
                }
×
101
            }
102
            return kvMap;
×
103
        } else {
104
            return cache.getAll(keys);
×
105
        }
106

107
    }
108
}
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