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

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

Introduction

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

Prototype

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

Source Link

Document

Set time to live for given key in seconds.

Usage

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

@Override
public void put(Object key, Object value) {
    redisTemplate.execute(new RedisCallback<Long>() {
        public Long doInRedis(RedisConnection connection) throws DataAccessException {
            byte[] keyb = SerializationUtils.serialize(key);
            byte[] valueb = SerializationUtils.serialize(value);
            connection.set(keyb, valueb);
            if (expires > 0) {
                connection.expire(keyb, expires);
            }//from ww w.ja  va  2s  .  c o m
            return 1L;
        }
    });

}

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

/**
 * {@inheritDoc}/*w w w  .j a v  a2 s .  c om*/
 */
@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
    return redisTemplate.execute(new RedisCallback<ValueWrapper>() {
        public ValueWrapper doInRedis(RedisConnection connection) throws DataAccessException {
            byte[] keyb = SerializationUtils.serialize(key);
            byte[] valueb = SerializationUtils.serialize(value);
            connection.setNX(keyb, valueb);
            if (expires > 0) {
                connection.expire(keyb, expires);
            }
            return new SimpleValueWrapper(value);
        }
    });
}

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

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

/**
 * Expire key for seconds/* w  w w  . ja  v  a2  s .  c  o  m*/
 * <p>
 * See http://redis.io/commands/strlen
 * 
 * @param key must not be {@literal null}.
 * @param seconds seconds time
 * @return Long
 */
public static Boolean expire(byte[] key, int seconds) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.expire(key, seconds);
        }
    });
}

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

/**
 * Set time to live for given {@code key} in seconds.
 * <p>//from  w  ww. j  a v a2s . c  o  m
 * See http://redis.io/commands/expire
 * 
 * @param key key
 * @param seconds seconds
 * @return Boolean
 */
public static Boolean expire(byte[] key, long seconds) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.expire(key, seconds);
        }
    });
}

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

/**
 * Expire key for seconds/*from   w  w  w . j ava2 s.c o m*/
 * <p>
 * See http://redis.io/commands/strlen
 * 
 * @param key must not be {@literal null}.
 * @param seconds seconds time
 * @return Long
 */
public Boolean expire(byte[] key, int seconds) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.expire(key, seconds);
        }
    });
}

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

/**
 * Set time to live for given {@code key} in seconds.
 * <p>//from  ww w.  j  a v a2s. c om
 * See http://redis.io/commands/expire
 * 
 * @param key key
 * @param seconds seconds
 * @return Boolean
 */
public Boolean expire(byte[] key, long seconds) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.expire(key, seconds);
        }
    });
}

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 w  w.  j  av a2 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);//from   w w  w  .j a v a  2s  .co  m

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