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

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

Introduction

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

Prototype

@Nullable
Boolean set(byte[] key, byte[] value);

Source Link

Document

Set value for key .

Usage

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

public void set(K key, V value) {
    final byte[] rawValue = rawValue(value);
    execute(new ValueDeserializingRedisCallback(key) {

        protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
            connection.select(dbIndex);//from   www  .  ja  v a2  s .co  m
            connection.set(rawKey, rawValue);
            return null;
        }
    }, true);
}

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);
            }/*w  ww  . j  a v  a2  s  . c  om*/
            return 1L;
        }
    });

}

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

@SuppressWarnings("unchecked")
@Override//  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//from w  w  w .j  a va2 s .  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:grails.plugin.cache.redis.GrailsRedisCache.java

@SuppressWarnings("unchecked")
@Override// www. j ava 2  s .co m
public void clear() {
    // need to del each key individually
    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            // another clear is on-going
            if (connection.exists(cacheLockName)) {
                return null;
            }

            try {
                connection.set(cacheLockName, cacheLockName);

                int offset = 0;
                boolean finished = false;

                do {
                    // need to paginate the keys
                    Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE,
                            (offset + 1) * PAGE_SIZE - 1);
                    finished = keys.size() < PAGE_SIZE;
                    offset++;
                    if (!keys.isEmpty()) {
                        connection.del(keys.toArray(new byte[keys.size()][]));
                    }
                } while (!finished);

                connection.del(setName);
                return null;

            } finally {
                connection.del(cacheLockName);
            }
        }
    }, true);
}

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

/**
 * Set {@code value} for {@code key}./*  w  w  w . j  a  va  2  s  . co  m*/
 * <p>
 * See http://redis.io/commands/set
 * 
 * @param key must not be {@literal null}.
 * @param value must not be {@literal null}.
 */
public static void set(byte[] key, byte[] value) {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            redis.set(key, value);
            return null;
        }
    });
}

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

/**
 * Set {@code value} for {@code key}.//from   w w w .  j  a  v  a2s .c o m
 * <p>
 * See http://redis.io/commands/set
 * 
 * @param <T> value class type
 * @param key must not be {@literal null}.
 * @param value must not be {@literal null}.
 */
public static <T> void set4Json(byte[] key, T value) {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            byte[] jsonValue = JsonUtils.toJson(value).getBytes();
            redis.set(key, jsonValue);
            return null;
        }
    });
}

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

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

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

/**
 * Set {@code value} for {@code key}./*from w ww . j  ava 2 s.  c  o m*/
 * <p>
 * See http://redis.io/commands/set
 * 
 * @param <T> value class type
 * @param key must not be {@literal null}.
 * @param value must not be {@literal null}.
 */
public <T> void set4Json(byte[] key, T value) {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            byte[] jsonValue = JsonUtils.toJson(value).getBytes();
            redis.set(key, jsonValue);
            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);//from  w  w  w.  ja  va  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);
}