Skip to content

Commit

Permalink
✨ 添加 mica-lite 模块。
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunMengLu committed Apr 17, 2021
1 parent 4a9d423 commit aeb851b
Show file tree
Hide file tree
Showing 27 changed files with 1,294 additions and 18 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -53,5 +53,4 @@ logs
Thumbs.db
Servers
.metadata
upload
gen_code
12 changes: 12 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,18 @@
# 变更记录

## 发行版本
### v2.4.5-GA - 2021-04-24
- :sparkles: 添加 mica-lite 模块。
- :sparkles: mica-captcha 中的 cache 改为每次读取, caffeine 会刷新,照成引用为 null。
- :sparkles: mica-metrics 优化 UndertowMetrics,待下版本完全重构。
- :sparkles: mica-core 添加网关通用 code。
- :sparkles: mica-logging 减少 reflections 日志。
- :sparkles: mica-core Pkcs7Encoder 中默认的 BLOCK_SIZE 改为 16 github #35 兼容更多编程语言。
- :bug: mica-caffeine 多 cache name 时报错。
- :memo: mica-qrcode 添加 base64 image 方法。
- :memo: mica-logging 添加阿里、腾讯云日志服务接入。
- :arrow_up: 升级 spring boot 到 2.4.5

### v2.4.4-GA - 2021-03-28
- :sparkles: mica-qrcode 新增模块,友好的二维码识别和生成
- :sparkles: mica-logging 重新设计,`logstash-logback-encoder` 调整为可选,`logstash``json` 需手动添加依赖
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -89,7 +89,7 @@ LGPL 是 GPL 的一个为主要为类库使用设计的开源协议。和 GPL

[![JetBrains](docs/img/jetbrains.png)](https://www.jetbrains.com/?from=mica)

感谢 `如梦技术VIP群` 小伙伴们的大力支持。
感谢 `如梦技术 VIP` **小伙伴们**的鼎力支持,更多 **VIP** 信息详见:https://www.dreamlu.net/vip/index.html

## 微信公众号

Expand Down
1 change: 1 addition & 0 deletions mica-bom/build.gradle
Expand Up @@ -31,6 +31,7 @@ dependencyManagement {
dependency "net.dreamlu:mica-caffeine:${VERSION}"
dependency "net.dreamlu:mica-logging:${VERSION}"
dependency "net.dreamlu:mica-qrcode:${VERSION}"
dependency "net.dreamlu:mica-lite:${VERSION}"
// commons
dependency "com.google.code.findbugs:jsr305:${findbugsVersion}"
dependency "io.swagger:swagger-annotations:${swaggerAnnotationsVersion}"
Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import net.dreamlu.mica.core.utils.ReflectUtil;
import net.dreamlu.mica.core.utils.StringPool;
import org.springframework.boot.convert.DurationStyle;
Expand All @@ -35,16 +36,16 @@
* @author L.cm
*/
public class CaffeineAutoCacheManager extends CaffeineCacheManager {
private static final Field CACHE_BUILDER_FIELD;
private static final Field CACHE_LOADER_FIELD;

static {
CACHE_BUILDER_FIELD = Objects.requireNonNull(ReflectUtil.getField(CaffeineCacheManager.class, "cacheBuilder"));
CACHE_BUILDER_FIELD.setAccessible(true);
CACHE_LOADER_FIELD = Objects.requireNonNull(ReflectUtil.getField(CaffeineCacheManager.class, "cacheLoader"));
CACHE_LOADER_FIELD.setAccessible(true);
}

@Nullable
private CaffeineSpec caffeineSpec = null;

public CaffeineAutoCacheManager() {
super();
}
Expand All @@ -53,15 +54,6 @@ public CaffeineAutoCacheManager(String... cacheNames) {
super(cacheNames);
}

@SuppressWarnings("unchecked")
protected Caffeine<Object, Object> getCacheBuilder() {
try {
return (Caffeine<Object, Object>) CACHE_BUILDER_FIELD.get(this);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}

@Nullable
@SuppressWarnings("unchecked")
protected CacheLoader<Object, Object> getCacheLoader() {
Expand All @@ -72,6 +64,18 @@ protected CacheLoader<Object, Object> getCacheLoader() {
}
}

@Override
public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
super.setCaffeineSpec(caffeineSpec);
this.caffeineSpec = caffeineSpec;
}

@Override
public void setCacheSpecification(String cacheSpecification) {
super.setCacheSpecification(cacheSpecification);
this.caffeineSpec = CaffeineSpec.parse(cacheSpecification);
}

/**
* Build a common Caffeine Cache instance for the specified cache name,
* using the common Caffeine configuration specified on this cache manager.
Expand All @@ -88,7 +92,12 @@ protected com.github.benmanes.caffeine.cache.Cache<Object, Object> createNativeC
}
// 转换时间,支持时间单位例如:300ms,第二个参数是默认单位
Duration duration = DurationStyle.detectAndParse(cacheArray[1], ChronoUnit.SECONDS);
Caffeine<Object, Object> cacheBuilder = getCacheBuilder();
Caffeine<Object, Object> cacheBuilder;
if (this.caffeineSpec != null) {
cacheBuilder = Caffeine.from(caffeineSpec);
} else {
cacheBuilder = Caffeine.newBuilder();
}
CacheLoader<Object, Object> cacheLoader = getCacheLoader();
if (cacheLoader == null) {
return cacheBuilder.expireAfterAccess(duration).build();
Expand Down
Expand Up @@ -33,26 +33,38 @@
public class SpringCacheCaptchaCache implements ICaptchaCache, InitializingBean {
private final MicaCaptchaProperties properties;
private final CacheManager cacheManager;
private Cache captchaCache;

@Override
public void put(String cacheName, String uuid, String value) {
Cache captchaCache = getCache();
captchaCache.put(uuid, value);
}

@Override
public String getAndRemove(String cacheName, String uuid) {
Cache captchaCache = getCache();
String value = captchaCache.get(uuid, String.class);
if (value != null) {
captchaCache.evict(uuid);
}
return value;
}

/**
* 发现 caffeine 中会刷新会导致引用为 null
*
* @return Cache
*/
private Cache getCache() {
String cacheName = properties.getCacheName();
return cacheManager.getCache(cacheName);
}

@Override
public void afterPropertiesSet() throws Exception {
String cacheName = properties.getCacheName();
Cache cache = cacheManager.getCache(cacheName);
this.captchaCache = Objects.requireNonNull(cache, "mica-captcha spring cache name " + cacheName + " is null.");
Objects.requireNonNull(cache, "mica-captcha spring cache name " + cacheName + " is null.");
}

}
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dreamlu.mica.core.context;

import java.util.function.Function;

/**
* 上下文,默认给个空的
*
* @author L.cm
*/
public class DefaultMicaContext implements IMicaContext {

@Override
public String getRequestId() {
return null;
}

@Override
public String getTenantId() {
return null;
}

@Override
public String getAccountId() {
return null;
}

@Override
public String get(String ctxKey) {
return null;
}

@Override
public <T> T get(String ctxKey, Function<String, T> function) {
return null;
}

}
24 changes: 24 additions & 0 deletions mica-lite/README.md
@@ -0,0 +1,24 @@
# mica-lite 组件

基于 VIP 版 mica-boot 抽取部分功能,使得 Spring boot 单服务可以直接使用。

## 功能
- 全局异常处理。
- 文件上传目录配置。
- 启动信息打印。

## 使用
### maven
```xml
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-lite</artifactId>
<version>${version}</version>
</dependency>
```

### gradle
```groovy
compile("net.dreamlu:mica-lite:${version}")
```

6 changes: 6 additions & 0 deletions mica-lite/build.gradle
@@ -1,3 +1,9 @@
dependencies {
api project(":mica-core")
api "org.springframework.boot:spring-boot-starter-validation"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-webflux"
compileOnly "org.springframework.cloud:spring-cloud-context"
compileOnly "net.dreamlu:mica-auto:${micaAutoVersion}"
annotationProcessor "net.dreamlu:mica-auto:${micaAutoVersion}"
}

0 comments on commit aeb851b

Please sign in to comment.