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

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

Introduction

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

Prototype

List<Object> exec();

Source Link

Document

Executes all queued commands in a transaction started with #multi() .

Usage

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 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:grails.plugin.cache.redis.GrailsRedisCache.java

@SuppressWarnings("unchecked")
@Override/*  w w w.  ja va 2  s  .  com*/
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

/**
 * Executes all queued commands in a transaction started with {@link #multi()}. <br>
 * If used along with {@link #watch(byte[])} the operation will fail if any of watched keys has been modified.
 * <p>/*w w w. j  a  v a 2 s  .com*/
 * See http://redis.io/commands/exec
 * 
 * @return List of replies for each executed command.
 */
public static List<Object> exec() {
    return redisTemplate.execute(new RedisCallback<List<Object>>() {
        @Override
        public List<Object> doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.exec();
        }
    });
}

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

/**
 * Executes all queued commands in a transaction started with {@link #multi()}. <br>
 * If used along with {@link #watch(byte[])} the operation will fail if any of watched keys has been modified.
 * <p>/*from  w  w  w  .j a  va 2 s .  c  o  m*/
 * See http://redis.io/commands/exec
 * 
 * @return List of replies for each executed command.
 */
public List<Object> exec() {
    return redisTemplate.execute(new RedisCallback<List<Object>>() {
        @Override
        public List<Object> doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.exec();
        }
    });
}

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);//w w  w . java2 s  .com

            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);
}