Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-concurrent-action
Project
Project
Details
Activity
Releases
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
刘凯
java-concurrent-action
Commits
9872c420
Commit
9872c420
authored
Dec 17, 2018
by
刘凯
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加caffenie测试
parent
fda81b5c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
0 deletions
+98
-0
build.gradle
build.gradle
+2
-0
CaffenieTest.java
src/test/java/com/example/demo/caffenie/CaffenieTest.java
+96
-0
No files found.
build.gradle
View file @
9872c420
...
...
@@ -30,5 +30,7 @@ dependencies {
compile
(
"com.google.guava:guava:18.0"
)
compile
'com.github.ben-manes.caffeine:caffeine:2.6.2'
}
src/test/java/com/example/demo/caffenie/CaffenieTest.java
0 → 100644
View file @
9872c420
package
com
.
example
.
demo
.
caffenie
;
import
com.github.benmanes.caffeine.cache.AsyncLoadingCache
;
import
com.github.benmanes.caffeine.cache.Cache
;
import
com.github.benmanes.caffeine.cache.CacheLoader
;
import
com.github.benmanes.caffeine.cache.Caffeine
;
import
com.github.benmanes.caffeine.cache.LoadingCache
;
import
org.junit.Test
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeoutException
;
/**
* @author liukai
* @Description:
* @date 2018/12/177:39 PM
*/
public
class
CaffenieTest
{
/**
* 同步加载写法,注意是LoadingCache
*
* 1.查询缓存不存在时会加载build里面的回调方法,并把非空的数据放入缓存
* 2.如果为空可以在回掉函数中写null,但是每次获取如果为空都会调用回调函数中的逻辑
*/
private
LoadingCache
<
String
,
String
>
loadingCache
=
Caffeine
.
newBuilder
()
.
expireAfterWrite
(
1
,
TimeUnit
.
MINUTES
)
//cache的初始容量值
.
initialCapacity
(
5
)
//maximumSize用来控制cache的最大缓存数量,maximumSize和maximumWeight不可以同时使用
.
maximumSize
(
100
)
.
build
(
new
CacheLoader
<
String
,
String
>()
{
@Override
public
String
load
(
String
key
)
throws
Exception
{
return
key
;
}
});
/**
* 异步加载,注意是AsyncLoadingCache
* 1.查询缓存不存在时会加载build里面的回调方法,并把非空的数据放入缓存
* 2.如果为空可以在回掉函数中写null,但是每次获取如果为空都会调用回调函数中的逻辑
*/
private
AsyncLoadingCache
<
String
,
String
>
asyncLoadingCache
=
Caffeine
.
newBuilder
()
.
expireAfterWrite
(
1
,
TimeUnit
.
MINUTES
)
//cache的初始容量值
.
initialCapacity
(
5
)
//maximumSize用来控制cache的最大缓存数量,maximumSize和maximumWeight不可以同时使用
.
maximumSize
(
100
)
.
buildAsync
(
new
CacheLoader
<
String
,
String
>()
{
@Override
public
String
load
(
String
key
)
throws
Exception
{
return
key
;
}
});
/**
* 手动加载,注意是Cache
* 如果缓存中不存在该键,则调用这个 Function 函数,并将返回值作为该缓存的值插入缓存中。
* get 方法是以阻塞方式执行调用,即使多个线程同时请求该值也只会调用一次Function方法。
* 这样可以避免与其他线程的写入竞争,这也是为什么使用get优于getIfPresent的原因
*
* 1.查询缓存不存在时会加载build里面的回调方法,并把非空的数据放入缓存
* 2.如果为空可以在回掉函数中写null,但是每次获取如果为空都会调用回调函数中的逻辑
* 3.refreshAfterWrite 再次访问才能刷新,如果没人访问,那么永远也不会刷新
*/
private
Cache
<
String
,
String
>
manualCache
=
Caffeine
.
newBuilder
()
.
expireAfterWrite
(
1
,
TimeUnit
.
MINUTES
)
.
initialCapacity
(
5
)
.
maximumSize
(
100
)
.
build
();
@Test
public
void
aaa
(){
try
{
String
key
=
"name1"
;
CompletableFuture
<
String
>
completableFuture
=
asyncLoadingCache
.
get
(
key
);
String
a
=
completableFuture
.
get
(
1
,
TimeUnit
.
SECONDS
);
System
.
out
.
println
(
a
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
catch
(
ExecutionException
e
)
{
e
.
printStackTrace
();
}
catch
(
TimeoutException
e
)
{
e
.
printStackTrace
();
}
}
}
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