Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
3c07a4aa
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 搜索 >>
提交
3c07a4aa
编写于
11月 03, 2021
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
优化 mica-redis。
上级
1b8c0949
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
128 addition
and
35 deletion
+128
-35
mica-redis/src/main/java/net/dreamlu/mica/redis/cache/MicaRedisCache.java
...ain/java/net/dreamlu/mica/redis/cache/MicaRedisCache.java
+128
-35
未找到文件。
mica-redis/src/main/java/net/dreamlu/mica/redis/cache/MicaRedisCache.java
浏览文件 @
3c07a4aa
...
...
@@ -18,10 +18,12 @@ package net.dreamlu.mica.redis.cache;
import
lombok.Getter
;
import
net.dreamlu.mica.core.utils.CollectionUtil
;
import
net.dreamlu.mica.core.utils.JsonUtil
;
import
org.springframework.data.redis.core.*
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.lang.Nullable
;
import
java.nio.charset.StandardCharsets
;
import
java.time.Duration
;
import
java.util.*
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -113,6 +115,30 @@ public class MicaRedisCache {
return
(
T
)
valueOps
.
get
(
key
);
}
/**
* 返回 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
);
});
}
/**
* 返回 key 所关联的 value 值,采用 json 序列化
* 如果 key 不存在那么返回特殊值 nil 。
*/
@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
);
});
}
/**
* 获取cache 为 null 时使用加载器,然后设置缓存
*
...
...
@@ -135,6 +161,29 @@ public class MicaRedisCache {
return
value
;
}
/**
* 获取cache 为 null 时使用加载器,然后设置缓存
*
* @param key cacheKey
* @param clazz Class
* @param loader cache loader
* @param <T> 泛型
* @return 结果
*/
@Nullable
public
<
T
>
T
getByJsonSer
(
String
key
,
Class
<
T
>
clazz
,
Supplier
<
T
>
loader
)
{
T
value
=
this
.
getByJsonSer
(
key
,
clazz
);
if
(
value
!=
null
)
{
return
value
;
}
value
=
loader
.
get
();
if
(
value
==
null
)
{
return
null
;
}
this
.
set
(
key
,
value
);
return
value
;
}
/**
* 返回 key 所关联的 value 值
* 如果 key 不存在那么返回特殊值 nil 。
...
...
@@ -144,6 +193,15 @@ public class MicaRedisCache {
return
(
T
)
valueOps
.
get
(
cacheKey
.
getKey
());
}
/**
* 返回 key 所关联的 value 值
* 如果 key 不存在那么返回特殊值 nil 。
*/
@Nullable
public
<
T
>
T
getByJsonSer
(
CacheKey
cacheKey
,
Class
<
T
>
clazz
)
{
return
getByJsonSer
(
cacheKey
.
getKey
(),
clazz
);
}
/**
* 获取cache 为 null 时使用加载器,然后设置缓存
*
...
...
@@ -167,6 +225,29 @@ public class MicaRedisCache {
return
value
;
}
/**
* 获取cache 为 null 时使用加载器,然后设置缓存
*
* @param cacheKey cacheKey
* @param loader cache loader
* @param <T> 泛型
* @return 结果
*/
@Nullable
public
<
T
>
T
getByJsonSer
(
CacheKey
cacheKey
,
Class
<
T
>
clazz
,
Supplier
<
T
>
loader
)
{
String
key
=
cacheKey
.
getKey
();
T
value
=
this
.
getByJsonSer
(
key
,
clazz
);
if
(
value
!=
null
)
{
return
value
;
}
value
=
loader
.
get
();
if
(
value
==
null
)
{
return
null
;
}
this
.
set
(
cacheKey
,
value
);
return
value
;
}
/**
* 删除给定的一个 key
* 不存在的 key 会被忽略。
...
...
@@ -189,6 +270,7 @@ public class MicaRedisCache {
* 删除给定的多个 key
* 不存在的 key 会被忽略。
*/
@Nullable
public
Long
del
(
String
...
keys
)
{
return
del
(
Arrays
.
asList
(
keys
));
}
...
...
@@ -243,7 +325,6 @@ public class MicaRedisCache {
*
* @param pattern 匹配表达式
* @param consumer 消费者
* @return 扫描结果
*/
public
void
scan
(
String
pattern
,
Consumer
<
String
>
consumer
)
{
scan
(
pattern
,
100L
,
consumer
);
...
...
@@ -255,11 +336,9 @@ public class MicaRedisCache {
* @param pattern 匹配表达式
* @param count 一次扫描的数量
* @param consumer 消费者
* @return 扫描结果
*/
public
void
scan
(
String
pattern
,
@Nullable
Long
count
,
Consumer
<
String
>
consumer
)
{
RedisSerializer
<
String
>
keySerializer
=
(
RedisSerializer
<
String
>)
redisTemplate
.
getKeySerializer
();
scanBytes
(
pattern
,
count
,
(
bytes
)
->
consumer
.
accept
(
keySerializer
.
deserialize
(
bytes
)));
scanBytes
(
pattern
,
count
,
(
bytes
)
->
consumer
.
accept
(
keyDeserialize
(
bytes
)));
}
/**
...
...
@@ -268,16 +347,15 @@ public class MicaRedisCache {
* @param pattern 匹配表达式
* @param count 一次扫描的数量
* @param consumer 消费者
* @return 扫描结果
*/
public
void
scanBytes
(
String
pattern
,
@Nullable
Long
count
,
Consumer
<
byte
[]>
consumer
)
{
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
action
->
{
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
redis
->
{
ScanOptions
.
ScanOptionsBuilder
builder
=
ScanOptions
.
scanOptions
()
.
match
(
pattern
);
if
(
count
!=
null
)
{
builder
.
count
(
count
);
}
try
(
Cursor
<
byte
[]>
cursor
=
action
.
scan
(
builder
.
build
()))
{
try
(
Cursor
<
byte
[]>
cursor
=
redis
.
scan
(
builder
.
build
()))
{
cursor
.
forEachRemaining
(
consumer
);
}
return
null
;
...
...
@@ -331,8 +409,7 @@ public class MicaRedisCache {
* @return 扫描结果
*/
public
void
sScan
(
String
key
,
String
pattern
,
@Nullable
Long
count
,
Consumer
<
String
>
consumer
)
{
RedisSerializer
<
String
>
keySerializer
=
(
RedisSerializer
<
String
>)
redisTemplate
.
getKeySerializer
();
sScanBytes
(
key
,
pattern
,
count
,
(
bytes
)
->
consumer
.
accept
(
keySerializer
.
deserialize
(
bytes
)));
sScanBytes
(
key
,
pattern
,
count
,
(
bytes
)
->
consumer
.
accept
(
keyDeserialize
(
bytes
)));
}
/**
...
...
@@ -345,14 +422,13 @@ public class MicaRedisCache {
* @return 扫描结果
*/
public
void
sScanBytes
(
String
key
,
String
pattern
,
@Nullable
Long
count
,
Consumer
<
byte
[]>
consumer
)
{
RedisSerializer
<
String
>
keySerializer
=
(
RedisSerializer
<
String
>)
redisTemplate
.
getKeySerializer
();
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
action
->
{
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
redis
->
{
ScanOptions
.
ScanOptionsBuilder
builder
=
ScanOptions
.
scanOptions
()
.
match
(
pattern
);
if
(
count
!=
null
)
{
builder
.
count
(
count
);
}
try
(
Cursor
<
byte
[]>
cursor
=
action
.
sScan
(
keySerializer
.
s
erialize
(
key
),
builder
.
build
()))
{
try
(
Cursor
<
byte
[]>
cursor
=
redis
.
sScan
(
keyS
erialize
(
key
),
builder
.
build
()))
{
cursor
.
forEachRemaining
(
consumer
);
}
return
null
;
...
...
@@ -424,11 +500,10 @@ public class MicaRedisCache {
* 关于更多递增(increment) / 递减(decrement)操作的更多信息,请参见 INCR 命令。
*/
public
Long
decrBy
(
String
key
,
long
longValue
,
long
seconds
)
{
RedisSerializer
<
String
>
keySerializer
=
(
RedisSerializer
<
String
>)
redisTemplate
.
getKeySerializer
();
byte
[]
serializedKey
=
keySerializer
.
serialize
(
key
);
List
<
Object
>
result
=
redisTemplate
.
executePipelined
((
RedisCallback
<
Long
>)
action
->
{
Long
data
=
action
.
decrBy
(
serializedKey
,
longValue
);
action
.
expire
(
serializedKey
,
seconds
);
byte
[]
serializedKey
=
keySerialize
(
key
);
List
<
Object
>
result
=
redisTemplate
.
executePipelined
((
RedisCallback
<
Long
>)
redis
->
{
Long
data
=
redis
.
decrBy
(
serializedKey
,
longValue
);
redis
.
expire
(
serializedKey
,
seconds
);
return
data
;
});
return
(
Long
)
result
.
get
(
0
);
...
...
@@ -465,11 +540,10 @@ public class MicaRedisCache {
* 关于递增(increment) / 递减(decrement)操作的更多信息,参见 INCR 命令。
*/
public
Long
incrBy
(
String
key
,
long
longValue
,
long
seconds
)
{
RedisSerializer
<
String
>
keySerializer
=
(
RedisSerializer
<
String
>)
redisTemplate
.
getKeySerializer
();
byte
[]
serializedKey
=
keySerializer
.
serialize
(
key
);
List
<
Object
>
result
=
redisTemplate
.
executePipelined
((
RedisCallback
<
Long
>)
action
->
{
Long
data
=
action
.
incrBy
(
serializedKey
,
longValue
);
action
.
expire
(
serializedKey
,
seconds
);
byte
[]
serializedKey
=
keySerialize
(
key
);
List
<
Object
>
result
=
redisTemplate
.
executePipelined
((
RedisCallback
<
Long
>)
redis
->
{
Long
data
=
redis
.
incrBy
(
serializedKey
,
longValue
);
redis
.
expire
(
serializedKey
,
seconds
);
return
data
;
});
return
(
Long
)
result
.
get
(
0
);
...
...
@@ -482,10 +556,12 @@ public class MicaRedisCache {
*/
@Nullable
public
Long
getCounter
(
String
key
)
{
RedisSerializer
<
String
>
keySerializer
=
(
RedisSerializer
<
String
>)
redisTemplate
.
getKeySerializer
();
return
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
action
->
{
byte
[]
value
=
action
.
get
(
keySerializer
.
serialize
(
key
));
return
Long
.
valueOf
(
new
String
(
value
));
return
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
redis
->
{
byte
[]
value
=
redis
.
get
(
keySerialize
(
key
));
if
(
value
==
null
)
{
return
null
;
}
return
Long
.
valueOf
(
new
String
(
value
,
StandardCharsets
.
UTF_8
));
});
}
...
...
@@ -498,17 +574,16 @@ public class MicaRedisCache {
*/
@Nullable
public
Long
getCounter
(
String
key
,
long
seconds
,
Supplier
<
Long
>
loader
)
{
RedisSerializer
<
String
>
keySerializer
=
(
RedisSerializer
<
String
>)
redisTemplate
.
getKeySerializer
();
return
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
action
->
{
byte
[]
keyBytes
=
keySerializer
.
serialize
(
key
);
byte
[]
value
=
action
.
get
(
keyBytes
);
return
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
redis
->
{
byte
[]
keyBytes
=
keySerialize
(
key
);
byte
[]
value
=
redis
.
get
(
keyBytes
);
long
longValue
;
if
(
value
!=
null
)
{
longValue
=
Long
.
valueOf
(
new
String
(
value
));
longValue
=
Long
.
parseLong
(
new
String
(
value
,
StandardCharsets
.
UTF_8
));
}
else
{
Long
loaderValue
=
loader
.
get
();
longValue
=
loaderValue
==
null
?
0
:
loaderValue
;
action
.
setEx
(
keyBytes
,
seconds
,
String
.
valueOf
(
longValue
).
getBytes
());
redis
.
setEx
(
keyBytes
,
seconds
,
String
.
valueOf
(
longValue
).
getBytes
());
}
return
longValue
;
});
...
...
@@ -1059,9 +1134,7 @@ public class MicaRedisCache {
@Nullable
public
Long
zAdd
(
String
key
,
Map
<
Object
,
Double
>
scoreMembers
)
{
Set
<
ZSetOperations
.
TypedTuple
<
Object
>>
tuples
=
new
HashSet
<>();
scoreMembers
.
forEach
((
k
,
v
)
->
{
tuples
.
add
(
new
DefaultTypedTuple
<>(
k
,
v
));
});
scoreMembers
.
forEach
((
k
,
v
)
->
tuples
.
add
(
new
DefaultTypedTuple
<>(
k
,
v
)));
return
zSetOps
.
add
(
key
,
tuples
);
}
...
...
@@ -1159,4 +1232,24 @@ public class MicaRedisCache {
return
zSetOps
.
score
(
key
,
member
);
}
/**
* redisKey 序列化
*
* @param redisKey redisKey
* @return byte array
*/
public
static
byte
[]
keySerialize
(
String
redisKey
)
{
return
Objects
.
requireNonNull
(
RedisSerializer
.
string
().
serialize
(
redisKey
),
"Redis key is null."
);
}
/**
* redisKey 序列化
*
* @param redisKey redisKey
* @return byte array
*/
public
static
String
keyDeserialize
(
byte
[]
redisKey
)
{
return
Objects
.
requireNonNull
(
RedisSerializer
.
string
().
deserialize
(
redisKey
),
"Redis key is null."
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录