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:com.mauersu.util.redis.DefaultListOperations.java

public Long rightPushIfPresent(K key, V value) {
    final byte[] rawKey = rawKey(key);
    final byte[] rawValue = rawValue(value);
    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);//  www. j  a  va  2s  .  c  om
            return connection.rPushX(rawKey, rawValue);
        }
    }, true);
}

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

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

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

        public Set<byte[]> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);//  w  ww  .j  a  v  a  2  s .c  om
            return connection.zRevRangeByScore(rawKey, min, max, offset, count);
        }
    }, true);

    return deserializeValues(rawValues);
}

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

public void delete(K key, Object... hashKeys) {
    final byte[] rawKey = rawKey(key);
    final byte[][] rawHashKeys = rawHashKeys(hashKeys);

    execute(new RedisCallback<Object>() {

        public Object doInRedis(RedisConnection connection) {
            connection.select(dbIndex);/*w ww  .  j  a  v  a 2  s  . c  o m*/
            connection.hDel(rawKey, rawHashKeys);
            return null;
        }
    }, true);
}

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

/**
 * Get the value of {@code key}./*from   w  w w  . ja v  a  2  s.c om*/
 * <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 <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.zxy.commons.cache.RedisUtils.java

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

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

public Long remove(K key, Object... 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  a  va2  s  .  c  o m
            return connection.sRem(rawKey, rawValues);
        }
    }, true);
}

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

public Long rightPush(K key, V pivot, V value) {
    final byte[] rawKey = rawKey(key);
    final byte[] rawPivot = rawValue(pivot);
    final byte[] rawValue = rawValue(value);

    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);/*from w w  w  . j av a2s  . c  om*/
            return connection.lInsert(rawKey, Position.AFTER, rawPivot, rawValue);
        }
    }, true);
}

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

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

    return execute(new RedisCallback<Boolean>() {

        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            connection.select(dbIndex);/*  w w w.  ja v a 2s. c o  m*/
            return connection.setNX(rawKey, rawValue);
        }
    }, true);
}

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

@SuppressWarnings("unchecked")
@Override//from  w  w  w  .j a v  a  2 s . c o  m
public Collection<Object> getAllKeys() {
    Set<byte[]> serializedKeys = (Set<byte[]>) template.execute(new RedisCallback<Set<byte[]>>() {
        public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
            Set<byte[]> allKeys = new HashSet<byte[]>();
            int offset = 0;
            boolean finished = false;
            while (!finished) {
                // need to paginate the keys
                Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE,
                        (offset + 1) * PAGE_SIZE - 1);
                allKeys.addAll(keys);
                finished = keys.size() < PAGE_SIZE;
                offset++;
            }
            return allKeys;
        }
    }, true);

    @SuppressWarnings("rawtypes")
    Collection<Object> keys = new HashSet(serializedKeys.size());
    RedisSerializer<byte[]> keySerializer = template.getKeySerializer();
    for (byte[] bytes : serializedKeys) {
        keys.add(keySerializer.deserialize(bytes));
    }

    return keys;
}

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

public Map<HK, HV> entries(K key) {
    final byte[] rawKey = rawKey(key);

    Map<byte[], byte[]> entries = execute(new RedisCallback<Map<byte[], byte[]>>() {

        public Map<byte[], byte[]> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);//from  ww w  .  j  a va2s.  c o  m
            return connection.hGetAll(rawKey);
        }
    }, true);

    return deserializeHashMap(entries);
}