mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-05-22 18:10:17 +08:00
初步实现插件机制
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package org.ssssssss.magicapi.redis;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
|
||||
import org.ssssssss.magicapi.core.config.MagicPluginConfiguration;
|
||||
import org.ssssssss.magicapi.core.config.Resource;
|
||||
import org.ssssssss.magicapi.core.model.Plugin;
|
||||
import org.ssssssss.magicapi.core.resource.RedisResource;
|
||||
|
||||
@Configuration
|
||||
public class MagicRedisConfiguration implements MagicPluginConfiguration {
|
||||
|
||||
private final MagicAPIProperties properties;
|
||||
|
||||
public MagicRedisConfiguration(MagicAPIProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用Redis存储
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(prefix = "magic-api", name = "resource.type", havingValue = "redis")
|
||||
public org.ssssssss.magicapi.core.resource.Resource magicRedisResource(RedisConnectionFactory connectionFactory) {
|
||||
Resource resource = properties.getResource();
|
||||
return new RedisResource(new StringRedisTemplate(connectionFactory), resource.getPrefix(), resource.isReadonly());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入redis模块
|
||||
*/
|
||||
@Bean
|
||||
public RedisModule redisFunctions(RedisConnectionFactory connectionFactory) {
|
||||
return new RedisModule(connectionFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin plugin() {
|
||||
return new Plugin("Redis");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.ssssssss.magicapi.redis;
|
||||
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.ssssssss.magicapi.core.config.MagicModule;
|
||||
import org.ssssssss.script.functions.DynamicMethod;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* redis模块
|
||||
*
|
||||
* @author mxd
|
||||
*/
|
||||
public class RedisModule implements MagicModule, DynamicMethod {
|
||||
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
|
||||
public RedisModule(RedisConnectionFactory connectionFactory) {
|
||||
this.redisTemplate = new StringRedisTemplate(connectionFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getModuleName() {
|
||||
return "redis";
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化
|
||||
*/
|
||||
private byte[] serializer(Object value) {
|
||||
if (value == null || value instanceof String) {
|
||||
return redisTemplate.getStringSerializer().serialize((String) value);
|
||||
}
|
||||
return serializer(value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化
|
||||
*/
|
||||
private Object deserialize(Object value) {
|
||||
if (value != null) {
|
||||
if (value instanceof byte[]) {
|
||||
return this.redisTemplate.getStringSerializer().deserialize((byte[]) value);
|
||||
}
|
||||
if (value instanceof List) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Object> valueList = (List<Object>) value;
|
||||
List<Object> resultList = new ArrayList<>(valueList.size());
|
||||
for (Object val : valueList) {
|
||||
resultList.add(deserialize(val));
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行命令
|
||||
*
|
||||
* @param methodName 命令名称
|
||||
* @param parameters 命令参数
|
||||
*/
|
||||
@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 deserialize(connection.execute(methodName, params));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.ssssssss.magicapi.redis.MagicRedisConfiguration
|
||||
Reference in New Issue
Block a user