加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

怎样举办高效的源码阅读:以Spring Cache扩展为例带你搞清晰

发布时间:2019-07-27 20:45:10 所属栏目:建站 来源:三旬老汉
导读:择要 一般开拓中,必要用到各类百般的框架来实现API、体系的构建。作为措施员,除了会行使框架还必必要相识框架事变的道理。这样可以便于我们排盘查题,和自界说的扩展。那么怎样去进修框架呢。凡是我们通过阅读文档、查察源码,然后又很快健忘。始终不能

知道怎样行使Spring Cache后,我们必要进一步思索,就是怎样扩展。那么带着题目出发。
好比Spring Cache不支持批量key的缓存,像上文我们举的例子,我们但愿缓存的key是userId,而不是Collection userIds。以userId为key,这样的缓存掷中率更高,存储的本钱更小。

  1. @Cacheable(cacheNames = "users") 
  2.    public Map<Long, User> getUser(final Collection<Long> userIds) {} 

以是我们要实现对Spring Cache举办扩展。step3中我们已经大抵相识了Spring Cache的实现。那么实现这个扩展的成果就是拆分Collection userIds,缓存掷中的从缓存中获取,没有掷中的,挪用源要领。

  1. @Aspect 
  2. @Component 
  3. public class CacheExtenionAspect { 
  4.  
  5.     @Autowired 
  6.     private CacheExtensionManage cacheExtensionManage; 
  7.  
  8.     /** 
  9.      * 返回的功效中缓存掷中的从缓存中获取,没有掷中的挪用原本的要领获取 
  10.      * @param joinPoint 
  11.      * @return 
  12.      */ 
  13.     @Around("@annotation(org.springframework.cache.annotation.Cacheable)") 
  14.     @SuppressWarnings("unchecked") 
  15.     public Object aroundCache(final ProceedingJoinPoint joinPoint) { 
  16.      
  17.         // 修改掉Collection值,cacheResult必要从头结构一个 
  18.         args[0] = cacheResult.getMiss(); 
  19.         try { 
  20.             final Map<Object, Object> notHit = CollectionUtils.isEmpty(cacheResult.getMiss()) ? null 
  21.                     : (Map<Object, Object>) (method.invoke(target, args)); 
  22.             final Map<Object, Object> hits = cacheResult.getHit(); 
  23.             if (Objects.isNull(notHit)) { 
  24.                 return hits; 
  25.             } 
  26.             // 配置缓存 
  27.             cacheResult.getCache().putAll(notHit); 
  28.             hits.putAll(notHit); 
  29.             return hits; 
  30.     } 
  31. 然后扩展Cache,CacheManage 
  32. 重写Cache的查找缓存要领,返回新的CacheResult 
  33.  
  34.   public static Object lookup(final CacheExtension cache, final Object key) { 
  35.         if (key instanceof Collection) { 
  36.             final Collection<Object> originalKeys = ((Collection) key); 
  37.             if (originalKeys == null || originalKeys.isEmpty()) { 
  38.                 return CacheResult.builder().cache(cache).miss( 
  39.                         Collections.emptySet()) 
  40.                         .build(); 
  41.             } 
  42.             final List<Object> keys = originalKeys.stream() 
  43.                     .filter(Objects::nonNull).collect(Collectors.toList()); 
  44.             final Map<Object, Object> hits = cache.getAll(keys); 
  45.             final Set<Object> miss = new HashSet(keys); 
  46.             miss.removeAll(hits.keySet()); 
  47.             return CacheResult.builder().cache(cache).hit(hits).miss(miss).build(); 
  48.         } 
  49.         return null; 
  50.     } 
  51. CacheResult就是新的缓存功效名目 
  52.  
  53.  @Builder 
  54.     @Setter 
  55.     @Getter 
  56.     static class CacheResult { 
  57.         final CacheExtension cache; 
  58.         // 掷中的缓存功效 
  59.         final Map<Object, Object> hit; 
  60.         // 必要从头挪用源要领的keys 
  61.         private Set<Object> miss; 
  62.     } 

然后扩展CacheManager,没什么重写,就是自界说一种manager范例

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读