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

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

Introduction

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

Prototype

@Nullable
Boolean setEx(byte[] key, long seconds, byte[] value);

Source Link

Document

Set the value and expiration in seconds for key .

Usage

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

public void set(K key, V value, final long timeout, final TimeUnit unit) {
    final byte[] rawKey = rawKey(key);
    final byte[] rawValue = rawValue(value);

    execute(new RedisCallback<Object>() {

        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            connection.select(dbIndex);/*from www  .  ja  va2s  .c om*/
            potentiallyUsePsetEx(connection);
            return null;
        }

        public void potentiallyUsePsetEx(RedisConnection connection) {

            if (!TimeUnit.MILLISECONDS.equals(unit) || !failsafeInvokePsetEx(connection)) {
                connection.select(dbIndex);
                connection.setEx(rawKey, TimeoutUtils.toSeconds(timeout, unit), rawValue);
            }
        }

        private boolean failsafeInvokePsetEx(RedisConnection connection) {

            boolean failed = false;
            try {
                connection.select(dbIndex);
                connection.pSetEx(rawKey, timeout, rawValue);
            } catch (UnsupportedOperationException e) {
                // in case the connection does not support pSetEx return false to allow fallback to other operation.
                failed = true;
            }
            return !failed;
        }

    }, true);
}

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

/**
 * Set the {@code value} and expiration in {@code seconds} for {@code key}.
 * <p>//from   ww  w .ja va2s .  c  om
 * See http://redis.io/commands/setex
 * 
 * @param key must not be {@literal null}.
 * @param seconds exprise time
 * @param value must not be {@literal null}.
 */
public static void setEx(byte[] key, long seconds, byte[] value) {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            redis.setEx(key, seconds, value);
            return null;
        }
    });
}

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

/**
 * Set the {@code value} and expiration in {@code seconds} for {@code key}.
 * <p>/*  w  ww . j a  v  a 2 s.c o  m*/
 * See http://redis.io/commands/setex
 * 
 * @param key must not be {@literal null}.
 * @param seconds exprise time
 * @param value must not be {@literal null}.
 */
public void setEx(byte[] key, long seconds, byte[] value) {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            redis.setEx(key, seconds, value);
            return null;
        }
    });
}