Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
ssssssss-team
magic-api
提交
d59411b3
M
magic-api
项目概览
ssssssss-team
/
magic-api
9 个月 前同步成功
通知
41
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
magic-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d59411b3
编写于
3月 20, 2023
作者:
J
jmxd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修复redis模块不支持redisson部分命令的问题
上级
d13f4e6e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
62 addition
and
13 deletion
+62
-13
magic-api-plugins/magic-api-plugin-redis/pom.xml
magic-api-plugins/magic-api-plugin-redis/pom.xml
+0
-5
magic-api-plugins/magic-api-plugin-redis/src/main/java/org/ssssssss/magicapi/redis/RedisModule.java
...rc/main/java/org/ssssssss/magicapi/redis/RedisModule.java
+62
-8
未找到文件。
magic-api-plugins/magic-api-plugin-redis/pom.xml
浏览文件 @
d59411b3
...
...
@@ -17,10 +17,5 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
org.redisson
</groupId>
<artifactId>
redisson-spring-data-23
</artifactId>
<version>
3.15.6
</version>
</dependency>
</dependencies>
</project>
magic-api-plugins/magic-api-plugin-redis/src/main/java/org/ssssssss/magicapi/redis/RedisModule.java
浏览文件 @
d59411b3
package
org.ssssssss.magicapi.redis
;
import
org.springframework.dao.InvalidDataAccessApiUsageException
;
import
org.springframework.data.redis.connection.DefaultStringRedisConnection
;
import
org.springframework.data.redis.connection.RedisConnection
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.co
re.RedisCallback
;
import
org.springframework.data.redis.co
nnection.RedisPipelineException
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.util.ReflectionUtils
;
import
org.ssssssss.magicapi.core.annotation.MagicModule
;
import
org.ssssssss.script.functions.DynamicMethod
;
import
org.ssssssss.script.reflection.JavaReflection
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
java.util.*
;
/**
...
...
@@ -18,8 +25,11 @@ public class RedisModule implements DynamicMethod {
private
final
StringRedisTemplate
redisTemplate
;
private
final
boolean
isRedisson
;
public
RedisModule
(
RedisConnectionFactory
connectionFactory
)
{
this
.
redisTemplate
=
new
StringRedisTemplate
(
connectionFactory
);
this
.
isRedisson
=
Objects
.
equals
(
"org.redisson.spring.data.connection.RedissonConnectionFactory"
,
this
.
redisTemplate
.
getConnectionFactory
().
getClass
().
getName
());
}
/**
...
...
@@ -32,6 +42,13 @@ public class RedisModule implements DynamicMethod {
return
serializer
(
value
.
toString
());
}
private
Object
serializerForRedisson
(
Object
value
){
if
(
value
==
null
||
JavaReflection
.
isPrimitiveAssignableFrom
(
value
.
getClass
(),
value
.
getClass
())){
return
value
;
}
return
serializer
(
value
.
toString
());
}
/**
* 反序列化
*/
...
...
@@ -49,7 +66,7 @@ public class RedisModule implements DynamicMethod {
}
return
resultList
;
}
if
(
value
instanceof
Map
)
{
if
(
value
instanceof
Map
)
{
Map
<
Object
,
Object
>
map
=
(
Map
<
Object
,
Object
>)
value
;
LinkedHashMap
<
Object
,
Object
>
newMap
=
new
LinkedHashMap
<>(
map
.
size
());
map
.
forEach
((
key
,
val
)
->
newMap
.
put
(
deserialize
(
key
),
deserialize
(
val
)));
...
...
@@ -67,12 +84,49 @@ public class RedisModule implements DynamicMethod {
*/
@Override
public
Object
execute
(
String
methodName
,
List
<
Object
>
parameters
)
{
return
this
.
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
connection
->
{
byte
[][]
params
=
new
byte
[
parameters
.
size
()][];
for
(
int
i
=
0
;
i
<
params
.
length
;
i
++)
{
params
[
i
]
=
serializer
(
parameters
.
get
(
i
));
return
this
.
redisTemplate
.
execute
(
connection
->
{
Object
result
;
if
(
isRedisson
){
result
=
executeForRedisson
(((
DefaultStringRedisConnection
)
connection
).
getDelegate
(),
methodName
,
parameters
);
}
else
{
byte
[][]
params
=
new
byte
[
parameters
.
size
()][];
for
(
int
i
=
0
;
i
<
params
.
length
;
i
++)
{
params
[
i
]
=
serializer
(
parameters
.
get
(
i
));
}
result
=
connection
.
execute
(
methodName
,
params
);
}
return
deserialize
(
connection
.
execute
(
methodName
,
params
)
);
});
return
deserialize
(
result
);
}
,
isRedisson
||
this
.
redisTemplate
.
isExposeConnection
()
);
}
private
Object
executeForRedisson
(
RedisConnection
connection
,
String
command
,
List
<
Object
>
parameters
)
{
Method
[]
methods
=
connection
.
getClass
().
getDeclaredMethods
();
for
(
Method
method
:
methods
)
{
if
(
method
.
getName
().
equalsIgnoreCase
(
command
)
&&
Modifier
.
isPublic
(
method
.
getModifiers
())
&&
method
.
getParameterTypes
().
length
==
parameters
.
size
())
{
try
{
Object
ret
=
this
.
execute
(
connection
,
method
,
parameters
);
if
(
ret
instanceof
String
)
{
return
((
String
)
ret
).
getBytes
();
}
return
ret
;
}
catch
(
IllegalArgumentException
e
)
{
if
(
connection
.
isPipelined
())
{
throw
new
RedisPipelineException
(
e
);
}
throw
new
InvalidDataAccessApiUsageException
(
e
.
getMessage
(),
e
);
}
}
}
throw
new
UnsupportedOperationException
();
}
private
Object
execute
(
RedisConnection
connection
,
Method
method
,
List
<
Object
>
parameters
){
if
(
method
.
getParameterTypes
().
length
>
0
&&
method
.
getParameterTypes
()[
0
]
==
byte
[][].
class
)
{
return
ReflectionUtils
.
invokeMethod
(
method
,
connection
,
parameters
.
stream
().
map
(
this
::
serializer
).
toArray
(
byte
[][]::
new
));
}
else
if
(
parameters
.
size
()
==
0
){
return
ReflectionUtils
.
invokeMethod
(
method
,
connection
);
}
return
ReflectionUtils
.
invokeMethod
(
method
,
connection
,
parameters
.
stream
().
map
(
this
::
serializerForRedisson
).
toArray
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录