Skip to content

Commit

Permalink
fix(light) Fixed super cache not emptied with dynamic light cache type.
Browse files Browse the repository at this point in the history
  • Loading branch information
yizzuide committed Oct 21, 2020
1 parent 80bef35 commit 7b4811d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 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>3.12.3-SNAPSHOT</project.release.version>
<project.release.version>3.12.4-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 @@ -67,7 +67,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>3.12.3</project.release.version>
<project.release.version>3.12.4</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author yizzuide
* @since 1.14.0
* @version 3.3.0
* @version 3.12.4
* Create at 2019/11/11 14:56
*/
@Configuration
Expand Down Expand Up @@ -69,6 +69,7 @@ public Cache lightCache() {
lightCache.setOnlyCacheL1(!crustProps.isEnableCacheL2());
lightCache.setL2Expire(lightProps.getL2Expire().getSeconds());
lightCache.setOnlyCacheL2(false);
lightCache.setEnableSuperCache(lightProps.isEnableSuperCache());
return lightCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* 缓存外层API,集超级缓存、一级缓存、二级缓存于一体的方法
*
* @since 1.10.0
* @version 3.2.1
* @version 3.12.4
* @author yizzuide
* Create at 2019/07/02 11:36
*/
Expand Down Expand Up @@ -136,7 +136,7 @@ public static <E> E get(Cache cache, TypeReference<E> eTypeRef, Serializable id
Spot<Serializable, E> fastSpot = null;
if (cache instanceof LightCache) {
LightCache lightCache = (LightCache) cache;
if (!lightCache.getOnlyCacheL2()) {
if (lightCache.isEnableSuperCache() && !lightCache.getOnlyCacheL2()) {
// 方案一:从超级缓存中获取,内存指针引用即可返回(耗时为O(1))
fastSpot = get(cache);
if (fastSpot != null) {
Expand Down Expand Up @@ -217,7 +217,7 @@ public static <E> E put(Cache cache, Serializable id,
Spot<Serializable, E> fastSpot = null;
if (cache instanceof LightCache) {
LightCache lightCache = (LightCache) cache;
if (!lightCache.getOnlyCacheL2()) {
if (lightCache.isEnableSuperCache() && !lightCache.getOnlyCacheL2()) {
fastSpot = get(cache);
if (fastSpot == null) {
// 设置超级缓存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* E:缓存业务数据
*
* @since 1.8.0
* @version 3.7.0
* @version 3.12.4
* @author yizzuide
* Create at 2019/06/28 13:33
*/
Expand Down Expand Up @@ -92,6 +92,13 @@ public class LightCache implements Cache {
@Getter
private Boolean onlyCacheL2;

/**
* 开启超缓存
*/
@Setter
@Getter
private boolean enableSuperCache;

/**
* 超级缓存(每个Cache都有自己的超级缓存,互不影响)
*/
Expand Down Expand Up @@ -379,6 +386,7 @@ public void copyFrom(LightCache other) {
this.setOnlyCacheL1(other.getOnlyCacheL1());
this.setL2Expire(other.getL2Expire());
this.setOnlyCacheL2(other.getOnlyCacheL2());
this.setEnableSuperCache(other.isEnableSuperCache());
}

/**
Expand All @@ -394,5 +402,6 @@ public void configFrom(LightProperties props) {
this.setOnlyCacheL1(props.isOnlyCacheL1());
this.setL2Expire(props.getL2Expire().getSeconds());
this.setOnlyCacheL2(props.isOnlyCacheL2());
this.setEnableSuperCache(props.isEnableSuperCache());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
* 超级缓存清理
*
* @author yizzuide
* @since 3.3.0
* @since 3.12.4
* Create at 2020/05/06 14:13
*/
public class LightCacheCleanAstrolabeHandler implements AstrolabeHandler {

@Override
public void postHandle(ServletRequest request, ServletResponse response) {
// 清除请求线程的所有Cache子实例的超级缓存
Map<String, Cache> cacheMap = ApplicationContextHolder.get().getBeansOfType(Cache.class);
for (Cache cache : cacheMap.values()) {
CacheHelper.remove(cache);
Map<String, LightCache> cacheMap = ApplicationContextHolder.get().getBeansOfType(LightCache.class);
for (LightCache cache : cacheMap.values()) {
if (cache.isEnableSuperCache()) {
CacheHelper.remove(cache);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author yizzuide
* @since 1.17.0
* @version 3.0.0
* @version 3.12.4
* Create at 2019/12/03 16:24
*/
@Data
Expand Down Expand Up @@ -49,4 +49,10 @@ public class LightProperties {
* 自定义实例名配置(实例的注册方式为首次使用时)
*/
private Map<String, LightProperties> instances = new HashMap<>();

/**
* 是否开启超缓存
* @since 3.12.4
*/
private boolean enableSuperCache = true;
}
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<milkomeda.version>3.12.3-SNAPSHOT</milkomeda.version>
<milkomeda.version>3.12.4-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
<redission.version>3.12.5</redission.version>
<zookeeper.version>3.4.14</zookeeper.version>
Expand Down

0 comments on commit 7b4811d

Please sign in to comment.