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

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

Introduction

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

Prototype

void multi();

Source Link

Document

Mark the start of a transaction block.

Usage

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

@SuppressWarnings("unchecked")
@Override/* w ww .  j a  v a 2s.com*/
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//from www.  j av  a  2s  .c om
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

/**
 * Mark the start of a transaction block. <br>
 * Commands will be queued and can then be executed by calling {@link #exec()} or rolled back using {@link #discard()}
 * <p>/*from  w  ww. ja  v a2 s.  c  om*/
 * See http://redis.io/commands/multi
 */
public static void multi() {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            redis.multi();
            return null;
        }
    });
}

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

/**
 * Mark the start of a transaction block. <br>
 * Commands will be queued and can then be executed by calling {@link #exec()} or rolled back using {@link #discard()}
 * <p>/* w  w  w.j  a  v  a 2s  .c o  m*/
 * See http://redis.io/commands/multi
 */
public void multi() {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            redis.multi();
            return null;
        }
    });
}

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);/*www  .j av a  2 s.  c o  m*/

            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.core.RedisConnectionUtils.java

private static void potentiallyRegisterTransactionSynchronisation(RedisConnectionHolder connHolder,
        final RedisConnectionFactory factory) {

    if (isActualNonReadonlyTransactionActive()) {

        if (!connHolder.isTransactionSyncronisationActive()) {
            connHolder.setTransactionSyncronisationActive(true);

            RedisConnection conn = connHolder.getConnection();
            conn.multi();

            TransactionSynchronizationManager
                    .registerSynchronization(new RedisTransactionSynchronizer(connHolder, conn, factory));
        }//from  w  ww. j a va  2 s .  c  o  m
    }
}