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

redis.RedisCluster Maven / Gradle / Ivy

There is a newer version: 4.13.1
Show newest version
package redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pluggable.CustomStorageWrapper;
import pluggable.Pipeline;
import redis.clients.jedis.JedisCluster;
import redis.common.CommonRedis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

class RedisCluster implements CustomStorageWrapper {
    private final CommonRedis _commonRedis;
    private final JedisCluster jedis;

    public static final String DEFAULT_HASHTAG = "{SPLITIO}" ;

    private static final Logger _log = LoggerFactory.getLogger(RedisCluster.class);

    private String validateHashtag(String hashtag) {
        if (hashtag == null) {
            _log.warn("The hashtag wasn't set. It's going to use the default hashtag");
            return DEFAULT_HASHTAG;
        }
        if (hashtag.length() <= 2) {
            _log.warn("The hashtag is too short. It's going to use the default hashtag");
            return DEFAULT_HASHTAG;
        }
        if (!hashtag.startsWith("{")) {
            _log.warn("The hashtag doesn't have '{'. It's going to use the default hashtag");
            return DEFAULT_HASHTAG;
        }
        if (!hashtag.endsWith("}")) {
            _log.warn("The hashtag doesn't have '}'. It's going to use the default hashtag");
            return DEFAULT_HASHTAG;
        }

        return hashtag;
    }

    public RedisCluster(JedisCluster jedisCluster, String prefix, String hashtag) {
        this.jedis = jedisCluster;
        _commonRedis = CommonRedis.create(validateHashtag(hashtag) + prefix);
    }

    @Override
    public String get(String key) throws Exception {
        try {
            return jedis.get(_commonRedis.buildKeyWithPrefix(key));
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public List getMany(List keys) throws Exception {
        if(keys == null || keys.isEmpty()){
            return new ArrayList<>();
        }
        try {
            keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList());

            return jedis.mget(keys.toArray(new String[keys.size()]));
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public void set(String key, String item) throws Exception {
        try {
            if(key.contains(CommonRedis.TELEMETRY_INIT)) {
                String[] splittedKey = key.split("::");
                jedis.hset(_commonRedis.buildKeyWithPrefix(splittedKey[0]), splittedKey[1], item);
                return;
            }
            jedis.set(_commonRedis.buildKeyWithPrefix(key), item);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public void hSet(String key, String field, String item) throws Exception {
        try {
            jedis.hset(_commonRedis.buildKeyWithPrefix(key), field, item);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public void delete(List keys) throws Exception {
        if(keys == null || keys.isEmpty()){
            return ;
        }
        try {
            keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList());

            jedis.del(keys.toArray(new String[keys.size()]));
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public String getAndSet(String key, String item) throws Exception {
        //Todo if this method isn't used we should deprecated
        try {
            return jedis.getSet(_commonRedis.buildKeyWithPrefix(key), item);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public Set getKeysByPrefix(String prefix) throws Exception {
        try {
            Set keysWithPrefix = jedis.keys(_commonRedis.buildKeyWithPrefix(prefix));
            keysWithPrefix = keysWithPrefix.stream().map(key -> key.replace(_commonRedis.getPrefix() + ".", "")).collect(Collectors.toSet());
            return keysWithPrefix;
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public long increment(String key, long value) throws Exception {
        try {
            return jedis.incrBy(_commonRedis.buildKeyWithPrefix(key), value);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public long hIncrement(String key, String field, long value) throws RedisException {
        try {
            return jedis.hincrBy(_commonRedis.buildKeyWithPrefix(key), field, value);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public long decrement(String key, long value) throws Exception {
        try {
            return jedis.decrBy(_commonRedis.buildKeyWithPrefix(key), value);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public long pushItems(String key, List items) throws Exception {
        try {
            long addedItems = jedis.rpush(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()]));
            if((CommonRedis.EVENTS_KEY.equals(key) || CommonRedis.IMPRESSIONS_KEY.equals(key)) && addedItems == items.size()) {
                jedis.pexpire(key, CommonRedis.IMPRESSIONS_OR_EVENTS_DEFAULT_TTL);
            }
            return addedItems;
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public List popItems(String key, long count) throws Exception {
        try {
            String keyWithPrefix = _commonRedis.buildKeyWithPrefix(key);
            List items = jedis.lrange(keyWithPrefix, 0, count-1);
            int fetchedCount = items.size();
            jedis.ltrim(keyWithPrefix, fetchedCount, -1);
            return items;
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    // Return length of redis set.
    @Override
    public long getItemsCount(String key) throws Exception {
        try {
            return jedis.scard(_commonRedis.buildKeyWithPrefix(key));
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public boolean itemContains(String key, String item) throws Exception {
        try {
            return jedis.sismember(_commonRedis.buildKeyWithPrefix(key), item);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public void addItems(String key, List items) throws Exception {
        try {
            jedis.sadd(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()]));
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public void removeItems(String key, List items) throws Exception {
        try {
            jedis.srem(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()]));
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public List getItems(List keys) throws Exception {
        if(keys == null || keys.isEmpty()){
            return new ArrayList<>();
        }
        try {
            keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList());

            return jedis.mget(keys.toArray(new String[keys.size()]));
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public Set getMembers(String key) throws Exception {
        Set items;
        try {
            items = jedis.smembers(_commonRedis.buildKeyWithPrefix(key));
            return new HashSet<>(items);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public boolean connect() throws Exception {
        try {
            return jedis.getClusterNodes().entrySet().stream().findFirst().map(e -> e.getValue().getResource().isConnected()).orElse(false);
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public boolean disconnect() throws Exception {
        try {
            jedis.close();

            return true;
        } catch (Exception ex) {
            throw new RedisException(ex.getMessage());
        }
    }

    @Override
    public Pipeline pipeline() throws Exception {
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy