@Override public void remove(String name, byte[] key) { Assert.notNull(name, "Name must not be null!"); Assert.notNull(key, "Key must not be null!"); execute(name, connection -> connection.del(key)); }
@Override public Long del(byte[]... keys) { return convertAndReturn(delegate.del(keys), identityConverter); }
private Long doUnlock(String name, RedisConnection connection) { return connection.del(createCacheLockKey(name)); }
/** * Removes all indexes. */ public void removeAllIndexes(String keyspace) { Set<byte[]> potentialIndex = connection.keys(toBytes(keyspace + ":*")); if (!potentialIndex.isEmpty()) { connection.del(potentialIndex.toArray(new byte[potentialIndex.size()][])); } }
@Override public void onComponentRemoved(final String componentId) throws IOException { withConnection(redisConnection -> { final byte[] key = getComponentKey(componentId).getBytes(StandardCharsets.UTF_8); redisConnection.del(key); return true; }); }
@Override public Boolean delete(K key) { byte[] rawKey = rawKey(key); Long result = execute(connection -> connection.del(rawKey), true); return result != null && result.intValue() == 1; }
@Override public Long delete(Collection<K> keys) { if (CollectionUtils.isEmpty(keys)) { return 0L; } byte[][] rawKeys = rawKeys(keys); return execute(connection -> connection.del(rawKeys), true); }
@Override public <K> boolean remove(final K key, final Serializer<K> keySerializer) throws IOException { return withConnection(redisConnection -> { final byte[] k = serialize(key, keySerializer); final long numRemoved = redisConnection.del(k); return numRemoved > 0; }); }
public void removeRefreshToken(String tokenValue) { byte[] refreshKey = serializeKey(REFRESH + tokenValue); byte[] refreshAuthKey = serializeKey(REFRESH_AUTH + tokenValue); byte[] refresh2AccessKey = serializeKey(REFRESH_TO_ACCESS + tokenValue); byte[] access2RefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue); RedisConnection conn = getConnection(); try { conn.openPipeline(); conn.del(refreshKey); conn.del(refreshAuthKey); conn.del(refresh2AccessKey); conn.del(access2RefreshKey); conn.closePipeline(); } finally { conn.close(); } }
@Override public void clean(String name, byte[] pattern) { Assert.notNull(name, "Name must not be null!"); Assert.notNull(pattern, "Pattern must not be null!"); execute(name, connection -> { boolean wasLocked = false; try { if (isLockingCacheWriter()) { doLock(name, connection); wasLocked = true; } byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet()) .toArray(new byte[0][]); if (keys.length > 0) { connection.del(keys); } } finally { if (wasLocked && isLockingCacheWriter()) { doUnlock(name, connection); } } return "OK"; }); }
@Override public long removeByPattern(final String regex) throws IOException { return withConnection(redisConnection -> { long deletedCount = 0; final List<byte[]> batchKeys = new ArrayList<>(); // delete keys in batches of 1000 using the cursor final Cursor<byte[]> cursor = redisConnection.scan(ScanOptions.scanOptions().count(100).match(regex).build()); while (cursor.hasNext()) { batchKeys.add(cursor.next()); if (batchKeys.size() == 1000) { deletedCount += redisConnection.del(getKeys(batchKeys)); batchKeys.clear(); } } // delete any left-over keys if some were added to the batch but never reached 1000 if (batchKeys.size() > 0) { deletedCount += redisConnection.del(getKeys(batchKeys)); batchKeys.clear(); } return deletedCount; }); }
@Override public void deleteAllOf(String keyspace) { redisOps.execute((RedisCallback<Void>) connection -> { connection.del(toBytes(keyspace)); new IndexWriter(connection, converter).removeAllIndexes(asString(keyspace)); return null; }); }
conn.get(accessKey); conn.get(authKey); conn.del(accessKey); conn.del(accessToRefreshKey); conn.del(authKey); List<Object> results = conn.closePipeline(); byte[] access = (byte[]) results.get(0); byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId()); conn.openPipeline(); conn.del(authToAccessKey); conn.sRem(unameKey, access); conn.sRem(clientId, access); conn.del(serialize(ACCESS + key)); conn.closePipeline();
private void removeAccessTokenUsingRefreshToken(String refreshToken) { byte[] key = serializeKey(REFRESH_TO_ACCESS + refreshToken); List<Object> results = null; RedisConnection conn = getConnection(); try { conn.openPipeline(); conn.get(key); conn.del(key); results = conn.closePipeline(); } finally { conn.close(); } if (results == null) { return; } byte[] bytes = (byte[]) results.get(0); String accessToken = deserializeString(bytes); if (accessToken != null) { removeAccessToken(accessToken); } }
/** * Removes a key from all available indexes. * * @param key must not be {@literal null}. */ public void removeKeyFromIndexes(String keyspace, Object key) { Assert.notNull(key, "Key must not be null!"); byte[] binKey = toBytes(key); byte[] indexHelperKey = ByteUtils.concatAll(toBytes(keyspace + ":"), binKey, toBytes(":idx")); for (byte[] indexKey : connection.sMembers(indexHelperKey)) { DataType type = connection.type(indexKey); if (DataType.ZSET.equals(type)) { connection.zRem(indexKey, binKey); } else { connection.sRem(indexKey, binKey); } } connection.del(indexHelperKey); }
@Override public <T> T delete(Object id, String keyspace, Class<T> type) { byte[] binId = toBytes(id); byte[] binKeyspace = toBytes(keyspace); T o = get(id, keyspace, type); if (o != null) { byte[] keyToDelete = createKey(asString(keyspace), asString(id)); redisOps.execute((RedisCallback<Void>) connection -> { connection.del(keyToDelete); connection.sRem(binKeyspace, binId); new IndexWriter(connection, converter).removeKeyFromIndexes(asString(keyspace), binId); return null; }); } return o; }
@Override public void onMessage(Message message, @Nullable byte[] pattern) { if (!isKeyExpirationMessage(message)) { return; } byte[] key = message.getBody(); byte[] phantomKey = ByteUtils.concat(key, converter.getConversionService().convert(KeyspaceIdentifier.PHANTOM_SUFFIX, byte[].class)); Map<byte[], byte[]> hash = ops.execute((RedisCallback<Map<byte[], byte[]>>) connection -> { Map<byte[], byte[]> hash1 = connection.hGetAll(phantomKey); if (!CollectionUtils.isEmpty(hash1)) { connection.del(phantomKey); } return hash1; }); Object value = converter.read(Object.class, new RedisData(hash)); String channel = !ObjectUtils.isEmpty(message.getChannel()) ? converter.getConversionService().convert(message.getChannel(), String.class) : null; RedisKeyExpiredEvent event = new RedisKeyExpiredEvent(channel, key, value); ops.execute((RedisCallback<Void>) connection -> { connection.sRem(converter.getConversionService().convert(event.getKeyspace(), byte[].class), event.getId()); new IndexWriter(connection, converter).removeKeyFromIndexes(event.getKeyspace(), event.getId()); return null; }); publishEvent(event); }
@SuppressWarnings("unchecked") @Test @RedisAvailable public void testMGetCommand() { RedisConnection connection = this.getConnectionFactoryForTest().getConnection(); byte[] value1 = "bar1".getBytes(); byte[] value2 = "bar2".getBytes(); connection.set("foo1".getBytes(), value1); connection.set("foo2".getBytes(), value2); this.mgetCommandChannel.send(MessageBuilder.withPayload(new String[] { "foo1", "foo2" }).build()); Message<?> receive = this.replyChannel.receive(1000); assertNotNull(receive); assertThat((List<byte[]>) receive.getPayload(), Matchers.contains(value1, value2)); connection.del("foo1".getBytes(), "foo2".getBytes()); }
@Before public void setup() { RedisConnectionFactory jcf = getConnectionFactoryForTest(); jcf.getConnection().del(this.queueName); this.inboundGateway.start(); }