Example usage for org.springframework.data.redis.connection RedisConnection select

List of usage examples for org.springframework.data.redis.connection RedisConnection select

Introduction

In this page you can find the example usage for org.springframework.data.redis.connection RedisConnection select.

Prototype

void select(int dbIndex);

Source Link

Document

Select the DB with given positive dbIndex .

Usage

From source file:com.mauersu.util.redis.DefaultHashOperations.java

public void putAll(K key, Map<? extends HK, ? extends HV> m) {
    if (m.isEmpty()) {
        return;/*from ww  w  .ja va2 s .  c o  m*/
    }

    final byte[] rawKey = rawKey(key);

    final Map<byte[], byte[]> hashes = new LinkedHashMap<byte[], byte[]>(m.size());

    for (Map.Entry<? extends HK, ? extends HV> entry : m.entrySet()) {
        hashes.put(rawHashKey(entry.getKey()), rawHashValue(entry.getValue()));
    }

    execute(new RedisCallback<Object>() {

        public Object doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            connection.hMSet(rawKey, hashes);
            return null;
        }
    }, true);
}

From source file:com.mauersu.util.redis.DefaultHashOperations.java

public List<HV> multiGet(K key, Collection<HK> fields) {
    if (fields.isEmpty()) {
        return Collections.emptyList();
    }//from   w  ww  .ja v  a 2  s . c om

    final byte[] rawKey = rawKey(key);

    final byte[][] rawHashKeys = new byte[fields.size()][];

    int counter = 0;
    for (HK hashKey : fields) {
        rawHashKeys[counter++] = rawHashKey(hashKey);
    }

    List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {

        public List<byte[]> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.hMGet(rawKey, rawHashKeys);
        }
    }, true);

    return deserializeHashValues(rawValues);
}

From source file:com.mauersu.util.redis.DefaultValueOperations.java

public void set(K key, V value, final long timeout, final TimeUnit unit) {
    final byte[] rawKey = rawKey(key);
    final byte[] rawValue = rawValue(value);

    execute(new RedisCallback<Object>() {

        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            connection.select(dbIndex);
            potentiallyUsePsetEx(connection);
            return null;
        }//from  ww w  . j  a  v  a2 s .com

        public void potentiallyUsePsetEx(RedisConnection connection) {

            if (!TimeUnit.MILLISECONDS.equals(unit) || !failsafeInvokePsetEx(connection)) {
                connection.select(dbIndex);
                connection.setEx(rawKey, TimeoutUtils.toSeconds(timeout, unit), rawValue);
            }
        }

        private boolean failsafeInvokePsetEx(RedisConnection connection) {

            boolean failed = false;
            try {
                connection.select(dbIndex);
                connection.pSetEx(rawKey, timeout, rawValue);
            } catch (UnsupportedOperationException e) {
                // in case the connection does not support pSetEx return false to allow fallback to other operation.
                failed = true;
            }
            return !failed;
        }

    }, true);
}

From source file:com.mauersu.util.redis.DefaultZSetOperations.java

public Long removeRange(K key, final long start, final long end) {
    final byte[] rawKey = rawKey(key);
    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.zRemRange(rawKey, start, end);
        }/*from   w  w  w . j  av  a  2 s . co  m*/
    }, true);
}

From source file:com.mauersu.util.redis.DefaultZSetOperations.java

public Long removeRangeByScore(K key, final double min, final double max) {
    final byte[] rawKey = rawKey(key);
    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.zRemRangeByScore(rawKey, min, max);
        }//from w ww  .ja va  2  s. c o  m
    }, true);
}

From source file:com.mauersu.util.redis.DefaultZSetOperations.java

public Long intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
    final byte[][] rawKeys = rawKeys(key, otherKeys);
    final byte[] rawDestKey = rawKey(destKey);
    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.zInterStore(rawDestKey, rawKeys);
        }//from  ww  w .j  av a  2s.  c om
    }, true);
}

From source file:com.mauersu.util.redis.DefaultZSetOperations.java

public Long count(K key, final double min, final double max) {
    final byte[] rawKey = rawKey(key);

    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.zCount(rawKey, min, max);
        }//from   ww  w .  j  av a 2  s.co m
    }, true);
}

From source file:com.mauersu.util.redis.DefaultZSetOperations.java

public Long unionAndStore(K key, Collection<K> otherKeys, K destKey) {
    final byte[][] rawKeys = rawKeys(key, otherKeys);
    final byte[] rawDestKey = rawKey(destKey);
    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.zUnionStore(rawDestKey, rawKeys);
        }// w ww  .  j  a v a  2  s  .  c om
    }, true);
}

From source file:com.mauersu.util.redis.DefaultZSetOperations.java

public Boolean add(final K key, final V value, final double score) {
    final byte[] rawKey = rawKey(key);
    final byte[] rawValue = rawValue(value);

    return execute(new RedisCallback<Boolean>() {

        public Boolean doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.zAdd(rawKey, score, rawValue);
        }/*from  w ww . j  av a  2  s . com*/
    }, true);
}

From source file:com.mauersu.util.redis.DefaultZSetOperations.java

public Long add(K key, Set<TypedTuple<V>> tuples) {
    final byte[] rawKey = rawKey(key);
    final Set<Tuple> rawValues = rawTupleValues(tuples);

    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.zAdd(rawKey, rawValues);
        }/*from ww  w.j  av a  2 s  .  com*/
    }, true);
}