分布式多级缓存(本地缓存,redis缓存)

打印 上一主题 下一主题

主题 974|帖子 974|积分 2922

结构包:

 
 
 使用案例:

 
 
 实现效果:
1、基本并发的本地缓存,基于分布式轻量级锁的redis缓存
2、热缓存(高频访问持续缓存)+快速过期(本地缓存2秒,redis缓存10秒)
3、方法级别缓存清理 (@HybridCache 与@HybridChange 绑定管理缓存 )
4、基于HybridType接口的可扩展式作用域,目前已实现:全局、token
5、基于HybridLevel接口的可扩展式缓存处理,目前已实现:本地缓存、redis缓存
核心代码包:
  1. package com.*.server.live.core.hybridCache;
  2. import com.*.server.live.core.hybridCache.impl.DepthLocal;
  3. import com.*.server.live.core.hybridCache.impl.DepthRedis;
  4. import java.lang.annotation.*;
  5. /**
  6. * 功能描述:多重缓存
  7. * 作者:唐泽齐
  8. * @case @HybridCache(scope = ScopeGlobal.class)
  9. */
  10. @Documented
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Target({ElementType.METHOD})
  13. public @interface HybridCache {
  14.     /*缓存深度*/
  15.     Class<? extends HybridLevel> depth() default DepthRedis.class;
  16.     /*缓存广度*/
  17.     Class<? extends HybridType> scope();
  18. }
复制代码
@HybridChange
  1. package com.*.server.live.core.hybridCache;
  2. import com.*.server.live.core.hybridCache.impl.DepthRedis;
  3. import java.lang.annotation.*;
  4. /**
  5. * 功能描述:多重缓存更新
  6. * 作者:唐泽齐
  7. * @case @HybridChange(clazz = LiveStudioController.class,method = "getStudio",args = {Long.class})
  8. * @case @HybridChange(clazz = LiveStudioController.class,method = "getStudio",args = {})
  9. */
  10. @Documented
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Target({ElementType.METHOD})
  13. public @interface HybridChange {
  14.     /*关联类*/
  15.     Class<? extends Object> clazz();
  16.     /*关联方法*/
  17.     String method();
  18.     /*关联方法入参类型*/
  19.     Class<?>[] args();
  20. }
复制代码
HybridLevel
  1. package com.*.server.live.core.hybridCache;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import javax.validation.constraints.NotNull;
  4. /**
  5. * 功能描述:多重缓存级别
  6. * 作者:唐泽齐
  7. */
  8. public interface HybridLevel {
  9.     /**
  10.      * 缓存
  11.      * @param key 缓存key
  12.      * @param id 混村ID
  13.      * @param o 缓存值
  14.      * @param self 是否执行节点
  15.      * @param joinPoint 节点
  16.      * @return
  17.      * @throws Throwable
  18.      */
  19.     Object cache(@NotNull String key,@NotNull String id, Object o,@NotNull boolean self,@NotNull ProceedingJoinPoint joinPoint) throws Throwable;
  20.     /**
  21.      * 清缓存
  22.      * @param key 缓存key
  23.      * @param self 是否执行节点
  24.      * @param joinPoint 节点
  25.      * @return
  26.      * @throws Throwable
  27.      */
  28.     Object del(@NotNull String key,@NotNull boolean self,@NotNull ProceedingJoinPoint joinPoint) throws Throwable;
  29. }
复制代码
HybridType
  1. package com.*.server.live.core.hybridCache;
  2. /**
  3. * 功能描述:多重缓存级别
  4. * 作者:唐泽齐
  5. */
  6. public interface HybridType {
  7.     String token();
  8. }
复制代码
HybridCacheInterceptor 扩展代码包:
  1. package com.*.server.live.core.hybridCache;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.nacos.common.util.Md5Utils;
  4. import org.aspectj.lang.ProceedingJoinPoint;
  5. import org.aspectj.lang.annotation.Around;
  6. import org.aspectj.lang.annotation.Aspect;
  7. import org.aspectj.lang.reflect.MethodSignature;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.annotation.Configuration;
  10. import javax.annotation.Resource;
  11. import java.lang.reflect.Method;
  12. /**
  13. * 功能描述:多重缓存接口
  14. * 作者:唐泽齐
  15. */
  16. @Aspect
  17. @Configuration
  18. public class HybridCacheInterceptor {
  19.     @Resource
  20.     ApplicationContext applicationContext;
  21.     final static String cache = "hybridCache:";
  22.     @Around(value = "@annotation(com.*.server.live.core.hybridCache.HybridCache)")
  23.     public Object cache(ProceedingJoinPoint joinPoint) throws Throwable {
  24.         MethodSignature ms = (MethodSignature) joinPoint.getSignature();
  25.         Method method = ms.getMethod();
  26.         HybridCache cacheAnnotation = method.getAnnotation(HybridCache.class);
  27.         String key = getCacheKey(method);
  28.         String id = getCacheId(method,joinPoint.getArgs());
  29.         HybridLevel hybridLevel = applicationContext.getBean(cacheAnnotation.depth());
  30.         return hybridLevel.cache(key, id, null, true, joinPoint);
  31.     }
  32.     @Around(value = "@annotation(com.*.server.live.core.hybridCache.HybridChange)")
  33.     public Object del(ProceedingJoinPoint joinPoint) throws Throwable {
  34.         MethodSignature ms = (MethodSignature) joinPoint.getSignature();
  35.         Method method = ms.getMethod();
  36.         HybridChange cacheAnnotation = method.getAnnotation(HybridChange.class);
  37.         try {
  38.             Method method1 = cacheAnnotation.clazz().getMethod(cacheAnnotation.method(),cacheAnnotation.args());
  39.             HybridCache cache = method1.getAnnotation(HybridCache.class);
  40.             String key = getCacheKey(method1);
  41.             HybridLevel hybridLevel = applicationContext.getBean(cache.depth());
  42.             return hybridLevel.del(key,true, joinPoint);
  43.         } catch (Exception e) {
  44.             return joinPoint.proceed();
  45.         }
  46.     }
  47.     /*获取缓存key*/
  48.     private String getCacheKey(Method method) {
  49.         return cache + method.getDeclaringClass().getSimpleName() + ":" + method.getName();
  50.     }
  51.     ;
  52.     /*获取缓存id*/
  53.     private String getCacheId(Method method,Object[] args) {
  54.         HybridCache cacheAnnotation = method.getAnnotation(HybridCache.class);
  55.         HybridType hybridType = applicationContext.getBean(cacheAnnotation.scope());
  56.         return Md5Utils.getMD5((hybridType.token() + JSON.toJSONString(args)).getBytes());
  57.     }
  58.     ;
  59. }
复制代码
DepthLocal [code]package com.*.server.live.core.hybridCache.impl;import cn.hutool.core.util.RandomUtil;import com.*.common.redis.service.RedisService;import com.*.server.live.core.hybridCache.HybridLevel;import org.aspectj.lang.ProceedingJoinPoint;import org.springframework.stereotype.Component;import javax.annotation.Resource;/** * 功能描述:redis缓存 * 作者:唐泽齐 */@Componentpublic class DepthRedis implements HybridLevel {    @Resource    private RedisService redisService;    @Resource    private HybridLevel depthLocal;    @Override    public synchronized Object cache(String key, String id, Object o,boolean search, ProceedingJoinPoint joinPoint) throws Throwable {        o = depthLocal.cache(key, id, o,false,joinPoint);        String lock = getLock(key, id);        if(search && o== null && (o=redisService.hget(key , id)) == null ) {            for (;;) {                if(redisService.incr(lock,1l)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表