Skip to content

Commit

Permalink
<feat>(moon): Support custom light cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
yizzuide committed Mar 16, 2020
1 parent 26d7b2a commit fa5aabf
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 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.1-SNAPSHOT</project.release.version>
<project.release.version>2.6.2-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.1</project.release.version>
<project.release.version>2.6.2</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author yizzuide
* @since 2.0.0
* @version 2.3.0
* @version 2.6.2
* Create at 2019/12/18 14:45
*/
@Order(98)
Expand Down Expand Up @@ -62,6 +62,9 @@ private Object applyAround(ProceedingJoinPoint joinPoint, Annotation annotation,
throw new IllegalArgumentException(String.format("You must set key before use %s.", annotation.annotationType().getSimpleName()));
}

// 解析缓存实例名
cacheBeanName = extractValue(joinPoint, cacheBeanName);

// 解析表达式
String viewId = extractValue(joinPoint, key);
LightCache cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.Map;

/**
* LightProperties
*
* @author yizzuide
* @version 1.17.0
* @since 1.17.0
* @version 2.6.2
* Create at 2019/12/03 16:24
*/
@Data
Expand Down Expand Up @@ -36,4 +40,8 @@ public class LightProperties {

/** 只缓存在二级缓存上 */
private boolean onlyCacheL2 = false;
/**
* 自定义实例名配置
*/
private Map<String, LightProperties> instance = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
*
* @author yizzuide
* @since 2.2.0
* @version 2.6.0
* @version 2.6.2
* Create at 2019/12/31 18:13
*/
public class Moon<T> {
public static final String CACHE_NAME = "lightCacheMoon";

/**
* 缓存实例名
*/
@Setter @Getter
private String cacheName = CACHE_NAME;
/**
* 链表头指针
*/
Expand Down Expand Up @@ -108,7 +114,7 @@ public static <T> T getPhase(String key, Moon<T> prototype) {
* @param leftHandPointer 左手指月
* @return LeftHandPointer
*/
@LightCachePut(value = CACHE_NAME, keyPrefix = "moon:lhp-", key = "#key")
@LightCachePut(value = "#target.cacheName", keyPrefix = "moon:lhp-", key = "#key")
protected LeftHandPointer pluckLeftHandPointer(String key, LeftHandPointer leftHandPointer) {
return this.getMoonStrategy().pluck(this, leftHandPointer);
}
Expand All @@ -118,7 +124,7 @@ protected LeftHandPointer pluckLeftHandPointer(String key, LeftHandPointer leftH
* @param key 缓存key
* @return LeftHandPointer
*/
@LightCacheable(value = CACHE_NAME, keyPrefix = "moon:lhp-", key = "#key", expire = 86400, onlyCacheL2 = true)
@LightCacheable(value = "#target.cacheName", keyPrefix = "moon:lhp-", key = "#key", expire = 86400, 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.1-SNAPSHOT</milkomeda.version>
<milkomeda.version>2.6.2-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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 @@ -13,6 +16,9 @@
*/
@Configuration
public class MoonConfig {
@Resource
private LightCache lightCache;

@Bean("smsMoon")
public Moon<String> smsMoon() {
Moon<String> moon = new Moon<>();
Expand All @@ -21,9 +27,21 @@ public Moon<String> smsMoon() {
}


@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.setMoonStrategy(new PercentMoonStrategy());
// AB测试阶段值:15%为0,85%为1
// moon.add(15, 85);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ public ResponseEntity<?> update() {
return ResponseEntity.status(HttpStatus.OK).build();
}

@RequestMapping("startNow")
public ResponseEntity<?> startNow() {
Neutron.startJobNow(jobName);
return ResponseEntity.status(HttpStatus.OK).build();
}

}

0 comments on commit fa5aabf

Please sign in to comment.