结构包:
使用案例:
实现效果:
1、基本并发的本地缓存,基于分布式轻量级锁的redis缓存
2、热缓存(高频访问持续缓存)+快速过期(本地缓存2秒,redis缓存10秒)
3、方法级别缓存清理 (@HybridCache 与@HybridChange 绑定管理缓存 )
4、基于HybridType接口的可扩展式作用域,目前已实现:全局、token
5、基于HybridLevel接口的可扩展式缓存处理,目前已实现:本地缓存、redis缓存
核心代码包:
@HybridCache
package com.*.server.live.core.hybridCache;
import com.*.server.live.core.hybridCache.impl.DepthLocal;
import com.*.server.live.core.hybridCache.impl.DepthRedis;
import java.lang.annotation.*;
/**
* 功能描述:多重缓存
* 作者:唐泽齐
* @case @HybridCache(scope = ScopeGlobal.class)
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface HybridCache {
/*缓存深度*/
Class<? extends HybridLevel> depth() default DepthRedis.class;
/*缓存广度*/
Class<? extends HybridType> scope();
}
@HybridCache
@HybridChange
package com.*.server.live.core.hybridCache; import com.*.server.live.core.hybridCache.impl.DepthRedis; import java.lang.annotation.*; /** * 功能描述:多重缓存更新 * 作者:唐泽齐 * @case @HybridChange(clazz = LiveStudioController.class,method = "getStudio",args = {Long.class}) * @case @HybridChange(clazz = LiveStudioController.class,method = "getStudio",args = {}) */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface HybridChange { /*关联类*/ Class<? extends Object> clazz(); /*关联方法*/ String method(); /*关联方法入参类型*/ Class<?>[] args(); } @HybridChange
HybridLevel
package com.*.server.live.core.hybridCache;
import org.aspectj.lang.ProceedingJoinPoint;
import javax.validation.constraints.NotNull;
/**
* 功能描述:多重缓存级别
* 作者:唐泽齐
*/
public interface HybridLevel {
/**
* 缓存
* @param key 缓存key
* @param id 混村ID
* @param o 缓存值
* @param self 是否执行节点
* @param joinPoint 节点
* @return
* @throws Throwable
*/
Object cache(@NotNull String key,@NotNull String id, Object o,@NotNull boolean self,@NotNull ProceedingJoinPoint joinPoint) throws Throwable;
/**
* 清缓存
* @param key 缓存key
* @param self 是否执行节点
* @param joinPoint 节点
* @return
* @throws Throwable
*/
Object del(@NotNull String key,@NotNull boolean self,@NotNull ProceedingJoinPoint joinPoint) throws Throwable;
}
HybridLevel
HybridType