Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Scala Macro Tools
提交
c1d76a8c
S
Scala Macro Tools
项目概览
梦境迷离
/
Scala Macro Tools
上一次同步 大约 1 年
通知
8
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Scala Macro Tools
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
c1d76a8c
编写于
2月 16, 2023
作者:
梦境迷离
提交者:
GitHub
2月 16, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimize cache (#273)
Signed-off-by:
梦境迷离
<
dreamylost@outlook.com
>
上级
79189947
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
68 addition
and
267 deletion
+68
-267
smt-cache/src/main/scala/org/bitlap/cache/Cache.scala
smt-cache/src/main/scala/org/bitlap/cache/Cache.scala
+14
-39
smt-cache/src/main/scala/org/bitlap/cache/CacheAdapter.scala
smt-cache/src/main/scala/org/bitlap/cache/CacheAdapter.scala
+3
-3
smt-cache/src/main/scala/org/bitlap/cache/CacheKeySerde.scala
...cache/src/main/scala/org/bitlap/cache/CacheKeySerde.scala
+7
-7
smt-cache/src/main/scala/org/bitlap/cache/CacheRef.scala
smt-cache/src/main/scala/org/bitlap/cache/CacheRef.scala
+7
-7
smt-cache/src/main/scala/org/bitlap/cache/CacheStrategy.scala
...cache/src/main/scala/org/bitlap/cache/CacheStrategy.scala
+1
-1
smt-cache/src/main/scala/org/bitlap/cache/GenericCache.scala
smt-cache/src/main/scala/org/bitlap/cache/GenericCache.scala
+13
-51
smt-cache/src/main/scala/org/bitlap/cache/package.scala
smt-cache/src/main/scala/org/bitlap/cache/package.scala
+0
-30
smt-cache/src/test/scala/org/bitlap/cache/CacheCustomCaseSpec.scala
...src/test/scala/org/bitlap/cache/CacheCustomCaseSpec.scala
+8
-29
smt-cache/src/test/scala/org/bitlap/cache/CacheImplicits.scala
...ache/src/test/scala/org/bitlap/cache/CacheImplicits.scala
+0
-39
smt-cache/src/test/scala/org/bitlap/cache/CacheKeyBuilderSpec.scala
...src/test/scala/org/bitlap/cache/CacheKeyBuilderSpec.scala
+2
-2
smt-cache/src/test/scala/org/bitlap/cache/CacheSpec.scala
smt-cache/src/test/scala/org/bitlap/cache/CacheSpec.scala
+13
-59
未找到文件。
smt-cache/src/main/scala/org/bitlap/cache/Cache.scala
浏览文件 @
c1d76a8c
...
...
@@ -31,59 +31,34 @@ import scala.concurrent.{ ExecutionContext, Future }
*/
object
Cache
{
def
getAsyncCache
[
K
,
T
<:
Product
](
implicit
def
apply
[
K
,
T
<:
Product
](
implicit
cache
:
Aux
[
K
,
T
,
Future
],
executionContext
:
ExecutionContext
,
key
Builder
:
CacheKeyBuilder
[
K
]
key
Serde
:
CacheKeySerde
[
K
]
)
:
CacheRef
[
K
,
T
,
Future
]
=
new
CacheRef
[
K
,
cache.Out
,
Future
]
{
override
def
batchPut
T
(
data
:
=>
Map
[
K
,
cache.Out
])
:
Future
[
Unit
]
=
override
def
batchPut
F
(
data
:
=>
Map
[
K
,
cache.Out
])
:
Future
[
Unit
]
=
cache
.
putAll
(
data
)
override
def
get
T
(
key
:
K
)
:
Future
[
Option
[
cache.Out
]]
=
override
def
get
F
(
key
:
K
)
:
Future
[
Option
[
cache.Out
]]
=
cache
.
get
(
key
)
override
def
put
T
(
key
:
K
,
value
:
cache.Out
)
:
Future
[
Unit
]
=
override
def
put
F
(
key
:
K
,
value
:
cache.Out
)
:
Future
[
Unit
]
=
cache
.
put
(
key
,
value
)
override
def
clear
()
:
Future
[
Unit
]
=
cache
.
clear
()
override
def
clear
F
()
:
Future
[
Unit
]
=
cache
.
clear
()
override
def
remove
(
key
:
K
)
:
Future
[
Unit
]
=
cache
.
remove
(
key
)
override
def
remove
F
(
key
:
K
)
:
Future
[
Unit
]
=
cache
.
remove
(
key
)
override
def
getAll
T
:
Future
[
Map
[
K
,
cache.Out
]]
=
cache
.
getAll
override
def
getAll
F
:
Future
[
Map
[
K
,
cache.Out
]]
=
cache
.
getAll
override
def
safeRefreshT
(
allNewData
:
Map
[
K
,
cache.Out
])
:
Future
[
Unit
]
=
this
.
getAllT
.
map
{
t
=>
val
invalidData
=
t
.
keySet
.
filterNot
(
allNewData
.
keySet
)
this
.
batchPutT
(
allNewData
).
map
(
_
=>
invalidData
.
foreach
(
this
.
remove
))
override
def
refreshF
(
allNewData
:
Map
[
K
,
cache.Out
])
:
Future
[
Unit
]
=
this
.
synchronized
{
this
.
getAllF
.
map
{
t
=>
val
invalidData
=
t
.
keySet
.
filterNot
(
allNewData
.
keySet
)
this
.
batchPutF
(
allNewData
).
map
(
_
=>
invalidData
.
foreach
(
this
.
removeF
))
}
}
}
def
getSyncCache
[
K
,
T
<:
Product
](
implicit
cache
:
Aux
[
K
,
T
,
Identity
],
keyBuilder
:
CacheKeyBuilder
[
K
]
)
:
CacheRef
[
K
,
T
,
Identity
]
=
new
CacheRef
[
K
,
cache.Out
,
Identity
]
{
override
def
batchPutT
(
data
:
=>
Map
[
K
,
cache.Out
])
:
Identity
[
Unit
]
=
data
.
foreach
(
kv
=>
cache
.
put
(
kv
.
_1
,
kv
.
_2
))
override
def
getT
(
key
:
K
)
:
Identity
[
Option
[
cache.Out
]]
=
cache
.
get
(
key
)
override
def
putT
(
key
:
K
,
value
:
cache.Out
)
:
Identity
[
Unit
]
=
cache
.
put
(
key
,
value
)
override
def
clear
()
:
Identity
[
Unit
]
=
cache
.
clear
()
override
def
getAllT
:
Identity
[
Map
[
K
,
cache.Out
]]
=
cache
.
getAll
override
def
remove
(
key
:
K
)
:
Identity
[
Unit
]
=
cache
.
remove
(
key
)
override
def
safeRefreshT
(
allNewData
:
Map
[
K
,
cache.Out
])
:
Identity
[
Unit
]
=
{
val
invalidData
=
this
.
getAllT
.
keySet
.
filterNot
(
allNewData
.
keySet
)
this
.
batchPutT
(
allNewData
)
invalidData
.
foreach
(
this
.
remove
)
}
}
}
smt-cache/src/main/scala/org/bitlap/cache/CacheAdapter.scala
浏览文件 @
c1d76a8c
...
...
@@ -31,7 +31,7 @@ import java.util.Collections
*/
trait
CacheAdapter
[
V
]
{
def
getAllK
eys
:
Set
[
String
]
def
k
eys
:
Set
[
String
]
def
batchPut
(
data
:
Map
[
String
,
V
])
:
Unit
...
...
@@ -61,7 +61,7 @@ object CacheAdapter {
class
LruHashMapCacheAdapter
[
V
](
underlyingCache
:
java.util.Map
[
String
,
V
])
extends
CacheAdapter
[
V
]
{
override
def
getAllK
eys
:
Set
[
String
]
=
underlyingCache
.
keySet
().
asScala
.
toSet
override
def
k
eys
:
Set
[
String
]
=
underlyingCache
.
keySet
().
asScala
.
toSet
override
def
batchPut
(
data
:
Map
[
String
,
V
])
:
Unit
=
underlyingCache
.
putAll
(
data
.
asJava
)
...
...
@@ -77,7 +77,7 @@ object CacheAdapter {
class
ConcurrentMapCacheAdapter
[
V
](
underlyingCache
:
java.util.concurrent.ConcurrentMap
[
String
,
V
])
extends
CacheAdapter
[
V
]
{
override
def
getAllK
eys
:
Set
[
String
]
=
underlyingCache
.
keySet
().
asScala
.
toSet
override
def
k
eys
:
Set
[
String
]
=
underlyingCache
.
keySet
().
asScala
.
toSet
override
def
batchPut
(
data
:
Map
[
String
,
V
])
:
Unit
=
underlyingCache
.
putAll
(
data
.
asJava
)
...
...
smt-cache/src/main/scala/org/bitlap/cache/CacheKey
Builder
.scala
→
smt-cache/src/main/scala/org/bitlap/cache/CacheKey
Serde
.scala
浏览文件 @
c1d76a8c
...
...
@@ -27,39 +27,39 @@ import java.util.UUID
* 梦境迷离
* @version 1.0,6/8/22
*/
trait
CacheKey
Builder
[
T
]
{
trait
CacheKey
Serde
[
T
]
{
def
serialize
(
key
:
T
)
:
String
def
deserialize
(
key
:
String
)
:
T
}
object
CacheKey
Builder
{
object
CacheKey
Serde
{
implicit
val
intKey
:
CacheKey
Builder
[
Int
]
=
new
CacheKeyBuilder
[
Int
]
{
implicit
val
intKey
:
CacheKey
Serde
[
Int
]
=
new
CacheKeySerde
[
Int
]
{
override
def
serialize
(
key
:
Int
)
:
String
=
key
.
toString
override
def
deserialize
(
key
:
String
)
:
Int
=
key
.
toInt
}
implicit
val
stringKey
:
CacheKey
Builder
[
String
]
=
new
CacheKeyBuilder
[
String
]
{
implicit
val
stringKey
:
CacheKey
Serde
[
String
]
=
new
CacheKeySerde
[
String
]
{
override
def
serialize
(
key
:
String
)
:
String
=
key
override
def
deserialize
(
key
:
String
)
:
String
=
key
}
implicit
val
longKey
:
CacheKey
Builder
[
Long
]
=
new
CacheKeyBuilder
[
Long
]
{
implicit
val
longKey
:
CacheKey
Serde
[
Long
]
=
new
CacheKeySerde
[
Long
]
{
override
def
serialize
(
key
:
Long
)
:
String
=
key
.
toString
override
def
deserialize
(
key
:
String
)
:
Long
=
key
.
toLong
}
implicit
val
doubleKey
:
CacheKey
Builder
[
Double
]
=
new
CacheKeyBuilder
[
Double
]
{
implicit
val
doubleKey
:
CacheKey
Serde
[
Double
]
=
new
CacheKeySerde
[
Double
]
{
override
def
serialize
(
key
:
Double
)
:
String
=
String
.
valueOf
(
key
)
override
def
deserialize
(
key
:
String
)
:
Double
=
key
.
toDouble
}
implicit
val
uuidKey
:
CacheKey
Builder
[
UUID
]
=
new
CacheKeyBuilder
[
UUID
]
{
implicit
val
uuidKey
:
CacheKey
Serde
[
UUID
]
=
new
CacheKeySerde
[
UUID
]
{
override
def
serialize
(
key
:
UUID
)
:
String
=
key
.
toString
override
def
deserialize
(
key
:
String
)
:
UUID
=
UUID
.
fromString
(
key
)
...
...
smt-cache/src/main/scala/org/bitlap/cache/CacheRef.scala
浏览文件 @
c1d76a8c
...
...
@@ -27,17 +27,17 @@ package org.bitlap.cache
*/
trait
CacheRef
[
In
,
T
<:
Product
,
F
[
_
]]
{
def
batchPut
T
(
data
:
=>
Map
[
In
,
T
])
:
F
[
Unit
]
def
batchPut
F
(
data
:
=>
Map
[
In
,
T
])
:
F
[
Unit
]
def
get
T
(
key
:
In
)
:
F
[
Option
[
T
]]
def
get
F
(
key
:
In
)
:
F
[
Option
[
T
]]
def
put
T
(
key
:
In
,
value
:
T
)
:
F
[
Unit
]
def
put
F
(
key
:
In
,
value
:
T
)
:
F
[
Unit
]
def
getAll
T
:
F
[
Map
[
In
,
T
]]
def
getAll
F
:
F
[
Map
[
In
,
T
]]
def
clear
()
:
F
[
Unit
]
def
clear
F
()
:
F
[
Unit
]
def
remove
(
key
:
In
)
:
F
[
Unit
]
def
remove
F
(
key
:
In
)
:
F
[
Unit
]
def
safeRefreshT
(
allNewData
:
Map
[
In
,
T
])
:
F
[
Unit
]
def
refreshF
(
allNewData
:
Map
[
In
,
T
])
:
F
[
Unit
]
}
smt-cache/src/main/scala/org/bitlap/cache/CacheStrategy.scala
浏览文件 @
c1d76a8c
...
...
@@ -25,7 +25,7 @@ package org.bitlap.cache
* 梦境迷离
* @version 1.0,6/8/22
*/
trait
CacheStrategy
sealed
trait
CacheStrategy
object
CacheStrategy
{
...
...
smt-cache/src/main/scala/org/bitlap/cache/GenericCache.scala
浏览文件 @
c1d76a8c
...
...
@@ -31,17 +31,17 @@ sealed trait GenericCache[K, F[_]] {
type
Out
<:
Product
def
get
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
F
[
Option
[
Out
]]
def
get
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
F
[
Option
[
Out
]]
def
put
(
key
:
K
,
value
:
Out
)(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
F
[
Unit
]
def
put
(
key
:
K
,
value
:
Out
)(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
F
[
Unit
]
def
putAll
(
map
:
Map
[
K
,
Out
])(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
F
[
Unit
]
def
putAll
(
map
:
Map
[
K
,
Out
])(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
F
[
Unit
]
def
getAll
(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
F
[
Map
[
K
,
Out
]]
def
getAll
(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
F
[
Map
[
K
,
Out
]]
def
clear
()
:
F
[
Unit
]
def
remove
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
F
[
Unit
]
def
remove
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
F
[
Unit
]
}
...
...
@@ -49,71 +49,33 @@ object GenericCache {
type
Aux
[
K
,
Out0
,
F
[
_
]]
=
GenericCache
[
K
,
F
]
{
type
Out
=
Out0
}
def
apply
[
K
,
Out0
<:
Product
](
cacheStrategy
:
CacheStrategy
)
:
Aux
[
K
,
Out0
,
Identity
]
=
new
GenericCache
[
K
,
Identity
]
{
private
val
cacheAdapter
:
CacheAdapter
[
Out0
]
=
CacheAdapter
.
adapted
[
Out0
](
cacheStrategy
)
override
type
Out
=
Out0
override
def
get
(
key
:
K
)(
implicit
keyBuilder
:
CacheKeyBuilder
[
K
]
)
:
Identity
[
Option
[
Out
]]
=
{
val
v
=
cacheAdapter
.
get
(
keyBuilder
.
serialize
(
key
))
if
(
v
==
null
)
None
else
Option
(
v
)
}
override
def
put
(
key
:
K
,
value
:
Out
)(
implicit
keyBuilder
:
CacheKeyBuilder
[
K
]
)
:
Identity
[
Unit
]
=
cacheAdapter
.
put
(
keyBuilder
.
serialize
(
key
),
value
)
override
def
putAll
(
map
:
Map
[
K
,
Out0
])(
implicit
keyBuilder
:
CacheKeyBuilder
[
K
])
:
Identity
[
Unit
]
=
cacheAdapter
.
batchPut
(
map
.
map
(
kv
=>
keyBuilder
.
serialize
(
kv
.
_1
)
->
kv
.
_2
))
override
def
clear
()
:
Identity
[
Unit
]
=
cacheAdapter
.
clear
()
override
def
getAll
(
implicit
keyBuilder
:
CacheKeyBuilder
[
K
])
:
Identity
[
Map
[
K
,
Out0
]]
=
cacheAdapter
.
getAllKeys
.
map
(
key
=>
keyBuilder
.
deserialize
(
key
)
->
cacheAdapter
.
get
(
key
))
.
collect
{
case
(
k
,
out
)
if
out
!=
null
=>
k
->
out
}
.
toMap
override
def
remove
(
key
:
K
)(
implicit
keyBuilder
:
CacheKeyBuilder
[
K
])
:
Identity
[
Unit
]
=
cacheAdapter
.
remove
(
keyBuilder
.
serialize
(
key
))
}
def
apply
[
K
,
Out0
<:
Product
](
cacheStrategy
:
CacheStrategy
,
executionContext
:
ExecutionContext
)
:
Aux
[
K
,
Out0
,
Future
]
=
cacheStrategy
:
CacheStrategy
)(
implicit
executionContext
:
ExecutionContext
)
:
Aux
[
K
,
Out0
,
Future
]
=
new
GenericCache
[
K
,
Future
]
{
implicit
val
ec
=
executionContext
private
val
cacheAdapter
:
CacheAdapter
[
Out0
]
=
CacheAdapter
.
adapted
[
Out0
](
cacheStrategy
)
override
type
Out
=
Out0
override
def
get
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
Future
[
Option
[
Out
]]
=
override
def
get
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
Future
[
Option
[
Out
]]
=
Future
{
val
v
=
cacheAdapter
.
get
(
keyBuilder
.
serialize
(
key
))
if
(
v
==
null
)
None
else
Option
(
v
)
}
def
put
(
key
:
K
,
value
:
Out
)(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
Future
[
Unit
]
=
def
put
(
key
:
K
,
value
:
Out
)(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
Future
[
Unit
]
=
Future
{
cacheAdapter
.
put
(
keyBuilder
.
serialize
(
key
),
value
)
}.
map
(
_
=>
())
override
def
putAll
(
map
:
Map
[
K
,
Out0
])(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
Future
[
Unit
]
=
override
def
putAll
(
map
:
Map
[
K
,
Out0
])(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
Future
[
Unit
]
=
Future
{
cacheAdapter
.
batchPut
(
map
.
map
(
kv
=>
keyBuilder
.
serialize
(
kv
.
_1
)
->
kv
.
_2
))
}
override
def
getAll
(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
Future
[
Map
[
K
,
Out0
]]
=
override
def
getAll
(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
Future
[
Map
[
K
,
Out0
]]
=
Future
{
cacheAdapter
.
getAllK
eys
cacheAdapter
.
k
eys
.
map
(
key
=>
keyBuilder
.
deserialize
(
key
)
->
cacheAdapter
.
get
(
key
))
.
collect
{
case
(
k
,
out
)
if
out
!=
null
=>
k
->
out
...
...
@@ -123,7 +85,7 @@ object GenericCache {
override
def
clear
()
:
Future
[
Unit
]
=
Future
.
successful
(
cacheAdapter
.
clear
())
override
def
remove
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Builder
[
K
])
:
Future
[
Unit
]
=
Future
{
override
def
remove
(
key
:
K
)(
implicit
keyBuilder
:
CacheKey
Serde
[
K
])
:
Future
[
Unit
]
=
Future
{
cacheAdapter
.
remove
(
keyBuilder
.
serialize
(
key
))
}
}
...
...
smt-cache/src/main/scala/org/bitlap/cache/package.scala
已删除
100644 → 0
浏览文件 @
79189947
/*
* Copyright (c) 2022 bitlap
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package
org.bitlap
/** @author
* 梦境迷离
* @version 1.0,6/10/22
*/
package
object
cache
{
type
Identity
[
O
]
=
O
}
smt-cache/src/test/scala/org/bitlap/cache/CacheCustomCaseSpec.scala
浏览文件 @
c1d76a8c
...
...
@@ -39,36 +39,15 @@ class CacheCustomCaseSpec extends AnyFlatSpec with Matchers {
"etc2"
->
TestEntity
(
"eth2"
,
"hello2"
,
"world2"
)
)
"cache1"
should
"ok while uses lru cache"
in
{
implicit
val
lruCache
=
CacheImplicits
.
testEntitySyncLruCache
val
cache
=
Cache
.
getSyncCache
[
String
,
TestEntity
]
cache
.
batchPutT
(
data
)
cache
.
putT
(
"etc3"
,
TestEntity
(
"eth3"
,
"hello3"
,
"world2"
))
val
result
:
Option
[
TestEntity
]
=
cache
.
getT
(
"btc"
)
result
shouldBe
None
cache
.
clear
()
val
result2
:
Option
[
TestEntity
]
=
cache
.
getT
(
"etc1"
)
result2
shouldBe
None
cache
.
batchPutT
(
data
)
val
result3
:
Option
[
TestEntity
]
=
cache
.
getT
(
"etc1"
)
result3
shouldBe
Some
(
TestEntity
(
"eth1"
,
"hello1"
,
"world2"
))
val
result4
=
cache
.
getAllT
result4
shouldBe
data
}
"cache2"
should
"ok while defines a custom cache"
in
{
import
scala.concurrent.Await
import
scala.concurrent.duration.Duration
implicit
val
ec
=
scala
.
concurrent
.
ExecutionContext
.
Implicits
.
global
implicit
val
customCache
=
GenericCache
[
String
,
TestEntity
](
CacheStrategy
.
CustomCacheStrategy
(
new
CacheAdapter
[
TestEntity
]
{
lazy
val
underlyingCache
:
util.HashMap
[
String
,
TestEntity
]
=
new
util
.
HashMap
[
String
,
TestEntity
]()
override
def
getAllK
eys
:
Set
[
String
]
=
underlyingCache
.
keySet
().
asScala
.
toSet
override
def
k
eys
:
Set
[
String
]
=
underlyingCache
.
keySet
().
asScala
.
toSet
override
def
batchPut
(
data
:
Map
[
String
,
TestEntity
])
:
Unit
=
underlyingCache
.
putAll
(
data
.
asJava
)
...
...
@@ -80,12 +59,12 @@ class CacheCustomCaseSpec extends AnyFlatSpec with Matchers {
override
def
remove
(
k
:
String
)
:
Unit
=
underlyingCache
.
remove
(
k
)
}))
val
cache
=
Cache
.
getSyncCache
[
String
,
TestEntity
]
cache
.
batchPutT
(
data
)
val
result
:
Option
[
TestEntity
]
=
cache
.
getT
(
"btc"
)
val
cache
=
Cache
[
String
,
TestEntity
]
Await
.
result
(
cache
.
batchPutF
(
data
),
Duration
.
Inf
)
val
result
:
Option
[
TestEntity
]
=
Await
.
result
(
cache
.
getF
(
"btc"
),
Duration
.
Inf
)
result
shouldBe
Some
(
TestEntity
(
"btc"
,
"hello1"
,
"world1"
))
val
result2
=
cache
.
getAllT
val
result2
=
Await
.
result
(
cache
.
getAllF
,
Duration
.
Inf
)
result2
shouldBe
data
}
}
smt-cache/src/test/scala/org/bitlap/cache/CacheImplicits.scala
已删除
100644 → 0
浏览文件 @
79189947
/*
* Copyright (c) 2022 bitlap
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package
org.bitlap.cache
import
scala.concurrent.ExecutionContext
import
org.bitlap.common.TestEntity
object
CacheImplicits
{
// NOTE:These are two completely different caches and the data is not shared
// If you don't use global, you should also make sure this place the same as getAsyncCache's
implicit
lazy
val
testEntitySyncCache
=
GenericCache
[
String
,
TestEntity
](
CacheStrategy
.
Normal
)
implicit
lazy
val
testEntityAsyncCache
=
GenericCache
[
String
,
TestEntity
](
CacheStrategy
.
Normal
,
ExecutionContext
.
Implicits
.
global
)
implicit
lazy
val
testEntitySyncLruCache
=
GenericCache
[
String
,
TestEntity
](
CacheStrategy
.
Lru
(
3
))
}
smt-cache/src/test/scala/org/bitlap/cache/CacheKeyBuilderSpec.scala
浏览文件 @
c1d76a8c
...
...
@@ -34,11 +34,11 @@ class CacheKeyBuilderSpec extends AnyFlatSpec with Matchers {
"CacheKeyBuilder1"
should
"ok while uses uuid type"
in
{
val
now
=
UUID
.
randomUUID
()
val
str
=
CacheKey
Builder
.
uuidKey
.
serialize
(
now
)
val
str
=
CacheKey
Serde
.
uuidKey
.
serialize
(
now
)
println
(
str
)
val
v
=
CacheKey
Builder
.
uuidKey
.
deserialize
(
str
)
val
v
=
CacheKey
Serde
.
uuidKey
.
deserialize
(
str
)
v
shouldBe
now
}
...
...
smt-cache/src/test/scala/org/bitlap/cache/CacheSpec.scala
浏览文件 @
c1d76a8c
...
...
@@ -27,6 +27,7 @@ import scala.concurrent.Await
import
scala.concurrent.ExecutionContext.Implicits.global
import
scala.concurrent.duration.DurationInt
import
org.bitlap.common.TestEntity
import
scala.concurrent.ExecutionContext
/** @author
* 梦境迷离
...
...
@@ -34,38 +35,8 @@ import org.bitlap.common.TestEntity
*/
class
CacheSpec
extends
AnyFlatSpec
with
Matchers
{
private
implicit
val
syncCache
=
CacheImplicits
.
testEntitySyncCache
private
implicit
val
asyncCache
=
CacheImplicits
.
testEntityAsyncCache
private
val
data
=
Map
(
"btc"
->
TestEntity
(
"btc"
,
"hello1"
,
"world1"
),
"etc"
->
TestEntity
(
"eth"
,
"hello2"
,
"world2"
)
)
"cache1"
should
"get entity from cache successfully"
in
{
val
cache
=
Cache
.
getSyncCache
[
String
,
TestEntity
]
cache
.
batchPutT
(
data
)
val
result
:
Option
[
TestEntity
]
=
cache
.
getT
(
"etc"
)
result
shouldBe
data
.
get
(
"etc"
)
val
result2
=
cache
.
getAllT
result2
shouldBe
data
}
"cache3"
should
"get entity's field after refresh"
in
{
val
cache
=
Cache
.
getSyncCache
[
String
,
TestEntity
]
cache
.
batchPutT
(
data
)
val
newData
=
Map
(
"btc"
->
TestEntity
(
"btc"
,
"hello1"
,
"world1"
),
"btc-zh-cn"
->
TestEntity
(
"btc"
,
"你好啊"
,
"你好哦"
),
"etc"
->
TestEntity
(
"eth"
,
"hello2"
,
"world2"
)
)
cache
.
clear
()
cache
.
batchPutT
(
newData
)
val
result
:
Option
[
TestEntity
]
=
cache
.
getT
(
"btc-zh-cn"
)
result
shouldBe
newData
.
get
(
"btc-zh-cn"
)
}
implicit
lazy
val
testEntityAsyncCache
=
GenericCache
[
String
,
TestEntity
](
CacheStrategy
.
Normal
)(
ExecutionContext
.
Implicits
.
global
)
"cache4"
should
"async cache"
in
{
val
newData
=
Map
(
...
...
@@ -77,13 +48,13 @@ class CacheSpec extends AnyFlatSpec with Matchers {
"btc-zh-cn"
->
TestEntity
(
"btc"
,
"id456"
,
"btc_zh_key456"
),
"eth"
->
TestEntity
(
"btc"
,
"id456"
,
"eth_key456"
)
)
val
cache
=
Cache
.
getAsyncCache
[
String
,
TestEntity
]
val
cache
=
Cache
[
String
,
TestEntity
]
val
ret
=
for
{
_
<-
cache
.
batchPut
T
(
newData
)
btcKey
<-
cache
.
get
T
(
"btc"
).
map
(
_
.
map
(
_
.
key
))
_
<-
cache
.
batchPut
T
(
newData2
)
ethKey
<-
cache
.
get
T
(
"eth"
).
map
(
_
.
map
(
_
.
key
))
_
<-
cache
.
batchPut
F
(
newData
)
btcKey
<-
cache
.
get
F
(
"btc"
).
map
(
_
.
map
(
_
.
key
))
_
<-
cache
.
batchPut
F
(
newData2
)
ethKey
<-
cache
.
get
F
(
"eth"
).
map
(
_
.
map
(
_
.
key
))
}
yield
btcKey
->
ethKey
Await
.
result
(
ret
,
3.
seconds
)
shouldBe
Option
(
"btc_key123"
)
->
Option
(
"eth_key456"
)
...
...
@@ -95,34 +66,17 @@ class CacheSpec extends AnyFlatSpec with Matchers {
"btc"
->
TestEntity
(
"btc"
,
"id123"
,
"btc_key123"
),
"btc-zh-cn"
->
TestEntity
(
"btc"
,
"id123"
,
"btc_zh_key123"
)
)
val
cache
=
Cache
.
getAsyncCache
[
String
,
TestEntity
]
val
cache
=
Cache
[
String
,
TestEntity
]
val
ret
=
for
{
_
<-
cache
.
batchPut
T
(
newData
)
btcKey
<-
cache
.
get
T
(
"btc"
).
map
(
_
.
map
(
_
.
key
))
_
<-
cache
.
clear
()
ethKey
<-
cache
.
get
T
(
"eth"
).
map
(
_
.
map
(
_
.
key
))
_
<-
cache
.
batchPut
F
(
newData
)
btcKey
<-
cache
.
get
F
(
"btc"
).
map
(
_
.
map
(
_
.
key
))
_
<-
cache
.
clear
F
()
ethKey
<-
cache
.
get
F
(
"eth"
).
map
(
_
.
map
(
_
.
key
))
}
yield
btcKey
->
ethKey
Await
.
result
(
ret
,
3.
seconds
)
shouldBe
Option
(
"btc_key123"
)
->
None
}
"cache7"
should
"refresh successfully"
in
{
val
cache
=
Cache
.
getSyncCache
[
String
,
TestEntity
]
cache
.
batchPutT
(
data
)
val
result
:
Option
[
TestEntity
]
=
cache
.
getT
(
"etc"
)
result
shouldBe
data
.
get
(
"etc"
)
val
newData
=
Map
(
"btc"
->
TestEntity
(
"btc"
,
"id123"
,
"btc_key123"
),
"btc-zh-cn"
->
TestEntity
(
"btc"
,
"id123"
,
"btc_zh_key123"
)
)
cache
.
safeRefreshT
(
newData
)
val
result2
=
cache
.
getT
(
"eth"
)
result2
shouldBe
None
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录