List of usage examples for org.springframework.data.redis.connection RedisConnection setNX
@Nullable Boolean setNX(byte[] key, byte[] value);
From source file:com.github.deerapple.spring.redis.UserDao.java
@Override public boolean add(final User user) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override//from w w w.jav a 2s . co m public Boolean doInRedis(RedisConnection rc) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] key = serializer.serialize(user.getId()); byte[] name = serializer.serialize(user.getKey()); return rc.setNX(key, name); } }); return result; }
From source file:com.zxy.commons.cache.RedisCache.java
/** * {@inheritDoc}/*from ww w .j a v a 2 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:com.mauersu.util.redis.DefaultValueOperations.java
public Boolean setIfAbsent(K key, V value) { final byte[] rawKey = rawKey(key); final byte[] rawValue = rawValue(value); return execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.select(dbIndex);//from w w w .j a va 2 s . c o m return connection.setNX(rawKey, rawValue); } }, true); }
From source file:com.zxy.commons.cache.RedisUtils.java
/** * Set {@code value} for {@code key}, only if {@code key} does not exist. * <p>// w w w . j av a2s . co m * See http://redis.io/commands/setnx * * @param key must not be {@literal null}. * @param value must not be {@literal null}. * @return Boolean */ public static Boolean setNX(byte[] key, byte[] value) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection redis) throws DataAccessException { return redis.setNX(key, value); } }); }
From source file:com.zxy.commons.cache.RedisHelper.java
/** * Set {@code value} for {@code key}, only if {@code key} does not exist. * <p>//w w w . j a va2 s . c o m * See http://redis.io/commands/setnx * * @param key must not be {@literal null}. * @param value must not be {@literal null}. * @return Boolean */ public Boolean setNX(byte[] key, byte[] value) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection redis) throws DataAccessException { return redis.setNX(key, value); } }); }
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 ww w. j a v a 2s .com 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)); }