Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit de53b14

Browse files
committed
fix: optimize guava cache
1 parent b6b2ff2 commit de53b14

File tree

4 files changed

+21
-67
lines changed

4 files changed

+21
-67
lines changed

src/main/java/org/code4everything/boot/cache/guava/GuavaCache.java

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package org.code4everything.boot.cache.guava;
22

33
import com.google.common.cache.CacheBuilder;
4-
import org.code4everything.boot.config.BootConfig;
5-
import org.code4everything.boot.web.mvc.exception.ExceptionFactory;
6-
import org.springframework.cache.Cache;
7-
import org.springframework.cache.support.SimpleValueWrapper;
8-
import org.springframework.http.HttpStatus;
4+
import org.code4everything.boot.cache.AbstractCache;
95

10-
import java.util.Objects;
116
import java.util.concurrent.Callable;
127

138
/**
@@ -16,74 +11,39 @@
1611
* @author pantao
1712
* @since 2019/6/13
1813
**/
19-
public class GuavaCache implements Cache {
14+
public class GuavaCache extends AbstractCache {
2015

2116
private final com.google.common.cache.Cache<Object, Object> cache;
2217

23-
private final String name;
24-
2518
public GuavaCache(String name, com.google.common.cache.Cache<Object, Object> cache) {
26-
this.name = name;
19+
super(name, cache);
2720
this.cache = cache;
2821
}
2922

3023
public GuavaCache(String name, CacheBuilder<Object, Object> cacheBuilder) {
3124
this(name, cacheBuilder.build());
3225
}
3326

34-
@Override
35-
public String getName() {
36-
return name;
37-
}
38-
39-
@Override
40-
public Object getNativeCache() {
41-
return cache;
42-
}
43-
4427
@Override
4528
public ValueWrapper get(Object key) {
46-
Object value = cache.getIfPresent(key);
47-
return Objects.isNull(value) ? null : new SimpleValueWrapper(value);
29+
return wrapValueIfNotNull(cache.getIfPresent(key));
4830
}
4931

5032
@Override
51-
@SuppressWarnings("unchecked")
5233
public <T> T get(Object key, Class<T> type) {
53-
return (T) cache.getIfPresent(key);
34+
return convert2(cache.getIfPresent(key), type);
5435
}
5536

5637
@Override
57-
@SuppressWarnings("unchecked")
5838
public <T> T get(Object key, Callable<T> valueLoader) {
59-
Object value = cache.getIfPresent(key);
60-
if (Objects.isNull(value)) {
61-
try {
62-
return valueLoader.call();
63-
} catch (Exception e) {
64-
if (BootConfig.isDebug()) {
65-
throw ExceptionFactory.exception(HttpStatus.INTERNAL_SERVER_ERROR, "value loader error");
66-
}
67-
return null;
68-
}
69-
}
70-
return (T) value;
39+
return convert2(cache.getIfPresent(key), valueLoader);
7140
}
7241

7342
@Override
7443
public void put(Object key, Object value) {
7544
cache.put(key, value);
7645
}
7746

78-
@Override
79-
public ValueWrapper putIfAbsent(Object key, Object value) {
80-
Object val = cache.getIfPresent(key);
81-
if (Objects.isNull(val)) {
82-
put(key, value);
83-
}
84-
return new SimpleValueWrapper(val);
85-
}
86-
8747
@Override
8848
public void evict(Object key) {
8949
cache.invalidate(key);

src/main/java/org/code4everything/boot/cache/guava/GuavaCacheCreator.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.cache.CacheBuilder;
44
import org.code4everything.boot.cache.CacheCreator;
5-
import org.springframework.cache.Cache;
65

76
import java.util.Objects;
87

@@ -14,19 +13,15 @@
1413
**/
1514
public class GuavaCacheCreator implements CacheCreator {
1615

17-
private final CacheBuilder<Object, Object> cacheBuilder;
16+
protected final CacheBuilder<Object, Object> cacheBuilder;
1817

1918
public GuavaCacheCreator(CacheBuilder<Object, Object> cacheBuilder) {
2019
Objects.requireNonNull(cacheBuilder, "guava cache builder must not be null");
2120
this.cacheBuilder = cacheBuilder;
2221
}
2322

2423
@Override
25-
public Cache createCache(String cacheName) {
24+
public GuavaCache createCache(String cacheName) {
2625
return new GuavaCache(cacheName, cacheBuilder.build());
2726
}
28-
29-
public CacheBuilder<Object, Object> getCacheBuilder() {
30-
return cacheBuilder;
31-
}
3227
}

src/main/java/org/code4everything/boot/cache/guava/GuavaCacheManager.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import com.google.common.cache.CacheBuilder;
44
import org.code4everything.boot.cache.BootCacheManager;
5-
import org.code4everything.boot.cache.CacheCreator;
6-
import org.springframework.cache.Cache;
75

6+
import java.util.ArrayList;
87
import java.util.Collection;
98

109
/**
@@ -23,23 +22,23 @@ public GuavaCacheManager(CacheBuilder<Object, Object> cacheBuilder, int capacity
2322
this(new GuavaCacheCreator(cacheBuilder), capacity);
2423
}
2524

26-
public GuavaCacheManager(CacheCreator cacheCreator) {
27-
this(cacheCreator, 16);
25+
public GuavaCacheManager(GuavaCacheCreator guavaCacheCreator) {
26+
this(guavaCacheCreator, 16);
2827
}
2928

30-
public GuavaCacheManager(CacheCreator cacheCreator, int capacity) {
31-
super(cacheCreator, capacity);
29+
public GuavaCacheManager(GuavaCacheCreator guavaCacheCreator, int capacity) {
30+
super(guavaCacheCreator, capacity);
3231
}
3332

34-
public GuavaCacheManager(Collection<Cache> caches, CacheCreator cacheCreator) {
35-
super(caches, cacheCreator);
33+
public GuavaCacheManager(Collection<GuavaCache> caches, GuavaCacheCreator guavaCacheCreator) {
34+
super(new ArrayList<>(caches), guavaCacheCreator);
3635
}
3736

38-
public GuavaCacheManager(Collection<Cache> caches, CacheBuilder<Object, Object> cacheBuilder) {
39-
super(caches, new GuavaCacheCreator(cacheBuilder));
37+
public GuavaCacheManager(Collection<GuavaCache> caches, CacheBuilder<Object, Object> cacheBuilder) {
38+
super(new ArrayList<>(caches), new GuavaCacheCreator(cacheBuilder));
4039
}
4140

42-
public GuavaCacheManager(Collection<Cache> caches) {
43-
super(caches);
41+
public GuavaCacheManager(Collection<GuavaCache> caches) {
42+
super(new ArrayList<>(caches));
4443
}
4544
}

src/main/java/org/code4everything/boot/cache/redis/RedisCacheCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
**/
1515
public class RedisCacheCreator implements CacheCreator {
1616

17-
private final long timeout;
17+
protected final long timeout;
1818

19-
private final Map<String, String> prefixMap;
19+
protected final Map<String, String> prefixMap;
2020

2121
public RedisCacheCreator() {
2222
this(Long.MAX_VALUE, null);

0 commit comments

Comments
 (0)