Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
c52b3f75
mica
项目概览
mica
/
mica
通知
10
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c52b3f75
编写于
12月 09, 2021
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' into mica-v2.6.x
# Conflicts: # build.gradle # gradle.properties
上级
91328954
893bd91a
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
266 addition
and
60 deletion
+266
-60
CHANGELOG.md
CHANGELOG.md
+13
-0
README.md
README.md
+1
-1
README_EN.md
README_EN.md
+1
-1
mica-core/src/main/java/net/dreamlu/mica/core/retry/IRetry.java
...ore/src/main/java/net/dreamlu/mica/core/retry/IRetry.java
+40
-0
mica-core/src/main/java/net/dreamlu/mica/core/retry/RetryCallback.java
.../main/java/net/dreamlu/mica/core/retry/RetryCallback.java
+25
-0
mica-core/src/main/java/net/dreamlu/mica/core/retry/SimpleRetry.java
...rc/main/java/net/dreamlu/mica/core/retry/SimpleRetry.java
+93
-0
mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
...e/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
+3
-3
mica-core/src/main/java/net/dreamlu/mica/core/utils/RsaUtil.java
...re/src/main/java/net/dreamlu/mica/core/utils/RsaUtil.java
+2
-2
mica-http/README.md
mica-http/README.md
+12
-12
mica-http/build.gradle
mica-http/build.gradle
+0
-1
mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
...http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
+25
-11
mica-http/src/main/java/net/dreamlu/mica/http/InMemoryCookieManager.java
...ain/java/net/dreamlu/mica/http/InMemoryCookieManager.java
+1
-1
mica-http/src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
...src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
+6
-19
mica-http/src/test/java/net/dreamlu/HttpRequestProxyTest.java
...-http/src/test/java/net/dreamlu/HttpRequestProxyTest.java
+1
-1
mica-redis/src/main/java/net/dreamlu/mica/redis/cache/MicaRedisCache.java
...ain/java/net/dreamlu/mica/redis/cache/MicaRedisCache.java
+43
-8
未找到文件。
CHANGELOG.md
浏览文件 @
c52b3f75
# 变更记录
## 发行版本
### v2.5.7 - 2021-11-28
-
:sparkles: mica-redis 优化,方便自定义序列化。
-
:sparkles: mica-xss 优化,避免 xss 关闭时被类扫描,导致 bean 找不到异常。
-
:sparkles: mica-core 添加 retry 接口。
-
:sparkles: mica-http 代码优化去掉 spring retry 依赖。
-
:memo: mica-redis 优化 README.md 文档。
-
:memo: mica-http 更新 README.md 文档。
-
:arrow_up: Upgrading dependencies.
-
:arrow_up: 升级 Gradle 到 7.3。
-
:arrow_up: 升级 lombok 到 1.18.22。
-
:arrow_up: 升级 Spring boot 到 2.5.7。
-
:arrow_up: 升级 mica-weixin 到 2.1.2。
### v2.5.6 - 2021-10-28
-
:sparkles: mica-redis 优化 MicaRedisCache bean 名称。
-
:bug: mica-redis rpush、lpush 修复,优化 MicaRedisCache 方法泛型。
...
...
README.md
浏览文件 @
c52b3f75
...
...
@@ -13,7 +13,7 @@
## 🔖 版本说明
| 最新版本 | mica 版本 | spring boot 版本 | spring cloud 版本 |
| ----------- | ---------------- | ---------------- | ----------------- |
| 2.5.
5
| mica 2.5.x | 2.5.x | 2020 |
| 2.5.
7
| mica 2.5.x | 2.5.x | 2020 |
| 2.4.11 | mica 2.4.x | 2.4.x | 2020 |
| 2.1.1-GA | mica 2.0.x~2.1.x | 2.2.x ~ 2.3.x | Hoxton |
...
...
README_EN.md
浏览文件 @
c52b3f75
...
...
@@ -15,7 +15,7 @@ English | [简体中文](README.md)
| Latest version | mica version | spring boot version | spring cloud version |
| -------------- | ---------------- | ------------------- | -------------------- |
| 2.5.
5
| mica 2.5.x | 2.5.x | 2020 |
| 2.5.
7
| mica 2.5.x | 2.5.x | 2020 |
| 2.4.11 | mica 2.4.x | 2.4.x | 2020 |
| 2.1.1-GA | mica 2.0.x~2.1.x | 2.2.x ~ 2.3.x | Hoxton |
...
...
mica-
http/src/main/java/net/dreamlu/mica/http/RetryPolic
y.java
→
mica-
core/src/main/java/net/dreamlu/mica/core/retry/IRetr
y.java
浏览文件 @
c52b3f75
/*
* Copyright (c) 2019-2029, Dreamlu (596392912@qq.com & www.dreamlu.net).
* 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.
...
...
@@ -14,45 +14,27 @@
* limitations under the License.
*/
package
net.dreamlu.mica.
http
;
package
net.dreamlu.mica.
core.retry
;
import
lombok.Getter
;
import
lombok.ToString
;
import
org.springframework.retry.policy.SimpleRetryPolicy
;
import
javax.annotation.Nullable
;
import
java.util.function.Predicate
;
import
net.dreamlu.mica.core.function.CheckedCallable
;
/**
* 重试
策略
* 重试
接口
*
* @author
dream.lu
* @author
L.cm
*/
@Getter
@ToString
public
class
RetryPolicy
{
public
static
final
RetryPolicy
INSTANCE
=
new
RetryPolicy
();
private
final
int
maxAttempts
;
private
final
long
sleepMillis
;
@Nullable
private
final
Predicate
<
ResponseSpec
>
respPredicate
;
public
RetryPolicy
()
{
this
(
null
);
}
public
RetryPolicy
(
int
maxAttempts
,
long
sleepMillis
)
{
this
(
maxAttempts
,
sleepMillis
,
null
);
}
public
RetryPolicy
(
@Nullable
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
(
SimpleRetryPolicy
.
DEFAULT_MAX_ATTEMPTS
,
0L
,
respPredicate
);
}
public
interface
IRetry
{
/**
* Execute the supplied {@link CheckedCallable} with the configured retry semantics. See
* implementations for configuration details.
*
* @param <T> the return value
* @param retryCallback the {@link CheckedCallable}
* @param <E> the exception thrown
* @return T the return value
* @throws E the exception thrown
*/
<
T
,
E
extends
Throwable
>
T
execute
(
RetryCallback
<
T
,
E
>
retryCallback
)
throws
E
;
public
RetryPolicy
(
int
maxAttempts
,
long
sleepMillis
,
@Nullable
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
.
maxAttempts
=
maxAttempts
;
this
.
sleepMillis
=
sleepMillis
;
this
.
respPredicate
=
respPredicate
;
}
}
mica-core/src/main/java/net/dreamlu/mica/core/retry/RetryCallback.java
0 → 100644
浏览文件 @
c52b3f75
package
net.dreamlu.mica.core.retry
;
import
java.io.Serializable
;
/**
* Callback interface for an operation that can be retried using a
*
* @param <T> the type of object returned by the callback
* @param <E> the type of exception it declares may be thrown
* @author Rob Harrop
* @author Dave Syer
*/
public
interface
RetryCallback
<
T
,
E
extends
Throwable
>
extends
Serializable
{
/**
* Execute an operation with retry semantics. Operations should generally be
* idempotent, but implementations may choose to implement compensation semantics when
* an operation is retried.
*
* @return the result of the successful operation.
* @throws E of type E if processing fails
*/
T
call
()
throws
E
;
}
mica-core/src/main/java/net/dreamlu/mica/core/retry/SimpleRetry.java
0 → 100644
浏览文件 @
c52b3f75
/*
* 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.retry
;
import
lombok.extern.slf4j.Slf4j
;
import
net.dreamlu.mica.core.utils.Exceptions
;
import
net.dreamlu.mica.core.utils.ThreadUtil
;
import
java.io.IOException
;
/**
* 简单的 retry 重试
*
* @author L.cm
*/
@Slf4j
public
final
class
SimpleRetry
implements
IRetry
{
/**
* The default limit to the number of attempts for a new policy.
*/
public
final
static
int
DEFAULT_MAX_ATTEMPTS
=
3
;
/**
* Default back off period - 1ms.
*/
private
static
final
long
DEFAULT_BACK_OFF_PERIOD
=
1L
;
/**
* 重试次数
*/
private
final
int
maxAttempts
;
/**
* 重试时间间隔
*/
private
final
long
sleepMillis
;
public
SimpleRetry
()
{
this
(
DEFAULT_MAX_ATTEMPTS
,
DEFAULT_BACK_OFF_PERIOD
);
}
public
SimpleRetry
(
int
maxAttempts
)
{
this
(
maxAttempts
,
DEFAULT_BACK_OFF_PERIOD
);
}
public
SimpleRetry
(
int
maxAttempts
,
long
sleepMillis
)
{
this
.
maxAttempts
=
maxAttempts
;
this
.
sleepMillis
=
(
sleepMillis
>
0
?
sleepMillis
:
1
);
}
public
int
getMaxAttempts
()
{
return
maxAttempts
;
}
public
long
getSleepMillis
()
{
return
sleepMillis
;
}
@Override
public
<
T
,
E
extends
Throwable
>
T
execute
(
RetryCallback
<
T
,
E
>
retryCallback
)
throws
E
{
int
retryCount
;
Throwable
lastThrowable
=
null
;
for
(
int
i
=
0
;
i
<
maxAttempts
;
i
++)
{
try
{
return
retryCallback
.
call
();
}
catch
(
Throwable
e
)
{
retryCount
=
i
+
1
;
log
.
warn
(
"retry on {} times error{}."
,
retryCount
,
e
.
getMessage
());
lastThrowable
=
e
;
if
(
sleepMillis
>
0
&&
retryCount
<
maxAttempts
)
{
ThreadUtil
.
sleep
(
sleepMillis
);
}
}
}
if
(
lastThrowable
==
null
)
{
lastThrowable
=
new
IOException
(
"retry on "
+
maxAttempts
+
" times,still fail."
);
}
throw
Exceptions
.
unchecked
(
lastThrowable
);
}
}
mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
浏览文件 @
c52b3f75
...
...
@@ -413,7 +413,7 @@ public class JsonUtil {
* @return 集合
*/
public
static
<
T
>
List
<
T
>
readList
(
@Nullable
String
content
,
Class
<
T
>
elementClass
)
{
if
(
ObjectUtil
.
isEmpty
(
content
))
{
if
(
StringUtil
.
isBlank
(
content
))
{
return
Collections
.
emptyList
();
}
try
{
...
...
@@ -521,7 +521,7 @@ public class JsonUtil {
* @return 集合
*/
public
static
<
K
,
V
>
Map
<
K
,
V
>
readMap
(
@Nullable
InputStream
content
,
Class
<?>
keyClass
,
Class
<?>
valueClass
)
{
if
(
ObjectUtil
.
isEmpty
(
content
)
)
{
if
(
content
==
null
)
{
return
Collections
.
emptyMap
();
}
try
{
...
...
@@ -542,7 +542,7 @@ public class JsonUtil {
* @return 集合
*/
public
static
<
K
,
V
>
Map
<
K
,
V
>
readMap
(
@Nullable
String
content
,
Class
<?>
keyClass
,
Class
<?>
valueClass
)
{
if
(
ObjectUtil
.
isEmpty
(
content
))
{
if
(
StringUtil
.
isBlank
(
content
))
{
return
Collections
.
emptyMap
();
}
try
{
...
...
mica-core/src/main/java/net/dreamlu/mica/core/utils/RsaUtil.java
浏览文件 @
c52b3f75
...
...
@@ -197,7 +197,7 @@ public class RsaUtil {
}
/**
*
共要
加密
*
公钥
加密
*
* @param base64PublicKey base64 的公钥
* @param data 待加密的内容
...
...
@@ -208,7 +208,7 @@ public class RsaUtil {
}
/**
*
共要
加密
*
公钥
加密
*
* @param publicKey 公钥
* @param data 待加密的内容
...
...
mica-http/README.md
浏览文件 @
c52b3f75
...
...
@@ -11,7 +11,7 @@
</dependency>
```
spring-retry 为可选依赖,用来对 http 结果断言重试。
注意:
**2.5.7 开始已经不再需要**
spring-retry 为可选依赖,用来对 http 结果断言重试。
```
xml
<dependency>
<groupId>
org.springframework.retry
</groupId>
...
...
@@ -41,17 +41,17 @@ HttpRequest.setGlobalLog(HttpLogger.Console, LogLevel.BODY);
```
java
// 同步请求 url,方法支持 get、post、patch、put、delete
HttpRequest
.
get
(
"https://www.baidu.com"
)
.
useSlf4jLog
()
// 使用 Slf4j 日志,同类的有 .useConsoleLog(),日志级别为 BODY
.
addHeader
(
"x-account-id"
,
"mica001"
)
// 添加 header
.
addCookie
(
builder
->
builder
.
domain
(
"www.baidu.com"
).
name
(
"name"
).
value
(
"value"
))
// 添加 cookie
.
query
(
"q"
,
"mica"
)
// 设置 url 参数,默认进行 url encode
.
queryEncoded
(
"name"
,
"encodedValue"
)
.
formBuilder
()
// 表单构造器,同类 multipartFormBuilder 文件上传表单
.
add
(
"id"
,
123123
)
// 表单参数
.
retryOn
(
responseSpec
->
!
responseSpec
.
isOk
())
// 结合 spring retry 进行结果集断言
.
proxy
(
InetSocketAddress
.
createUnresolved
(
"127.0.0.1"
,
8080
))
// 设置代理
.
execute
()
// 发起请求
.
asJsonNode
();
// 结果集转换,注:如果网络异常等会直接抛出异常。
.
useSlf4jLog
()
// 使用 Slf4j 日志,同类的有 .useConsoleLog(),日志级别为 BODY
.
addHeader
(
"x-account-id"
,
"mica001"
)
// 添加 header
.
addCookie
(
builder
->
builder
.
domain
(
"www.baidu.com"
).
name
(
"name"
).
value
(
"value"
))
// 添加 cookie
.
query
(
"q"
,
"mica"
)
// 设置 url 参数,默认进行 url encode
.
queryEncoded
(
"name"
,
"encodedValue"
)
.
retryOn
(
responseSpec
->
!
responseSpec
.
isOk
())
// 结合 spring retry 进行结果集断言
.
proxy
(
InetSocketAddress
.
createUnresolved
(
"127.0.0.1"
,
8080
))
// 设置代理
.
formBuilder
()
// 表单构造器,同类 multipartFormBuilder 文件上传表单
.
add
(
"id"
,
123123
)
// 表单参数
.
execute
()
// 发起请求
.
asJsonNode
();
// 结果集转换,注:如果网络异常等会直接抛出异常。
// 同类的方法有 asString、asBytes
// json 类响应:asJsonNode、asValue、asList、asMap、atJsonPath、,采用 jackson 处理
// file 文件:toFile
...
...
mica-http/build.gradle
浏览文件 @
c52b3f75
...
...
@@ -4,5 +4,4 @@ dependencies {
}
api
"com.squareup.okhttp3:okhttp:${okhttpVersion}"
api
"com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"
implementation
"org.springframework.retry:spring-retry"
}
mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
浏览文件 @
c52b3f75
...
...
@@ -16,6 +16,8 @@
package
net.dreamlu.mica.http
;
import
net.dreamlu.mica.core.retry.IRetry
;
import
net.dreamlu.mica.core.retry.SimpleRetry
;
import
net.dreamlu.mica.core.ssl.DisableValidationTrustManager
;
import
net.dreamlu.mica.core.ssl.TrustAllHostNames
;
import
net.dreamlu.mica.core.utils.Exceptions
;
...
...
@@ -28,7 +30,10 @@ import okhttp3.internal.http.HttpMethod;
import
okhttp3.logging.HttpLoggingInterceptor
;
import
javax.annotation.Nullable
;
import
javax.net.ssl.*
;
import
javax.net.ssl.HostnameVerifier
;
import
javax.net.ssl.SSLContext
;
import
javax.net.ssl.SSLSocketFactory
;
import
javax.net.ssl.X509TrustManager
;
import
java.net.InetSocketAddress
;
import
java.net.Proxy
;
import
java.net.ProxySelector
;
...
...
@@ -92,7 +97,9 @@ public class HttpRequest {
@Nullable
private
Authenticator
proxyAuthenticator
;
@Nullable
private
RetryPolicy
retryPolicy
;
private
IRetry
retry
;
@Nullable
private
Predicate
<
ResponseSpec
>
respPredicate
;
@Nullable
private
Boolean
disableSslValidation
;
@Nullable
...
...
@@ -279,8 +286,8 @@ public class HttpRequest {
if
(
followSslRedirects
!=
null
)
{
builder
.
followSslRedirects
(
followSslRedirects
);
}
if
(
retry
Policy
!=
null
)
{
builder
.
addInterceptor
(
new
RetryInterceptor
(
retry
Policy
));
if
(
retry
!=
null
)
{
builder
.
addInterceptor
(
new
RetryInterceptor
(
retry
,
respPredicate
));
}
if
(
logger
!=
null
&&
logLevel
!=
null
&&
HttpLoggingInterceptor
.
Level
.
NONE
!=
logLevel
)
{
builder
.
addInterceptor
(
getLoggingInterceptor
(
logger
,
logLevel
));
...
...
@@ -482,22 +489,29 @@ public class HttpRequest {
}
public
HttpRequest
retry
()
{
this
.
retryPolicy
=
RetryPolicy
.
INSTANCE
;
return
this
;
return
retry
(
new
SimpleRetry
());
}
public
HttpRequest
retryOn
(
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
.
retryPolicy
=
new
RetryPolicy
(
respPredicate
);
return
this
;
return
retry
(
new
SimpleRetry
(),
respPredicate
);
}
public
HttpRequest
retry
(
int
maxAttempts
,
long
sleepMillis
)
{
this
.
retryPolicy
=
new
RetryPolicy
(
maxAttempts
,
sleepMillis
);
return
this
;
return
retry
(
new
SimpleRetry
(
maxAttempts
,
sleepMillis
));
}
public
HttpRequest
retry
(
int
maxAttempts
,
long
sleepMillis
,
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
.
retryPolicy
=
new
RetryPolicy
(
maxAttempts
,
sleepMillis
);
return
retry
(
new
SimpleRetry
(
maxAttempts
,
sleepMillis
),
respPredicate
);
}
public
HttpRequest
retry
(
IRetry
retry
)
{
this
.
retry
=
retry
;
return
this
;
}
public
HttpRequest
retry
(
IRetry
retry
,
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
.
retry
=
retry
;
this
.
respPredicate
=
respPredicate
;
return
this
;
}
...
...
mica-http/src/main/java/net/dreamlu/mica/http/InMemoryCookieManager.java
浏览文件 @
c52b3f75
...
...
@@ -57,7 +57,7 @@ public class InMemoryCookieManager implements CookieJar {
}
// 清除过期 cookie
if
(!
needRemoveCookieList
.
isEmpty
())
{
cookieSet
.
removeAll
(
needRemoveCookieList
);
needRemoveCookieList
.
forEach
(
cookieSet:
:
remove
);
}
return
matchedCookieList
;
}
...
...
mica-http/src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
浏览文件 @
c52b3f75
...
...
@@ -17,14 +17,13 @@
package
net.dreamlu.mica.http
;
import
lombok.RequiredArgsConstructor
;
import
net.dreamlu.mica.core.retry.IRetry
;
import
okhttp3.Interceptor
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
okhttp3.ResponseBody
;
import
org.springframework.retry.backoff.FixedBackOffPolicy
;
import
org.springframework.retry.policy.SimpleRetryPolicy
;
import
org.springframework.retry.support.RetryTemplate
;
import
javax.annotation.Nullable
;
import
java.io.IOException
;
import
java.util.function.Predicate
;
...
...
@@ -35,16 +34,16 @@ import java.util.function.Predicate;
*/
@RequiredArgsConstructor
public
class
RetryInterceptor
implements
Interceptor
{
private
final
RetryPolicy
retryPolicy
;
private
final
IRetry
retry
;
@Nullable
private
final
Predicate
<
ResponseSpec
>
respPredicate
;
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
Request
request
=
chain
.
request
();
RetryTemplate
template
=
createRetryTemplate
(
retryPolicy
);
return
template
.
execute
(
context
->
{
return
retry
.
execute
(()
->
{
Response
response
=
chain
.
proceed
(
request
);
// 结果集校验
Predicate
<
ResponseSpec
>
respPredicate
=
retryPolicy
.
getRespPredicate
();
if
(
respPredicate
==
null
)
{
return
response
;
}
...
...
@@ -59,16 +58,4 @@ public class RetryInterceptor implements Interceptor {
});
}
private
static
RetryTemplate
createRetryTemplate
(
RetryPolicy
policy
)
{
RetryTemplate
template
=
new
RetryTemplate
();
// 重试策略
SimpleRetryPolicy
retryPolicy
=
new
SimpleRetryPolicy
();
retryPolicy
.
setMaxAttempts
(
policy
.
getMaxAttempts
());
// 设置间隔策略
FixedBackOffPolicy
backOffPolicy
=
new
FixedBackOffPolicy
();
backOffPolicy
.
setBackOffPeriod
(
policy
.
getSleepMillis
());
template
.
setRetryPolicy
(
retryPolicy
);
template
.
setBackOffPolicy
(
backOffPolicy
);
return
template
;
}
}
mica-http/src/test/java/net/dreamlu/HttpRequestProxyTest.java
浏览文件 @
c52b3f75
...
...
@@ -29,7 +29,7 @@ public class HttpRequestProxyTest {
// 代理都不可用
HttpRequest
.
get
(
"https://www.baidu.com"
)
.
useConsoleLog
(
LogLevel
.
BASIC
)
//
.retry()
.
retry
()
.
proxySelector
(
new
MicaProxySelector
())
.
execute
()
.
asString
();
...
...
mica-redis/src/main/java/net/dreamlu/mica/redis/cache/MicaRedisCache.java
浏览文件 @
c52b3f75
...
...
@@ -28,6 +28,7 @@ import java.time.Duration;
import
java.util.*
;
import
java.util.concurrent.TimeUnit
;
import
java.util.function.Consumer
;
import
java.util.function.Function
;
import
java.util.function.Supplier
;
/**
...
...
@@ -77,6 +78,17 @@ public class MicaRedisCache {
valueOps
.
set
(
key
,
value
);
}
/**
* 存放 key value 对到 redis,用于自定义序列化的方式
*
* @param key redis key
* @param mapper 序列化转换
* @param <T> 泛型
*/
public
<
T
>
void
set
(
String
key
,
T
value
,
Function
<
T
,
byte
[]>
mapper
)
{
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
redis
->
redis
.
set
(
keySerialize
(
key
),
mapper
.
apply
(
value
)));
}
/**
* 存放 key value 对到 redis,并将 key 的生存时间设为 seconds (以秒为单位)。
* 如果 key 已经存在, SETEX 命令将覆写旧值。
...
...
@@ -115,16 +127,32 @@ public class MicaRedisCache {
return
(
T
)
valueOps
.
get
(
key
);
}
/**
* 返回 key 所关联的 value 值
*
* @param key redis key
* @param mapper 函数式
* @param <T> 泛型
* @return T
*/
@Nullable
public
<
T
>
T
get
(
String
key
,
Function
<
byte
[],
T
>
mapper
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
T
>)
redis
->
{
byte
[]
value
=
redis
.
get
(
keySerialize
(
key
));
if
(
value
==
null
)
{
return
null
;
}
return
mapper
.
apply
(
value
);
});
}
/**
* 返回 key 所关联的 value 值,采用 jdk 序列化
* 如果 key 不存在那么返回特殊值 nil 。
*/
@Nullable
public
<
T
>
T
getByJdkSer
(
String
key
)
{
return
(
T
)
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
redis
->
{
byte
[]
bytes
=
redis
.
get
(
keySerialize
(
key
));
return
RedisSerializer
.
java
().
deserialize
(
bytes
);
});
return
get
(
key
,
(
value
)
->
(
T
)
RedisSerializer
.
java
().
deserialize
(
value
));
}
/**
...
...
@@ -133,10 +161,7 @@ public class MicaRedisCache {
*/
@Nullable
public
<
T
>
T
getByJsonSer
(
String
key
,
Class
<
T
>
clazz
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
T
>)
redis
->
{
byte
[]
valueBytes
=
redis
.
get
(
keySerialize
(
key
));
return
JsonUtil
.
readValue
(
valueBytes
,
clazz
);
});
return
get
(
key
,
(
value
)
->
JsonUtil
.
readValue
(
value
,
clazz
));
}
/**
...
...
@@ -435,6 +460,16 @@ public class MicaRedisCache {
});
}
/**
* 返回集合中元素的数量。
* @param key redis key
* @return 数量
*/
@Nullable
public
Long
sCard
(
String
key
)
{
return
setOps
.
size
(
key
);
}
/**
* 同时设置一个或多个 key-value 对。
* 如果某个给定 key 已经存在,那么 MSET 会用新值覆盖原来的旧值,如果这不是你所希望的效果,请考虑使用 MSETNX 命令:它只会在所有给定 key 都不存在的情况下进行设置操作。
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录