|
| 1 | +package biz.chenxu.tutorial; |
| 2 | + |
| 3 | +import com.google.common.cache.CacheBuilder; |
| 4 | +import com.google.common.cache.CacheLoader; |
| 5 | +import com.google.common.cache.LoadingCache; |
| 6 | + |
| 7 | +import java.util.Map; |
| 8 | +import java.util.concurrent.ExecutionException; |
| 9 | + |
| 10 | +public class LoadingCacheTutorial { |
| 11 | + |
| 12 | + private LoadingCache<String, String> loadingCache; |
| 13 | + |
| 14 | + private int counter; |
| 15 | + |
| 16 | + private static LoadingCacheTutorial instance = new LoadingCacheTutorial(); |
| 17 | + |
| 18 | + public static LoadingCacheTutorial getInstance() { |
| 19 | + return instance; |
| 20 | + } |
| 21 | + |
| 22 | + private LoadingCacheTutorial() { |
| 23 | + loadingCache = CacheBuilder.newBuilder() |
| 24 | + .maximumSize(3) |
| 25 | + .build(new CacheLoader<String, String>() { |
| 26 | + |
| 27 | + @Override |
| 28 | + public String load(String key) throws Exception { |
| 29 | + System.out.println(String.format("loading not exist key [%s]", key)); |
| 30 | + return String.format("cache value %d", counter++); |
| 31 | + } |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + public String getCacheValueIfExist(String key) { |
| 36 | + return loadingCache.getIfPresent(key); |
| 37 | + } |
| 38 | + |
| 39 | + public String getCacheValue(String key) { |
| 40 | + try { |
| 41 | + return loadingCache.get(key); |
| 42 | + } catch (ExecutionException e) { |
| 43 | + e.printStackTrace(); |
| 44 | + return null; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public void dump() { |
| 49 | + System.out.println("***begin dump cache content***"); |
| 50 | + for (Map.Entry<String, String> entry : loadingCache.asMap().entrySet()) { |
| 51 | + System.out.println(String.format("%s = %s", entry.getKey(), entry.getValue())); |
| 52 | + } |
| 53 | + System.out.println("***end dump cache content***"); |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) { |
| 57 | + |
| 58 | + // load notExistKey1 |
| 59 | + String notExistKey1 = "notExistKey1"; |
| 60 | + String notExistValue1 = LoadingCacheTutorial.getInstance().getCacheValue(notExistKey1); |
| 61 | + System.out.println(String.format("1.%s = %s", notExistKey1, notExistValue1)); |
| 62 | + |
| 63 | + // don't load notExistKey2 |
| 64 | + String notExistKey2 = "notExistKey2"; |
| 65 | + String notExistValue2 = LoadingCacheTutorial.getInstance().getCacheValueIfExist(notExistKey2); |
| 66 | + System.out.println(String.format("2.%s = %s", notExistKey2, notExistValue2)); |
| 67 | + |
| 68 | + // LRU |
| 69 | + // load notExistKey2 |
| 70 | + LoadingCacheTutorial.getInstance().getCacheValue(notExistKey2); |
| 71 | + LoadingCacheTutorial.getInstance().dump(); |
| 72 | + |
| 73 | + System.err.println("[Warn] the key2 will be removed by LRU strategy ......"); |
| 74 | + // use key1 |
| 75 | + LoadingCacheTutorial.getInstance().getCacheValue(notExistKey1); |
| 76 | + LoadingCacheTutorial.getInstance().getCacheValue(notExistKey1); |
| 77 | + |
| 78 | + // load notExistKey3 |
| 79 | + LoadingCacheTutorial.getInstance().getCacheValue("notExistKey3"); |
| 80 | + |
| 81 | + // load notExistKey4 |
| 82 | + LoadingCacheTutorial.getInstance().getCacheValue("notExistKey4"); |
| 83 | + |
| 84 | + LoadingCacheTutorial.getInstance().dump(); |
| 85 | + } |
| 86 | +} |
0 commit comments