All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ssssssss.magicapi.modules.RedisModule Maven / Gradle / Ivy

package org.ssssssss.magicapi.modules;

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.config.MagicModule;
import org.ssssssss.script.functions.DynamicMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * redis模块
 */
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 valueList = (List) value;
				List 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 parameters) {
		return this.redisTemplate.execute((RedisCallback) 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));
		});
	}
}