Skip to content

Commit

Permalink
<feat>(light): Support config light cache instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
yizzuide committed Mar 16, 2020
1 parent fa5aabf commit 0115e8e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Milkomeda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<java.version>1.8</java.version>
<project.release.version>2.6.2-SNAPSHOT</project.release.version>
<project.release.version>2.7.0-SNAPSHOT</project.release.version>
<spring-boot.version>2.2.4</spring-boot.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<mybatis.starter.version>2.1.1</mybatis.starter.version>
Expand Down Expand Up @@ -65,7 +65,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>2.6.2</project.release.version>
<project.release.version>2.7.0</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* E:缓存业务数据
*
* @since 1.8.0
* @version 2.3.0
* @version 2.7.0
* @author yizzuide
* Create at 2019/06/28 13:33
*/
Expand Down Expand Up @@ -375,5 +375,21 @@ public void copyFrom(LightCache other) {
this.setStrategyClass(other.getStrategyClass());
this.setOnlyCacheL1(other.getOnlyCacheL1());
this.setL2Expire(other.getL2Expire());
this.setOnlyCacheL2(other.getOnlyCacheL2());
}

/**
* 配置实例
* @param props LightCache
*/
public void configFrom(LightProperties props) {
this.setL1MaxCount(props.getL1MaxCount());
this.setL1DiscardPercent(props.getL1DiscardPercent());
this.setL1Expire(props.getL1Expire());
this.setStrategy(props.getStrategy());
this.setStrategyClass(props.getStrategyClass());
this.setOnlyCacheL1(props.isOnlyCacheL1());
this.setL2Expire(props.getL2Expire());
this.setOnlyCacheL2(props.isOnlyCacheL2());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;
Expand All @@ -29,14 +30,17 @@
*
* @author yizzuide
* @since 2.0.0
* @version 2.6.2
* @version 2.7.0
* Create at 2019/12/18 14:45
*/
@Order(98)
@Aspect
public class LightCacheAspect {
public static final String DEFAULT_BEAN_NAME = "lightCache";

@Autowired
private LightProperties props;

@Around("execution(@LightCacheable * *.*(..)) && @annotation(cacheable)")
public Object cacheableAround(ProceedingJoinPoint joinPoint, LightCacheable cacheable) throws Throwable {
return applyAround(joinPoint, cacheable, cacheable.condition(), cacheable.value(), cacheable.keyPrefix(), cacheable.key());
Expand Down Expand Up @@ -75,7 +79,7 @@ private Object applyAround(ProceedingJoinPoint joinPoint, Annotation annotation,
customCacheFlag = true;
} else {
// 修改Bean name,防止与开发者项目里重复
cacheBeanName = DEFAULT_BEAN_NAME + "_" + cacheBeanName;
cacheBeanName = innerCacheBeanName(cacheBeanName);
cache = WebContext.registerBean((ConfigurableApplicationContext) ApplicationContextHolder.get(), cacheBeanName, LightCache.class);
}
// 针对LightCacheable类型的处理
Expand All @@ -101,6 +105,11 @@ private Object applyAround(ProceedingJoinPoint joinPoint, Annotation annotation,
}
}

// 缓存实例配置(优先级最高)
if (props.getInstances().containsKey(originCacheBeanName(cacheBeanName))) {
cache.configFrom(props.getInstances().get(originCacheBeanName(cacheBeanName)));
}

// key生成器
Function<Serializable, String> keyGenerator = id -> prefix + id;

Expand All @@ -119,4 +128,21 @@ private Object applyAround(ProceedingJoinPoint joinPoint, Annotation annotation,
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
return CacheHelper.get(cache, signature.getReturnType(), viewId, keyGenerator, id -> joinPoint.proceed());
}

/**
* 转为内部缓存Bean名
* @param cacheBeanName 原bean名
* @return 内部缓存Bean名
*/
private String innerCacheBeanName(String cacheBeanName) {
return DEFAULT_BEAN_NAME + "_" + cacheBeanName;
}

private String originCacheBeanName(String innerCacheBeanName) {
String prefix = DEFAULT_BEAN_NAME + "_";
if (innerCacheBeanName.startsWith(prefix)) {
return innerCacheBeanName.substring(prefix.length());
}
return innerCacheBeanName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author yizzuide
* @since 1.17.0
* @version 2.3.0
* @version 2.7.0
* Create at 2019/12/03 16:22
*/
@Configuration
Expand All @@ -33,14 +33,7 @@ public LightCacheAspect lightCacheAspect() {
@Bean(LightCacheAspect.DEFAULT_BEAN_NAME)
public Cache lightCache() {
LightCache lightCache = new LightCache();
lightCache.setL1MaxCount(props.getL1MaxCount());
lightCache.setL1DiscardPercent(props.getL1DiscardPercent());
lightCache.setL1Expire(props.getL1Expire());
lightCache.setStrategy(props.getStrategy());
lightCache.setStrategyClass(props.getStrategyClass());
lightCache.setOnlyCacheL1(props.isOnlyCacheL1());
lightCache.setL2Expire(props.getL2Expire());
lightCache.setOnlyCacheL2(props.isOnlyCacheL2());
lightCache.configFrom(props);
return lightCache;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @author yizzuide
* @since 1.17.0
* @version 2.6.2
* @version 2.7.0
* Create at 2019/12/03 16:24
*/
@Data
Expand Down Expand Up @@ -43,5 +43,5 @@ public class LightProperties {
/**
* 自定义实例名配置
*/
private Map<String, LightProperties> instance = new HashMap<>();
private Map<String, LightProperties> instances = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author yizzuide
* @since 2.2.0
* @version 2.6.2
* @version 2.7.0
* Create at 2019/12/31 18:13
*/
public class Moon<T> {
Expand Down Expand Up @@ -124,7 +124,7 @@ protected LeftHandPointer pluckLeftHandPointer(String key, LeftHandPointer leftH
* @param key 缓存key
* @return LeftHandPointer
*/
@LightCacheable(value = "#target.cacheName", keyPrefix = "moon:lhp-", key = "#key", expire = 86400, onlyCacheL2 = true)
@LightCacheable(value = "#target.cacheName", keyPrefix = "moon:lhp-", key = "#key", onlyCacheL2 = true)
protected LeftHandPointer getLeftHandPointer(String key) {
// 无法从缓存中获取时,创建新的左手指月
return new LeftHandPointer();
Expand Down
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<milkomeda.version>2.6.2-SNAPSHOT</milkomeda.version>
<milkomeda.version>2.7.0-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.github.yizzuide.milkomeda.demo.moon;

import com.github.yizzuide.milkomeda.light.LightCache;
import com.github.yizzuide.milkomeda.moon.Moon;
import com.github.yizzuide.milkomeda.moon.PercentMoonStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
* MoonConfig
*
Expand All @@ -16,8 +13,6 @@
*/
@Configuration
public class MoonConfig {
@Resource
private LightCache lightCache;

@Bean("smsMoon")
public Moon<String> smsMoon() {
Expand All @@ -26,22 +21,11 @@ public Moon<String> smsMoon() {
return moon;
}


@Bean("abTestLightCache")
public LightCache abTestLightCache() {
LightCache abTestLightCache = new LightCache();
abTestLightCache.copyFrom(lightCache);
abTestLightCache.setOnlyCacheL2(true);
abTestLightCache.setL2Expire(-1L);
return abTestLightCache;
}


@Bean("abTestMoon")
public Moon<Integer> abTestMoon() {
Moon<Integer> moon = new Moon<>();
// 设置L2缓存不过期
moon.setCacheName("abTestLightCache");
moon.setCacheName("ab-test");
moon.setMoonStrategy(new PercentMoonStrategy());
// AB测试阶段值:15%为0,85%为1
// moon.add(15, 85);
Expand Down
5 changes: 5 additions & 0 deletions MilkomedaDemo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ milkomeda:
l2-expire: 86400
# 全局使用时间线丢弃策略
strategy: timeline
instances:
# 缓存实例配置(没有配置的使用默认值),优先级比注解高
ab-test:
l2-expire: -1
only-cache-l2: true

echo:
# 设置请求读取超时
Expand Down

0 comments on commit 0115e8e

Please sign in to comment.