package com.elab.cache.redis;

import com.elab.cache.ICacheClient;
import com.sohu.tv.builder.RedisSentinelBuilder;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

/**
 * 操作主备类型的redis
 *
 * @author Liukx
 * @create 2017-05-17 14:06
 * @email liukx@elab-plus.com
 **/
public class RedisSentinelClient extends BaseRedisOperation implements ICacheClient {

    public RedisSentinelBuilder redisBuilder;

    public JedisSentinelPool jedisSentinelPool;

    private Jedis jedisCluster;

    public void init() {
        this.jedisSentinelPool = redisBuilder.build();
    }

    public RedisSentinelBuilder getRedisBuilder() {
        return redisBuilder;
    }

    public void setRedisBuilder(RedisSentinelBuilder redisBuilder) {
        this.redisBuilder = redisBuilder;
    }

    public Jedis getJedisCluster() {
        return jedisCluster;
    }

    public void setJedisCluster(Jedis jedisCluster) {
        this.jedisCluster = jedisCluster;
    }

    @Override
    public Jedis getResource() {
//        HostAndPort currentHostMaster = jedisSentinelPool.getCurrentHostMaster();
//        logger.debug("当前选举的IP : " + currentHostMaster);
        return this.jedisSentinelPool.getResource();
    }


    @Override
    public ICacheClient getCacheClient() {
        return this;
    }
}
