• 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/AbstractEmbeddedCache.java
1
/**
2
 * Created on  13-10-17 23:01
3
 */
4
package com.alicp.jetcache.embedded;
5

6
import com.alicp.jetcache.*;
7

8
import java.util.ArrayList;
9
import java.util.HashMap;
10
import java.util.Map;
11
import java.util.Set;
12
import java.util.concurrent.TimeUnit;
13
import java.util.concurrent.locks.ReentrantLock;
14
import java.util.function.Function;
15
import java.util.stream.Collectors;
16

17
/**
18
 * @author huangli
19
 */
20
public abstract class AbstractEmbeddedCache<K, V> extends AbstractCache<K, V> {
21
    protected EmbeddedCacheConfig<K, V> config;
22
    protected InnerMap innerMap;
23

24
    protected abstract InnerMap createAreaCache();
25

26
    private final ReentrantLock lock = new ReentrantLock();
×
27

28
    public AbstractEmbeddedCache(EmbeddedCacheConfig<K, V> config) {
×
29
        this.config = config;
×
30
        innerMap = createAreaCache();
×
31
    }
×
32

33
    @Override
34
    public CacheConfig<K, V> config() {
35
        return config;
×
36
    }
37

38
    public Object buildKey(K key) {
39
        Object newKey = key;
×
40
        Function<K, Object> keyConvertor = config.getKeyConvertor();
×
41
        if (keyConvertor != null) {
×
42
            newKey = keyConvertor.apply(key);
×
43
        }
44
        return newKey;
×
45
    }
46

47
    @Override
48
    protected CacheGetResult<V> do_GET(K key) {
49
        Object newKey = buildKey(key);
×
50
        CacheValueHolder<V> holder = (CacheValueHolder<V>) innerMap.getValue(newKey);
×
51
        return parseHolderResult(holder);
×
52
    }
53

54
    protected CacheGetResult<V> parseHolderResult(CacheValueHolder<V> holder) {
55
        long now = System.currentTimeMillis();
×
56
        if (holder == null) {
×
57
            return CacheGetResult.NOT_EXISTS_WITHOUT_MSG;
×
58
        } else if (now >= holder.getExpireTime()) {
×
59
            return CacheGetResult.EXPIRED_WITHOUT_MSG;
×
60
        } else {
61
            lock.lock();
×
62
            try{
63
                long accessTime = holder.getAccessTime();
×
64
                if (config.isExpireAfterAccess()) {
×
65
                    long expireAfterAccess = config.getExpireAfterAccessInMillis();
×
66
                    if (now >= accessTime + expireAfterAccess) {
×
67
                        return CacheGetResult.EXPIRED_WITHOUT_MSG;
×
68
                    }
69
                }
70
                holder.setAccessTime(now);
×
71
            }finally {
72
                lock.unlock();
×
73
            }
74

75
            return new CacheGetResult(CacheResultCode.SUCCESS, null, holder);
×
76
        }
77
    }
78

79
    @Override
80
    protected MultiGetResult<K, V> do_GET_ALL(Set<? extends K> keys) {
81
        ArrayList<K> keyList = new ArrayList<K>(keys.size());
×
82
        ArrayList<Object> newKeyList = new ArrayList<Object>(keys.size());
×
83
        keys.stream().forEach((k) -> {
×
84
            Object newKey = buildKey(k);
×
85
            keyList.add(k);
×
86
            newKeyList.add(newKey);
×
87
        });
×
88
        Map<Object, CacheValueHolder<V>> innerResultMap = innerMap.getAllValues(newKeyList);
×
89
        Map<K, CacheGetResult<V>> resultMap = new HashMap<>();
×
90
        for (int i = 0; i < keyList.size(); i++) {
×
91
            K key = keyList.get(i);
×
92
            Object newKey = newKeyList.get(i);
×
93
            CacheValueHolder<V> holder = innerResultMap.get(newKey);
×
94
            resultMap.put(key, parseHolderResult(holder));
×
95
        }
96
        MultiGetResult<K, V> result = new MultiGetResult<>(CacheResultCode.SUCCESS, null, resultMap);
×
97
        return result;
×
98
    }
99

100
    @Override
101
    protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
102
        CacheValueHolder<V> cacheObject = new CacheValueHolder(value ,timeUnit.toMillis(expireAfterWrite));
×
103
        innerMap.putValue(buildKey(key), cacheObject);
×
104
        return CacheResult.SUCCESS_WITHOUT_MSG;
×
105
    }
106

107
    @Override
108
    protected CacheResult do_PUT_ALL(Map<? extends K, ? extends V> map, long expireAfterWrite, TimeUnit timeUnit) {
109
        HashMap newKeyMap = new HashMap();
×
110
        for (Map.Entry<? extends K, ? extends V> en : map.entrySet()) {
×
111
            CacheValueHolder<V> cacheObject = new CacheValueHolder(en.getValue(), timeUnit.toMillis(expireAfterWrite));
×
112
            newKeyMap.put(buildKey(en.getKey()), cacheObject);
×
113
        }
×
114
        innerMap.putAllValues(newKeyMap);
×
115

116
        final HashMap resultMap = new HashMap();
×
117
        map.keySet().forEach((k) -> resultMap.put(k, CacheResultCode.SUCCESS));
×
118
        return CacheResult.SUCCESS_WITHOUT_MSG;
×
119
    }
120

121
    @Override
122
    protected CacheResult do_REMOVE(K key) {
123
        innerMap.removeValue(buildKey(key));
×
124
        return CacheResult.SUCCESS_WITHOUT_MSG;
×
125
    }
126

127
    @Override
128
    protected CacheResult do_REMOVE_ALL(Set<? extends K> keys) {
129
        Set newKeys = keys.stream().map((key) -> buildKey(key)).collect(Collectors.toSet());
×
130
        innerMap.removeAllValues(newKeys);
×
131

132
        return CacheResult.SUCCESS_WITHOUT_MSG;
×
133
    }
134

135
    // internal method
136
    public void __removeAll(Set<? extends K> keys) {
137
        innerMap.removeAllValues(keys);
×
138
    }
×
139

140
    @Override
141
    protected CacheResult do_PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
142
        CacheValueHolder<V> cacheObject = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite));
×
143
        if (innerMap.putIfAbsentValue(buildKey(key), cacheObject)) {
×
144
            return CacheResult.SUCCESS_WITHOUT_MSG;
×
145
        } else {
146
            return CacheResult.EXISTS_WITHOUT_MSG;
×
147
        }
148
    }
149
}
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