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

com.zc.smartcity.redis.service.impl.RedisClusterImpl Maven / Gradle / Ivy

The newest version!
package com.zc.smartcity.redis.service.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.zc.smartcity.redis.common.Command;
import com.zc.smartcity.redis.enums.ExistEnum;
import com.zc.smartcity.redis.enums.ExpireTimeEnum;
import com.zc.smartcity.redis.service.IRedis;
import com.zc.smartcity.redis.core.IRedisClusterConnection;
import com.zc.smartcity.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.reflect.FastClass;
import org.springframework.cglib.reflect.FastMethod;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.params.SetParams;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
public class RedisClusterImpl implements IRedis {

    @Autowired
    private IRedisClusterConnection redisClusterConnectionFactory;


    @Override
    public String hget(String business, String key, String field) {
        return getJedisCluster().hget(RedisUtil.buildKey(getBusiness(business), key), field);
    }

    @Override
    public Map hgetAll(String business, String key) {
        return getJedisCluster().hgetAll(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long expire(String business, String key, int seconds) {
        return getJedisCluster().expire(RedisUtil.buildKey(getBusiness(business), key), seconds);
    }

    @Override
    public Long expireAt(String business, String key, long unixTime) {
        return getJedisCluster().expireAt(RedisUtil.buildKey(getBusiness(business), key), unixTime);
    }

    @Override
    public Long ttl(String business, String key) {
        return getJedisCluster().ttl(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long decrBy(String business, String key, long integer) {
        return getJedisCluster().decrBy(RedisUtil.buildKey(getBusiness(business), key), integer);
    }

    @Override
    public Long decr(String business, String key) {
        return getJedisCluster().decr(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long incrBy(String business, String key, long integer) {
        return getJedisCluster().incrBy(RedisUtil.buildKey(getBusiness(business), key), integer);
    }

    @Override
    public Long incr(String business, String key) {
        return getJedisCluster().incr(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long hset(String business, String key, String field, String value) {
        return getJedisCluster().hset(RedisUtil.buildKey(getBusiness(business), key), field, value);
    }

    @Override
    public String set(String business, String key, String value) {
        return getJedisCluster().set(RedisUtil.buildKey(getBusiness(business), key), value);
    }

    @Override
    public String set(String business, byte[] key, byte[] value) {
        return getJedisCluster().set(RedisUtil.buildKey(getBusiness(business), key), value);
    }

    @Override
    public String get(String business, String key) {
        return getJedisCluster().get(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public byte[] get(String business, byte[] key) {
        return getJedisCluster().get(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Boolean exists(String business, String key) {
        return getJedisCluster().exists(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public String type(String business, String key) {
        return getJedisCluster().type(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long append(String business, String key, String value) {
        return getJedisCluster().append(RedisUtil.buildKey(getBusiness(business), key), value);
    }

    @Override
    public String substr(String business, String key, int start, int end) {
        return getJedisCluster().substr(RedisUtil.buildKey(getBusiness(business), key), start, end);
    }

    @Override
    public Long hsetnx(String business, String key, String field, String value) {
        return getJedisCluster().hsetnx(RedisUtil.buildKey(getBusiness(business), key), field, value);
    }

    @Override
    public String hmset(String business, String key, Map hash) {
        return getJedisCluster().hmset(RedisUtil.buildKey(getBusiness(business), key), hash);
    }

    @Override
    public List hmget(String business, String key, String... fields) {
        return getJedisCluster().hmget(RedisUtil.buildKey(getBusiness(business), key), fields);
    }

    @Override
    public Long hincrBy(String business, String key, String field, long value) {
        return getJedisCluster().hincrBy(RedisUtil.buildKey(getBusiness(business), key), field, value);
    }

    @Override
    public Boolean hexists(String business, String key, String field) {
        return getJedisCluster().hexists(RedisUtil.buildKey(getBusiness(business), key), field);
    }

    @Override
    public Long hdel(String business, String key, String... fields) {
        return getJedisCluster().hdel(RedisUtil.buildKey(getBusiness(business), key), fields);
    }

    @Override
    public Long hlen(String business, String key) {
        return getJedisCluster().hlen(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Set hkeys(String business, String key) {
        return getJedisCluster().hkeys(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public List mutliExecute(String business, List commands) {
        String tmpBusiness = getBusiness(business);
        if(Strings.isNullOrEmpty(tmpBusiness)){
            return null;
        }
        List result = Lists.newArrayList();
        Class clazz = RedisClusterImpl.class;
        for(Command c : commands){
            List params = c.getParameters();
            int paramsSize = params.size();
            Class[] types = new Class[paramsSize+1];
            Object[] paramss = new Object[paramsSize+1];
            paramss[0] = tmpBusiness;
            types[0] = tmpBusiness.getClass();
            for(int i=0;i lrange(String business, String key, Long start, Long end) {
        return getJedisCluster().lrange(RedisUtil.buildKey(getBusiness(business), key), start, end);
    }

    @Override
    public String lindex(String business, String key, Long index) {
        return getJedisCluster().lindex(RedisUtil.buildKey(getBusiness(business), key), index);
    }

    @Override
    public String rpop(String business, String key) {
        return getJedisCluster().rpop(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long rpush(String business, String key, String... values) {
        return getJedisCluster().rpush(RedisUtil.buildKey(getBusiness(business), key), values);
    }

    @Override
    public Long sadd(String business, String key, String... values) {
        return getJedisCluster().sadd(RedisUtil.buildKey(getBusiness(business), key), values);
    }

    @Override
    public Long scard(String business, String key) {
        return getJedisCluster().scard(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public String setex(String business, String key, Integer seconds, String value) {
        return getJedisCluster().setex(RedisUtil.buildKey(getBusiness(business), key), seconds, value);
    }

    @Override
    public Set smembers(String business, String key) {
        return getJedisCluster().smembers(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long srem(String business, String key, String... values) {
        return getJedisCluster().srem(RedisUtil.buildKey(getBusiness(business), key), values);
    }

    @Override
    public String ltrim(String business, String key, Long start, Long end) {
        return getJedisCluster().ltrim(RedisUtil.buildKey(getBusiness(business), key), start, end);
    }

    @Override
    public Boolean sismember(String business, String key, String member) {
        return getJedisCluster().sismember(RedisUtil.buildKey(getBusiness(business), key), member);
    }


    @Override
    public String set(String business, String key, String value, ExistEnum existEnum, ExpireTimeEnum expireTimeEnum, long time) {
        // existEnum.name(), existEnum.name(), time
        SetParams setParams = new SetParams();
        if(existEnum.name() == "NX") setParams.nx();
        if(existEnum.name() == "XX") setParams.xx();
        if(expireTimeEnum.name() == "EX") setParams.ex((int) time);
        if(expireTimeEnum.name() == "PX") setParams.px(time);
        return getJedisCluster().set(RedisUtil.buildKey(getBusiness(business), key), value, setParams);
    }

    @Override
    public String set(String business, byte[] key, byte[] value, ExistEnum existEnum, ExpireTimeEnum expireTimeEnum, long time) {
        // existEnum.name(), existEnum.name(), time
        SetParams setParams = new SetParams();
        if(existEnum.name() == "NX") setParams.nx();
        if(existEnum.name() == "XX") setParams.xx();
        if(expireTimeEnum.name() == "EX") setParams.ex((int) time);
        if(expireTimeEnum.name() == "PX") setParams.px(time);
        return getJedisCluster().set(RedisUtil.buildKey(getBusiness(business), key), value, setParams);
    }

    @Override
    public String getSet(String business, String key, String value) {
        return getJedisCluster().getSet(RedisUtil.buildKey(getBusiness(business), key), value);
    }

    @Override
    public byte[] getSet(String business, byte[] key, byte[] value) {
        return getJedisCluster().getSet(RedisUtil.buildKey(getBusiness(business), key), value);
    }

    @Override
    public Long append(String business, byte[] key, byte[] value) {
        return getJedisCluster().append(RedisUtil.buildKey(getBusiness(business), key), value);
    }

    @Override
    public byte[] substr(String business, byte[] key, int start, int end) {
        return getJedisCluster().substr(RedisUtil.buildKey(getBusiness(business), key), start, end);
    }

    @Override
    public Long hset(String business, byte[] key, byte[] field, byte[] value) {
        return getJedisCluster().hset(RedisUtil.buildKey(getBusiness(business), key), field, value);
    }

    @Override
    public byte[] hget(String business, byte[] key, byte[] field) {
        return getJedisCluster().hget(RedisUtil.buildKey(getBusiness(business), key), field);
    }

    @Override
    public Long hsetnx(String business, byte[] key, byte[] field, byte[] value) {
        return getJedisCluster().hsetnx(RedisUtil.buildKey(getBusiness(business), key), field, value);
    }

    @Override
    public String hmset(String business, byte[] key, Map hash) {
        return getJedisCluster().hmset(RedisUtil.buildKey(getBusiness(business), key), hash);
    }

    @Override
    public List hmget(String business, byte[] key, byte[]... fields) {
        return getJedisCluster().hmget(RedisUtil.buildKey(getBusiness(business), key), fields);
    }

    @Override
    public Long hincrBy(String business, byte[] key, byte[] field, long value) {
        return getJedisCluster().hincrBy(RedisUtil.buildKey(getBusiness(business), key), field, value);
    }

    @Override
    public Boolean hexists(String business, byte[] key, byte[] field) {
        return getJedisCluster().hexists(RedisUtil.buildKey(getBusiness(business), key), field);
    }

    @Override
    public Long hdel(String business, byte[] key, byte[]... fields) {
        return getJedisCluster().hdel(RedisUtil.buildKey(getBusiness(business), key), fields);
    }

    @Override
    public Set hkeys(String business, byte[] key) {
        return getJedisCluster().hkeys(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Map hgetAll(String business, byte[] key) {
        return getJedisCluster().hgetAll(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long rpush(String business, byte[] key, byte[]... values) {
        return getJedisCluster().rpush(RedisUtil.buildKey(getBusiness(business), key), values);
    }

    @Override
    public Long lpush(String business, byte[] key, byte[]... values) {
        return getJedisCluster().lpush(RedisUtil.buildKey(getBusiness(business), key), values);
    }

    @Override
    public List lrange(String business, byte[] key, Long start, Long end) {
        return getJedisCluster().lrange(RedisUtil.buildKey(getBusiness(business), key), start, end);
    }

    @Override
    public byte[] lindex(String business, byte[] key, long index) {
        return getJedisCluster().lindex(RedisUtil.buildKey(getBusiness(business), key), index);
    }

    @Override
    public Long lrem(String business, byte[] key, long count, byte[] value) {
        return getJedisCluster().lrem(RedisUtil.buildKey(getBusiness(business), key), count, value);
    }

    @Override
    public byte[] lpop(String business, byte[] key) {
        return getJedisCluster().lpop(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public byte[] rpop(String business, byte[] key) {
        return getJedisCluster().rpop(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long sadd(String business, byte[] key, byte[]... values) {
        return getJedisCluster().sadd(RedisUtil.buildKey(getBusiness(business), key), values);
    }

    @Override
    public Set smembers(String business, byte[] key) {
        return getJedisCluster().smembers(RedisUtil.buildKey(getBusiness(business), key));
    }

    @Override
    public Long srem(String business, byte[] key, byte[]... values) {
        return getJedisCluster().srem(RedisUtil.buildKey(getBusiness(business), key), values);
    }

    @Override
    public Boolean sismember(String business, byte[] key, byte[] member) {
        return getJedisCluster().sismember(RedisUtil.buildKey(getBusiness(business), key), member);
    }


    private String getBusiness(String business){
        if(Strings.isNullOrEmpty(redisClusterConnectionFactory.getBusiness())){
            return business;
        } else {
            return redisClusterConnectionFactory.getBusiness();
        }
    }

    private JedisCluster getJedisCluster(){
        return redisClusterConnectionFactory.getJedisCluster();
    }


    
}