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

3
import com.alicp.jetcache.CacheValueHolder;
4
import com.github.benmanes.caffeine.cache.Caffeine;
5
import com.github.benmanes.caffeine.cache.Expiry;
6

7
import java.util.Collection;
8
import java.util.Map;
9
import java.util.concurrent.TimeUnit;
10

11
/**
12
 * Created on 2016/10/25.
13
 *
14
 * @author huangli
15
 */
16
public class CaffeineCache<K, V> extends AbstractEmbeddedCache<K, V> {
17

18
    private com.github.benmanes.caffeine.cache.Cache cache;
19

20
    public CaffeineCache(EmbeddedCacheConfig<K, V> config) {
21
        super(config);
×
22
    }
×
23

24
    @Override
25
    public <T> T unwrap(Class<T> clazz) {
26
        if (clazz.equals(com.github.benmanes.caffeine.cache.Cache.class)) {
×
27
            return (T) cache;
×
28
        }
29
        throw new IllegalArgumentException(clazz.getName());
×
30
    }
31

32
    @Override
33
    @SuppressWarnings("unchecked")
34
    protected InnerMap createAreaCache() {
35
        Caffeine<Object, Object> builder = Caffeine.newBuilder();
×
36
        builder.maximumSize(config.getLimit());
×
37
        final boolean isExpireAfterAccess = config.isExpireAfterAccess();
×
38
        final long expireAfterAccess = config.getExpireAfterAccessInMillis();
×
39
        builder.expireAfter(new Expiry<Object, CacheValueHolder>() {
×
40
            private long getRestTimeInNanos(CacheValueHolder value) {
41
                long now = System.currentTimeMillis();
×
42
                long ttl = value.getExpireTime() - now;
×
43
                if(isExpireAfterAccess){
×
44
                    ttl = Math.min(ttl, expireAfterAccess);
×
45
                }
46
                return TimeUnit.MILLISECONDS.toNanos(ttl);
×
47
            }
48

49
            @Override
50
            public long expireAfterCreate(Object key, CacheValueHolder value, long currentTime) {
51
                return getRestTimeInNanos(value);
×
52
            }
53

54
            @Override
55
            public long expireAfterUpdate(Object key, CacheValueHolder value,
56
                                          long currentTime, long currentDuration) {
57
                return currentDuration;
×
58
            }
59

60
            @Override
61
            public long expireAfterRead(Object key, CacheValueHolder value,
62
                                        long currentTime, long currentDuration) {
63
                return getRestTimeInNanos(value);
×
64
            }
65
        });
66

67
        cache = builder.build();
×
68
        return new InnerMap() {
×
69
            @Override
70
            public Object getValue(Object key) {
71
                return cache.getIfPresent(key);
×
72
            }
73

74
            @Override
75
            public Map getAllValues(Collection keys) {
76
                return cache.getAllPresent(keys);
×
77
            }
78

79
            @Override
80
            public void putValue(Object key, Object value) {
81
                cache.put(key, value);
×
82
            }
×
83

84
            @Override
85
            public void putAllValues(Map map) {
86
                cache.putAll(map);
×
87
            }
×
88

89
            @Override
90
            public boolean removeValue(Object key) {
91
                return cache.asMap().remove(key) != null;
×
92
            }
93

94
            @Override
95
            public void removeAllValues(Collection keys) {
96
                cache.invalidateAll(keys);
×
97
            }
×
98

99
            @Override
100
            public boolean putIfAbsentValue(Object key, Object value) {
101
                return cache.asMap().putIfAbsent(key, value) == null;
×
102
            }
103
        };
104
    }
105
}
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