Guava 2 - Primitives, Strings & Cache Guava 对基本数据类型常用的操作进行了封装,命名也很好记,Int 类型的封装类取之为 Ints,long 类型的封装类取名为 Longs。
原始类型常用方法 1 2 3 4 5 6 7 8 9 Ints.min(int ... array); Ints.max(int ... array) Ints.concat(int []... arrays) Ints.reverse(int [] array) Ints.toArray(Collection<? extends Number> collection) Ints.asList(int ... backingArray) Ints.contains(int [] array, int target) Ints.indexOf(int [] array, int [] target) Ints.join(String separator, int ... array)
字符串相关 1 2 3 4 5 6 7 8 9 String nullToEmpty (@Nullable String string) ;String emptyToNull (@Nullable String string) ;boolean isNullOrEmpty (@Nullable String string) ;padStart(String string, int minLength, char padChar); padEnd(String string, int minLength, char padChar); repeat(String string, int count); commonPrefix(CharSequence a, CharSequence b); commonSuffix(CharSequence a, CharSequence b);
Joiner 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public String join (List<Integer> list, String separator) { StringBuilder builder = new StringBuilder(); list.forEach(item->builder.append(item).append(separator)); builder.setLength(builder.length() - 1 ); return builder.toString(); } Joiner joiner = Joiner.on("; " ).skipNulls(); return joiner.join("Harry" , null , "Ron" , "Hermione" ); Joiner.on("," ).join(Arrays.asList(1 , 5 , 7 )); Joiner.on("&" ).withKeyValueSeparator("=" ).join(ImmutableMap.of("id" , "1" , "name" , "kotlin" ));
Splitter 1 2 3 4 5 6 7 8 ",a,,b," .split("," ); Splitter.on("," ).trimResults().omitEmptyStrings().split("foo,bar,, qux" ); Splitter.on("&" ).withKeyValueSeparator("=" ).split("id=1&name=kotlin" );
缓存 guava 提供了缓存功能,是本地内存缓存,与 Redis 等分布式缓存不同,guava cache 只适用于单个应用运行时的数据存取,一般的像用户登录相关的 token ,userInfo 等频繁访问的接口数据,就可以使用缓存。
guava 缓存不是一整个全局缓存,你可以为某个具体的业务模块单独定义全局缓存
guava 提供了五大类缓存回收策略,按缓存数目,按缓存权重,按时间回收,按引用回收,显式手动清除
guava 提供了缓存统计功能,可统计每类缓存的命中率等数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 public LoadingCache<K, V> getLoadingCache () { if (loadingCache == null ) { synchronized (this ) { if (loadingCache == null ) { loadingCache = CacheBuilder.newBuilder() .maximumSize(maxSize) .expireAfterAccess(expireAfterAccessDuration, timeUnit) .recordStats() .removalListener(removalNotification -> { }).build(new CacheLoader<K, V>() { @Override public V load (K key) { return fetchValue(key); } }); } } } return loadingCache; } public abstract V fetchValue (K key) ;public V getCacheValue (K key) { V result = null ; try { result = getLoadingCache().get(key); if (getLoadingCache().size() > highestSize) { highestSize = getLoadingCache().size(); highestTime = new Date(); } } catch (Exception e) { logger.warn("guava 获取缓存异常:{}" , e.getMessage()); } return result; } public void refreshCacheValue (K key) { this .getLoadingCache().refresh(key); } @Component public class TokenCache extends AbsLoadingCache <String , LoginDo > { public TokenCache () { setMaximumSize(1000 ); setExpireAfterAccessDuration(2 ); setTimeUnit(TimeUnit.DAYS); } @Autowired LoginServiceMapper loginMapper; @Override public LoginDo fetchValue (String key) { return loginMapper.findByToken(key); } } @Autowired TokenCache tokenCache; public LoginDto findValidByToken (String token) { LoginDo loginDo = tokenCache.getCacheValue(token); if (loginDo == null ) { return null ; } boolean isExpired = DateUtils.isExpired(loginDo.getExpiredAt(), 0 ); if (isExpired) { loginDo.setExpiredAt(DateUtils.getDayFromNow(30 )); loginMapper.refreshToken(loginDo); } return CopyUtils.copyObject(loginDo, LoginDto.class); }
Title: Guava 2 - Primitives, Strings & Cache
Author: mjd507
Date: 2018-05-17
Last Update: 2024-01-27
Blog Link: https://mjd507.github.io/2018/05/17/Guava-Guide-2/
Copyright Declaration: This station is mainly used to sort out incomprehensible knowledge. I have not fully mastered most of the content. Please refer carefully.