package org.ssssssss.magicapi.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.ssssssss.magicapi.core.annotation.MagicModule;

/**
 * redis 多数据源插件模块
 * @since 3.0.0
 * @author 冰点
 */
@MagicModule("dynamicRedis")
@Slf4j
public class RedisDynamicModule  {

    private final RedisInstanceFactory redisInstanceFactory;


    public RedisDynamicModule(RedisInstanceFactory redisInstanceFactory) {
        this.redisInstanceFactory = redisInstanceFactory;
    }

    /**
     * 序列化
     */
    public RedisTemplate<?, ?> of(String name) {
        RedisTemplate<?, ?> redisTemplate = redisInstanceFactory.getRedisTemplate(name, true);
        if (redisTemplate!=null){
            log.info("使用redis {}", name);
        }else {
            log.error("当前没有配置redis {}", name);
        }
        return redisTemplate;
    }

    public RedisTemplate<?, ?> name(String name) {
        return of(name);
    }


}
