001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.support.redis;
017
018
019import io.jboot.Jboot;
020import io.jboot.utils.ConfigUtil;
021import io.jboot.exception.JbootException;
022import io.jboot.exception.JbootIllegalConfigException;
023import io.jboot.support.redis.jedis.JbootJedisClusterImpl;
024import io.jboot.support.redis.jedis.JbootJedisImpl;
025
026import java.util.Map;
027import java.util.concurrent.ConcurrentHashMap;
028
029/**
030 * 参考: com.jfinal.plugin.redis
031 * JbootRedis 命令文档: http://redisdoc.com/
032 */
033public class JbootRedisManager {
034
035    private static JbootRedisManager manager = new JbootRedisManager();
036
037    private JbootRedisManager() {
038    }
039
040    public static JbootRedisManager me() {
041        return manager;
042    }
043
044    private JbootRedis redis;
045    private Map<String, JbootRedis> jbootRedisMap = new ConcurrentHashMap<>();
046
047    public JbootRedis getRedis() {
048        if (redis == null) {
049            JbootRedisConfig config = Jboot.config(JbootRedisConfig.class);
050            redis = getRedis(config);
051        }
052
053        return redis;
054    }
055
056    public JbootRedis getRedis(String name) {
057        JbootRedis redis = jbootRedisMap.get(name);
058        if (redis == null) {
059            synchronized (this) {
060                redis = jbootRedisMap.get(name);
061                if (redis == null) {
062                    Map<String, JbootRedisConfig> configModels = ConfigUtil.getConfigModels(JbootRedisConfig.class);
063                    if (!configModels.containsKey(name)) {
064                        throw new JbootIllegalConfigException("Please config \"jboot.redis." + name + ".host\" in your jboot.properties.");
065                    }
066                    JbootRedisConfig jbootRedisConfig = configModels.get(name);
067                    redis = getRedis(jbootRedisConfig);
068                    if (redis != null) {
069                        jbootRedisMap.put(name, redis);
070                    }
071                }
072            }
073        }
074        return redis;
075    }
076
077
078    public JbootRedis getRedis(JbootRedisConfig config) {
079        if (config == null || !config.isConfigOk()) {
080            return null;
081        }
082
083        switch (config.getType()) {
084            case JbootRedisConfig.TYPE_JEDIS:
085                return getJedisClient(config);
086            case JbootRedisConfig.TYPE_LETTUCE:
087                return getLettuceClient(config);
088            case JbootRedisConfig.TYPE_REDISSON:
089                return getRedissonClient(config);
090        }
091        return null;
092    }
093
094
095    private JbootRedis getJedisClient(JbootRedisConfig config) {
096        if (config.isCluster()) {
097            return new JbootJedisClusterImpl(config);
098        } else {
099            return new JbootJedisImpl(config);
100        }
101    }
102
103    private JbootRedis getLettuceClient(JbootRedisConfig config) {
104        throw new JbootException("lettuce is not finished.");
105    }
106
107    private JbootRedis getRedissonClient(JbootRedisConfig config) {
108        throw new JbootException("redisson is not finished.");
109    }
110
111
112}
113
114
115
116
117
118