@Override public Boolean setNX(byte[] key, byte[] value) { return convertAndReturn(delegate.setNX(key, value), identityConverter); }
private Boolean doLock(String name, RedisConnection connection) { return connection.setNX(createCacheLockKey(name), new byte[0]); }
@Override public Boolean setIfAbsent(K key, V value) { byte[] rawKey = rawKey(key); byte[] rawValue = rawValue(value); return execute(connection -> connection.setNX(rawKey, rawValue), true); }
@Override public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Duration ttl) { Assert.notNull(name, "Name must not be null!"); Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); return execute(name, connection -> { if (isLockingCacheWriter()) { doLock(name, connection); } try { if (connection.setNX(key, value)) { if (shouldExpireWithin(ttl)) { connection.pExpire(key, ttl.toMillis()); } return null; } return connection.get(key); } finally { if (isLockingCacheWriter()) { doUnlock(name, connection); } } }); }
@Override public <K, V> boolean putIfAbsent(final K key, final V value, final Serializer<K> keySerializer, final Serializer<V> valueSerializer) throws IOException { return withConnection(redisConnection -> { final Tuple<byte[],byte[]> kv = serialize(key, value, keySerializer, valueSerializer); boolean set = redisConnection.setNX(kv.getKey(), kv.getValue()); if (ttl != -1L && set) { redisConnection.expire(kv.getKey(), ttl); } return set; }); }
redisConnection.setNX(kv.getKey(), kv.getValue());
public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] keyStr = serializer.serialize(key); byte[] object = serializer.serialize(value); return connection.setNX(keyStr, object); } });
@Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(keyBytes, valueBytes); } });
@Override public Boolean setNX(byte[] key, byte[] value) { return redisConnection.setNX(key, value); }
/** * 将 key的值保存为 value ,当且仅当 key 不存在。 若给定的 key 已经存在,则 SETNX 不做任何动作。 SETNX 是『SET if * Not eXists』(如果不存在,则 SET)的简写。 <br> * 保存成功,返回 true <br> * 保存失败,返回 false */ public static boolean saveNX(String key, String val) { /** 设置成功,返回 1 设置失败,返回 0 **/ return cacheUtils.redisTemplate.execute((RedisCallback<Boolean>) connection -> { return connection.setNX(key.getBytes(), val.getBytes()); }); }
@Override public Boolean setNX(byte[] key, byte[] value) { return convertAndReturn(delegate.setNX(key, value), identityConverter); }
private Boolean doLock(String name, RedisConnection connection) { return connection.setNX(createCacheLockKey(name), new byte[0]); }
@Override public Boolean setNX(byte[] key, byte[] value) { return convertAndReturn(delegate.setNX(key, value), identityConverter); }
private Boolean doLock(String name, RedisConnection connection) { return connection.setNX(createCacheLockKey(name), new byte[0]); }
public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); for (Map.Entry<String, String> entry : map.entrySet()) { byte[] key = serializer.serialize(entry.getKey()); byte[] name = serializer.serialize(entry.getValue()); connection.setNX(key, name); } return true; } }, false, true);
@Override public Boolean setIfAbsent(K key, V value) { byte[] rawKey = rawKey(key); byte[] rawValue = rawValue(value); return execute(connection -> connection.setNX(rawKey, rawValue), true); }
public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); byte[] key = serializer.serialize(k); byte[] name = serializer.serialize(v); return connection.setNX(key, name); } });
@Override public Boolean setIfAbsent(K key, V value) { byte[] rawKey = rawKey(key); byte[] rawValue = rawValue(value); return execute(connection -> connection.setNX(rawKey, rawValue), true); }
@Override public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Duration ttl) { Assert.notNull(name, "Name must not be null!"); Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); return execute(name, connection -> { if (isLockingCacheWriter()) { doLock(name, connection); } try { if (connection.setNX(key, value)) { if (shouldExpireWithin(ttl)) { connection.pExpire(key, ttl.toMillis()); } return null; } return connection.get(key); } finally { if (isLockingCacheWriter()) { doUnlock(name, connection); } } }); }
@Override public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Duration ttl) { Assert.notNull(name, "Name must not be null!"); Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); return execute(name, connection -> { if (isLockingCacheWriter()) { doLock(name, connection); } try { if (connection.setNX(key, value)) { if (shouldExpireWithin(ttl)) { connection.pExpire(key, ttl.toMillis()); } return null; } return connection.get(key); } finally { if (isLockingCacheWriter()) { doUnlock(name, connection); } } }); }