Example usage for org.springframework.data.redis.core TimeoutUtils toSeconds

List of usage examples for org.springframework.data.redis.core TimeoutUtils toSeconds

Introduction

In this page you can find the example usage for org.springframework.data.redis.core TimeoutUtils toSeconds.

Prototype

public static long toSeconds(long timeout, TimeUnit unit) 

Source Link

Document

Converts the given timeout to seconds.

Usage

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

public V leftPop(K key, long timeout, TimeUnit unit) {
    final int tm = (int) TimeoutUtils.toSeconds(timeout, unit);
    return execute(new ValueDeserializingRedisCallback(key) {

        protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
            connection.select(dbIndex);/*w  w w  .  ja v a 2s. c  om*/
            List<byte[]> lPop = connection.bLPop(tm, rawKey);
            return (CollectionUtils.isEmpty(lPop) ? null : lPop.get(1));
        }
    }, true);
}

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

public V rightPop(K key, long timeout, TimeUnit unit) {
    final int tm = (int) TimeoutUtils.toSeconds(timeout, unit);

    return execute(new ValueDeserializingRedisCallback(key) {

        protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
            connection.select(dbIndex);//  w ww .  ja  v  a 2s. c  o  m
            List<byte[]> bRPop = connection.bRPop(tm, rawKey);
            return (CollectionUtils.isEmpty(bRPop) ? null : bRPop.get(1));
        }
    }, true);
}

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);//  w  w  w  .ja  v  a2s.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.mauersu.util.redis.DefaultListOperations.java

public V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) {
    final int tm = (int) TimeoutUtils.toSeconds(timeout, unit);
    final byte[] rawDestKey = rawKey(destinationKey);

    return execute(new ValueDeserializingRedisCallback(sourceKey) {

        protected byte[] inRedis(byte[] rawSourceKey, RedisConnection connection) {
            connection.select(dbIndex);/*from  ww  w. ja va  2s .  c  o m*/
            return connection.bRPopLPush(tm, rawSourceKey, rawDestKey);
        }
    }, true);
}