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  .j  ava 2  s  .c o m
public ValueWrapper putIfAbsent(final Object key, final Object value) {
    final byte[] k = computeKey(key);
    return (ValueWrapper) template.execute(new RedisCallback<ValueWrapper>() {
        public ValueWrapper doInRedis(RedisConnection connection) throws DataAccessException {
            waitForLock(connection);
            byte[] bs = connection.get(computeKey(key));

            if (bs == null) {
                connection.multi();
                connection.set(k, template.getValueSerializer().serialize(value));
                connection.zAdd(setName, 0, k);

                // Set key time to live when expiration has been configured.
                if (ttl > NEVER_EXPIRE) {
                    connection.expire(k, ttl);
                    connection.expire(setName, ttl);
                }

                connection.exec();
            }

            bs = connection.get(computeKey(key));
            return (bs == null ? null : newValueWrapper(template.getValueSerializer().deserialize(bs)));
        }
    }, true);
}

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

public Set<TypedTuple<V>> rangeWithScores(K key, final long start, final long end) {
    final byte[] rawKey = rawKey(key);

    Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() {

        public Set<Tuple> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);//from   w  w  w . j  a  v  a2s  . c  om
            return connection.zRangeWithScores(rawKey, start, end);
        }
    }, true);

    return deserializeTupleValues(rawValues);
}

From source file:io.gravitee.repository.redis.management.internal.impl.EventRedisRepositoryImpl.java

@Override
public RedisEvent saveOrUpdate(RedisEvent event) {
    redisTemplate.executePipelined(new RedisCallback<Object>() {
        @Override/* w ww. j av a2  s  .  co  m*/
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            redisTemplate.opsForHash().put(REDIS_KEY, event.getId(), event);
            redisTemplate.opsForSet().add(REDIS_KEY + ":type:" + event.getType(), event.getId());
            redisTemplate.opsForZSet().add(REDIS_KEY + ":updated_at", event.getId(), event.getCreatedAt());
            if (event.getProperties() != null) {
                event.getProperties().forEach((key, value) -> redisTemplate.opsForSet()
                        .add(REDIS_KEY + ":" + key + ":" + value, event.getId()));
            }

            return null;
        }
    });

    return event;
}

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

public void multiSet(Map<? extends K, ? extends V> m) {
    if (m.isEmpty()) {
        return;/*  w  w w .j  a va 2  s  .c om*/
    }

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

    for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
        rawKeys.put(rawKey(entry.getKey()), rawValue(entry.getValue()));
    }

    execute(new RedisCallback<Object>() {

        public Object doInRedis(RedisConnection connection) {
            connection.select(dbIndex);
            connection.mSet(rawKeys);
            return null;
        }
    }, true);
}

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

public Set<V> members(K key) {
    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  a2 s .c o  m*/
            return connection.sMembers(rawKey);
        }
    }, true);

    return deserializeValues(rawValues);
}

From source file:com.company.project.service.dao.RedisDao.java

/**
 * 4.10. Pipelining - http://docs.spring.io/spring-data/redis/docs/current/reference/html/
 * @param key// w w  w  .j  av  a  2  s  .  c o  m
 * @return 
 */
public List<Object> pipelining(final String key) {
    //pop a specified number of items from a queue
    List<Object> results = redisTemplate.executePipelined(new RedisCallback<Object>() {
        @Override
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            StringRedisConnection stringRedisConn = (StringRedisConnection) connection;
            for (int i = 0; i < 10; i++) {
                stringRedisConn.rPop(key);
            }
            return null;
        }
    });

    return results;
}

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

public Long remove(K key, final long count, Object value) {
    final byte[] rawKey = rawKey(key);
    final byte[] rawValue = rawValue(value);
    return execute(new RedisCallback<Long>() {

        public Long doInRedis(RedisConnection connection) {
            connection.select(dbIndex);//from   ww  w .ja  va2s  .  co m
            return connection.lRem(rawKey, count, rawValue);
        }
    }, true);
}

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

public Set<TypedTuple<V>> reverseRangeWithScores(K key, final long start, final long end) {
    final byte[] rawKey = rawKey(key);

    Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() {

        public Set<Tuple> doInRedis(RedisConnection connection) {
            connection.select(dbIndex);/*from  www .j  a  v a 2 s .c  o  m*/
            return connection.zRevRangeWithScores(rawKey, start, end);
        }
    }, true);

    return deserializeTupleValues(rawValues);
}

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 w  w.ja v a 2s. c  o  m*/

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

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

    return execute(new RedisCallback<Boolean>() {

        public Boolean doInRedis(RedisConnection connection) {
            connection.select(dbIndex);//from  w ww  .j  ava2 s  . c o  m
            return connection.sMove(rawKey, rawDestKey, rawValue);
        }
    }, true);
}