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

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

Introduction

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

Prototype

@Nullable
Boolean zAdd(byte[] key, double score, byte[] value);

Source Link

Document

Add value to a sorted set at key , or update its score if it already exists.

Usage

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

@SuppressWarnings("unchecked")
@Override/*  w  w w .j  a v a2  s.  c om*/
public void put(final Object key, final Object value) {
    final byte[] k = computeKey(key);
    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            waitForLock(connection);
            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();
            return null;
        }
    }, 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);//  w  w w  . j av  a2  s.c om
            return connection.zAdd(rawKey, score, rawValue);
        }
    }, true);
}

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

@SuppressWarnings("unchecked")
@Override/*from  w w  w  . j a  va  2s . 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.zxy.commons.cache.RedisUtils.java

/**
 * Add {@code value} to a sorted set at {@code key}, or update its {@code score} if it already exists.
 * <p>/*from  w w w. jav a  2 s .  c  o m*/
 * See http://redis.io/commands/zadd
 * 
 * @param key key
 * @param score score
 * @param value value
 * @return Boolean
 */
public static Boolean zAdd(byte[] key, double score, byte[] value) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.zAdd(key, score, value);
        }
    });
}

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

/**
 * Add {@code value} to a sorted set at {@code key}, or update its {@code score} if it already exists.
 * <p>/*  ww  w  .  j  a v a  2s  . c o m*/
 * See http://redis.io/commands/zadd
 * 
 * @param key key
 * @param score score
 * @param value value
 * @return Boolean
 */
public Boolean zAdd(byte[] key, double score, byte[] value) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.zAdd(key, score, value);
        }
    });
}

From source file:org.springframework.data.redis.cache.RedisCache.java

public void put(final Object key, final Object value) {

    final byte[] keyBytes = computeKey(key);
    final byte[] valueBytes = convertToBytesIfNecessary(template.getValueSerializer(), value);

    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {

            waitForLock(connection);//from   w ww  .  j a  v a 2 s .  c om

            connection.multi();

            connection.set(keyBytes, valueBytes);
            connection.zAdd(setName, 0, keyBytes);

            if (expiration > 0) {
                connection.expire(keyBytes, expiration);
                // update the expiration of the set of keys as well
                connection.expire(setName, expiration);
            }
            connection.exec();

            return null;
        }
    }, true);
}

From source file:org.springframework.data.redis.cache.RedisCache.java

public ValueWrapper putIfAbsent(Object key, final Object value) {

    final byte[] keyBytes = computeKey(key);
    final byte[] valueBytes = convertToBytesIfNecessary(template.getValueSerializer(), value);

    return toWrapper(template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {

            waitForLock(connection);/*ww w .j  a va 2  s .com*/

            Object resultValue = value;
            boolean valueWasSet = connection.setNX(keyBytes, valueBytes);
            if (valueWasSet) {
                connection.zAdd(setName, 0, keyBytes);
                if (expiration > 0) {
                    connection.expire(keyBytes, expiration);
                    // update the expiration of the set of keys as well
                    connection.expire(setName, expiration);
                }
            } else {
                resultValue = deserializeIfNecessary(template.getValueSerializer(), connection.get(keyBytes));
            }

            return resultValue;
        }
    }, true));
}