Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐少华
midjourney-proxy
Commits
64adc3bf
Commit
64adc3bf
authored
Mar 01, 2022
by
guiq
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug: [none] getEbookPdf4Wechat 接口优化
parent
2908e4a4
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
164 additions
and
0 deletions
+164
-0
pom.xml
pcloud-common/pom.xml
+5
-0
CaffeineUtils.java
...com/pcloud/common/utils/cache/caffeine/CaffeineUtils.java
+78
-0
ExceptionUtils.java
...va/com/pcloud/common/utils/exceptioin/ExceptionUtils.java
+81
-0
No files found.
pcloud-common/pom.xml
View file @
64adc3bf
...
...
@@ -404,6 +404,11 @@
<artifactId>
nacos-spring-context
</artifactId>
</dependency>
<dependency>
<groupId>
com.github.ben-manes.caffeine
</groupId>
<artifactId>
caffeine
</artifactId>
</dependency>
</dependencies>
</project>
pcloud-common/src/main/java/com/pcloud/common/utils/cache/caffeine/CaffeineUtils.java
0 → 100644
View file @
64adc3bf
package
com
.
pcloud
.
common
.
utils
.
cache
.
caffeine
;
import
com.github.benmanes.caffeine.cache.Cache
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.MapUtils
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.Set
;
import
java.util.function.Function
;
import
java.util.function.Supplier
;
public
final
class
CaffeineUtils
{
private
CaffeineUtils
()
{
}
private
static
<
K
,
V
>
void
putAll
(
Cache
<
K
,
V
>
cache
,
Map
<
K
,
V
>
m
)
{
if
(
MapUtils
.
isNotEmpty
(
m
))
{
cache
.
putAll
(
m
);
}
}
public
static
<
K
,
V
>
Map
<
K
,
V
>
getByCache
(
Cache
<
K
,
V
>
cache
,
List
<
K
>
keyList
,
Function
<
Set
<
K
>,
Map
<
K
,
V
>>
function
)
{
Map
<
K
,
V
>
result
=
new
HashMap
<>();
if
(
CollectionUtils
.
isEmpty
(
keyList
))
{
return
result
;
}
Set
<
K
>
needQueryIds
=
new
HashSet
<>();
Map
<
K
,
V
>
mapInCache
=
new
HashMap
<>();
keyList
.
forEach
((
key
)
->
{
V
cacheValue
=
cache
.
getIfPresent
(
key
);
if
(
cacheValue
!=
null
)
{
mapInCache
.
put
(
key
,
cacheValue
);
}
else
{
needQueryIds
.
add
(
key
);
}
});
if
(
CollectionUtils
.
isEmpty
(
needQueryIds
))
{
return
mapInCache
;
}
Map
<
K
,
V
>
queryResult
=
function
.
apply
(
needQueryIds
);
// 放入缓存
putAll
(
cache
,
queryResult
);
// 汇总结果集合
result
.
putAll
(
mapInCache
);
result
.
putAll
(
queryResult
);
return
result
;
}
public
static
<
K
,
V
>
Optional
<
V
>
getByCache
(
Cache
<
K
,
V
>
cache
,
K
key
,
Supplier
<
V
>
supplier
)
{
if
(
key
==
null
)
{
return
Optional
.
empty
();
}
V
cacheValue
=
cache
.
getIfPresent
(
key
);
if
(
cacheValue
!=
null
)
{
return
Optional
.
of
(
cacheValue
);
}
V
queryResult
=
supplier
.
get
();
if
(
queryResult
!=
null
)
{
// 放入缓存
cache
.
put
(
key
,
queryResult
);
}
return
Optional
.
ofNullable
(
queryResult
);
}
}
pcloud-common/src/main/java/com/pcloud/common/utils/exceptioin/ExceptionUtils.java
0 → 100644
View file @
64adc3bf
package
com
.
pcloud
.
common
.
utils
.
exceptioin
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.function.Failable
;
import
org.apache.commons.lang3.function.FailableRunnable
;
import
org.apache.commons.lang3.function.FailableSupplier
;
import
java.util.function.Consumer
;
/**
* 异常处理工具类
*/
@Slf4j
public
final
class
ExceptionUtils
{
private
ExceptionUtils
()
{
}
/**
* 出现错误时,返回默认值
*
* @param supplier (可能抛出异常的)带返回值的操作
* @param onError 错误时的处理方法
* @param defaultValue 错误时返回的默认值
* @param <T> 类型
* @return 成功返回执行结果,失败返回默认值
*/
public
static
<
T
>
T
defaultWhenException
(
final
FailableSupplier
<
T
,
?
extends
Exception
>
supplier
,
final
Consumer
<
Exception
>
onError
,
final
T
defaultValue
)
{
try
{
return
Failable
.
get
(
supplier
);
}
catch
(
final
Exception
e
)
{
onError
.
accept
(
e
);
return
defaultValue
;
}
}
/**
* 出现错误时,返回默认值, 默认打印warn日志
*
* @param supplier (可能抛出异常的)带返回值的操作
* @param defaultValue 错误时返回的默认值
* @param <T> 类型
* @return 成功返回执行结果,失败返回默认值
*/
public
static
<
T
>
T
defaultWhenException
(
final
FailableSupplier
<
T
,
?
extends
Exception
>
supplier
,
final
T
defaultValue
)
{
try
{
return
Failable
.
get
(
supplier
);
}
catch
(
final
Exception
e
)
{
log
.
warn
(
"[{}] 操作失败! ERR:{}"
,
Thread
.
currentThread
().
getStackTrace
()[
2
].
getMethodName
(),
e
.
getMessage
(),
e
);
return
defaultValue
;
}
}
/**
* 出现错误时,执行指定方法
*
* @param runnable (可能抛出异常的)无返回值的操作
* @param onError 错误时无返回值的处理方法
*/
public
static
void
runWhenException
(
final
FailableRunnable
<?
extends
Exception
>
runnable
,
final
Consumer
<
Exception
>
onError
)
{
try
{
Failable
.
run
(
runnable
);
}
catch
(
final
Exception
e
)
{
onError
.
accept
(
e
);
}
}
/**
* 出现错误时,忽略异常,打印warn日志
*
* @param runnable (可能抛出异常的)无返回值的操作
*/
public
static
void
runWhenException
(
final
FailableRunnable
<?
extends
Exception
>
runnable
)
{
try
{
Failable
.
run
(
runnable
);
}
catch
(
final
Exception
e
)
{
log
.
warn
(
"[{}] 操作失败! ERR:{}"
,
Thread
.
currentThread
().
getStackTrace
()[
2
].
getMethodName
(),
e
.
getMessage
(),
e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment