Example usage for org.springframework.data.redis.core RedisCallback RedisCallback

List of usage examples for org.springframework.data.redis.core RedisCallback RedisCallback

Introduction

In this page you can find the example usage for org.springframework.data.redis.core RedisCallback RedisCallback.

Prototype

RedisCallback

Source Link

Usage

From source file:grails.plugin.cache.redis.GrailsRedisCache.java

@SuppressWarnings("unchecked")
@Override/*from   www .ja va2 s  . c  o  m*/
public void clear() {
    // need to del each key individually
    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            // another clear is on-going
            if (connection.exists(cacheLockName)) {
                return null;
            }

            try {
                connection.set(cacheLockName, cacheLockName);

                int offset = 0;
                boolean finished = false;

                do {
                    // need to paginate the keys
                    Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE,
                            (offset + 1) * PAGE_SIZE - 1);
                    finished = keys.size() < PAGE_SIZE;
                    offset++;
                    if (!keys.isEmpty()) {
                        connection.del(keys.toArray(new byte[keys.size()][]));
                    }
                } while (!finished);

                connection.del(setName);
                return null;

            } finally {
                connection.del(cacheLockName);
            }
        }
    }, true);
}

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);/*w  ww.j  a v a 2s .c  om*/
            potentiallyUsePsetEx(connection);
            return null;
        }

        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.DefaultSetOperations.java

public Set<V> distinctRandomMembers(K key, final long count) {
    if (count < 0) {
        throw new IllegalArgumentException(
                "Negative count not supported. " + "Use randomMembers to allow duplicate elements.");
    }//www . j  ava2 s  . co m
    final byte[] rawKey = rawKey(key);
    Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
        public Set<byte[]> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return new HashSet<byte[]>(connection.sRandMember(rawKey, count));
        }
    }, true);

    return deserializeValues(rawValues);
}

From source file:com.zxy.commons.cache.RedisHelper.java

/**
 * Get the value of {@code key}.//from   ww  w.  j  a  va 2  s .co  m
 * <p>
 * See http://redis.io/commands/get
 * 
 * @param key must not be {@literal null}.
 * @return value
 */
public byte[] get(byte[] key) {
    return redisTemplate.execute(new RedisCallback<byte[]>() {
        @Override
        public byte[] doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.get(key);
        }
    });
}

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

public Boolean putIfAbsent(K key, HK hashKey, HV value) {
    final byte[] rawKey = rawKey(key);
    final byte[] rawHashKey = rawHashKey(hashKey);
    final byte[] rawHashValue = rawHashValue(value);

    return execute(new RedisCallback<Boolean>() {

        public Boolean doInRedis(RedisConnection connection) {
            connection.select(dbIndex);/*www .j  a va2s. c om*/
            return connection.hSetNX(rawKey, rawHashKey, rawHashValue);
        }
    }, true);
}

From source file:com.zxy.commons.cache.RedisUtils.java

/**
 * Get the value of {@code key}.//from   w ww. ja v  a  2  s.  com
 * <p>
 * See http://redis.io/commands/get
 * 
 * @param <T> return object type
 * @param key must not be {@literal null}.
 * @param callback callback
 * @return value
*/
public static <T> T get(byte[] key, RedisTransferCallback<T> callback) {
    return redisTemplate.execute(new RedisCallback<T>() {
        @Override
        public T doInRedis(RedisConnection redis) throws DataAccessException {
            byte[] value = redis.get(key);
            if (value == null) {
                return null;
            }
            return callback.transfer(value);
        }
    });
}

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

public Long rightPushAll(K key, V... values) {
    final byte[] rawKey = rawKey(key);
    final byte[][] rawValues = rawValues(values);
    return execute(new RedisCallback<Long>() {
        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);/*from   www  .  j  av a2 s . co m*/
            return connection.rPush(rawKey, rawValues);
        }
    }, true);
}

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

public Set<V> reverseRangeByScore(K key, final double min, final double max) {
    final byte[] rawKey = rawKey(key);

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

        public Set<byte[]> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);/* ww w  . ja v  a2s .c  o m*/
            return connection.zRevRangeByScore(rawKey, min, max);
        }
    }, true);

    return deserializeValues(rawValues);
}

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

public List<HV> values(K key) {
    final byte[] rawKey = rawKey(key);

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

        public List<byte[]> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);// w  w w. j a  v  a2s. c o m
            return connection.hVals(rawKey);
        }
    }, true);

    return deserializeHashValues(rawValues);
}

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

public List<V> randomMembers(K key, final long count) {
    if (count < 0) {
        throw new IllegalArgumentException(
                "Use a positive number for count. " + "This method is already allowing duplicate elements.");
    }/* www  .j  a v a2  s . c  o  m*/
    final byte[] rawKey = rawKey(key);
    List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {
        public List<byte[]> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            return connection.sRandMember(rawKey, -count);
        }
    }, true);

    return deserializeValues(rawValues);
}