diff --git a/src/asciidoc/reference/redis.adoc b/src/asciidoc/reference/redis.adoc index e64b9f943..796ea1b8e 100644 --- a/src/asciidoc/reference/redis.adoc +++ b/src/asciidoc/reference/redis.adoc @@ -399,6 +399,8 @@ NOTE: By default `RedisCacheManager` will lazily initialize `RedisCache` wheneve NOTE: By default `RedisCacheManager` will not participate in any ongoing transaction. Use `setTransactionAware` to enable transaction support. +NOTE: By default `RedisCacheManager` does not prefix keys for cache regions, which can lead to an unexpected growth of a `ZSET` used to maintain known keys. It's highly recommended to enable the usage of prefixes in order to avoid this unexpected growth and potential key clashes using more than one cache region. + [[redis:future]] == Roadmap ahead diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCache.java b/src/main/java/org/springframework/data/redis/cache/RedisCache.java index 424e7e581..131af40ee 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -23,6 +23,7 @@ import org.springframework.cache.Cache; import org.springframework.cache.support.SimpleValueWrapper; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.ReturnType; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; @@ -39,6 +40,8 @@ import org.springframework.util.Assert; @SuppressWarnings("unchecked") public class RedisCache implements Cache { + private static final byte[] REMOVE_KEYS_BY_PATTERN_LUA = "local keys = redis.call('KEYS', ARGV[1]); local keysCount = table.getn(keys); if(keysCount > 0) then for _, key in ipairs(keys) do redis.call('del', key); end; end; return keysCount;" + .getBytes(); private static final int PAGE_SIZE = 128; private final String name; @SuppressWarnings("rawtypes") private final RedisTemplate template; @@ -124,12 +127,17 @@ public class RedisCache implements Cache { connection.multi(); connection.set(keyBytes, valueBytes); - connection.zAdd(setName, 0, keyBytes); + if (shouldTrackKeys()) { + 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); + + if (shouldTrackKeys()) { + connection.expire(setName, expiration); + } } connection.exec(); @@ -151,11 +159,18 @@ public class RedisCache implements Cache { Object resultValue = value; boolean valueWasSet = connection.setNX(keyBytes, valueBytes); if (valueWasSet) { - connection.zAdd(setName, 0, keyBytes); + + if (shouldTrackKeys()) { + 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); + + if (shouldTrackKeys()) { + connection.expire(setName, expiration); + } } } else { resultValue = deserializeIfNecessary(template.getValueSerializer(), connection.get(keyBytes)); @@ -177,7 +192,9 @@ public class RedisCache implements Cache { public Object doInRedis(RedisConnection connection) throws DataAccessException { connection.del(k); // remove key from set - connection.zRem(setName, k); + if (shouldTrackKeys()) { + connection.zRem(setName, k); + } return null; } }, true); @@ -198,19 +215,27 @@ public class RedisCache implements Cache { int offset = 0; boolean finished = false; - do { - // need to paginate the keys - Set 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); + if (shouldTrackKeys()) { + do { + // need to paginate the keys + Set 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); + connection.del(setName); + } else { + + byte[] wildCard = "*".getBytes(); + byte[] prefixToUse = Arrays.copyOf(prefix, prefix.length + wildCard.length); + System.arraycopy(wildCard, 0, prefixToUse, prefix.length, wildCard.length); + + connection.eval(REMOVE_KEYS_BY_PATTERN_LUA, ReturnType.INTEGER, 0, prefixToUse); + } return null; - } finally { connection.del(cacheLockName); } @@ -270,4 +295,15 @@ public class RedisCache implements Cache { return value; } + /** + * A set of keys needs to be maintained in case no prefix is provided. Clear cache depends on either a pattern to + * identify keys to remove or a maintained set of known keys, otherwise the entire db would be flushed potentially + * removing non cache entry values. + * + * @return + */ + private boolean shouldTrackKeys() { + return (prefix == null || prefix.length == 0); + } + } diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java new file mode 100644 index 000000000..8919e648f --- /dev/null +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java @@ -0,0 +1,132 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.cache; + +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.ReturnType; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.RedisSerializer; + +/** + * @author Christoph Strobl + */ +@SuppressWarnings("rawtypes") +@RunWith(MockitoJUnitRunner.class) +public class RedisCacheUnitTests { + + private static final String CACHE_NAME = "foo"; + private static final String PREFIX = "prefix:"; + private static final byte[] PREFIX_BYTES = "prefix:".getBytes(); + private static final byte[] KNOWN_KEYS_SET_NAME_BYTES = (CACHE_NAME + "~keys").getBytes(); + + private static final String KEY = "key"; + private static final byte[] KEY_BYTES = KEY.getBytes(); + private static final byte[] KEY_WITH_PREFIX_BYTES = (PREFIX + KEY).getBytes(); + + private static final String VALUE = "value"; + private static final byte[] VALUE_BYTES = VALUE.getBytes(); + + private static final byte[] NO_PREFIX_BYTES = new byte[] {}; + private static final long EXPIRATION = 1000; + + RedisTemplate templateSpy; + @Mock RedisSerializer keySerializerMock; + @Mock RedisSerializer valueSerializerMock; + @Mock RedisConnectionFactory connectionFactoryMock; + @Mock RedisConnection connectionMock; + + RedisCache cache; + + @SuppressWarnings("unchecked") + @Before + public void setUp() { + + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(connectionFactoryMock); + template.setKeySerializer(keySerializerMock); + template.setValueSerializer(valueSerializerMock); + template.afterPropertiesSet(); + + templateSpy = spy(template); + + when(connectionFactoryMock.getConnection()).thenReturn(connectionMock); + + when(keySerializerMock.serialize(any(byte[].class))).thenReturn(KEY_BYTES); + when(valueSerializerMock.serialize(any(byte[].class))).thenReturn(VALUE_BYTES); + } + + /** + * @see DATAREDIS-369 + */ + @Test + public void putShouldNotKeepTrackOfKnownKeysWhenPrefixIsSet() { + + cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION); + cache.put(KEY, VALUE); + + verify(connectionMock, times(1)).set(eq(KEY_WITH_PREFIX_BYTES), eq(VALUE_BYTES)); + verify(connectionMock, times(1)).expire(eq(KEY_WITH_PREFIX_BYTES), eq(EXPIRATION)); + verify(connectionMock, never()).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), any(byte[].class)); + } + + /** + * @see DATAREDIS-369 + */ + @Test + public void putShouldKeepTrackOfKnownKeysWhenNoPrefixIsSet() { + + cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION); + cache.put(KEY, VALUE); + + verify(connectionMock, times(1)).set(eq(KEY_BYTES), eq(VALUE_BYTES)); + verify(connectionMock, times(1)).expire(eq(KEY_BYTES), eq(EXPIRATION)); + verify(connectionMock, times(1)).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), eq(KEY_BYTES)); + } + + /** + * @see DATAREDIS-369 + */ + @Test + public void clearShouldRemoveKeysUsingKnownKeysWhenNoPrefixIsSet() { + + cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION); + cache.clear(); + + verify(connectionMock, times(1)).zRange(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0L), eq(127L)); + } + + /** + * @see DATAREDIS-369 + */ + @Test + public void clearShouldCallLuaScritpToRemoveKeysWhenPrefixIsSet() { + + cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION); + cache.clear(); + + verify(connectionMock, times(1)).eval(any(byte[].class), eq(ReturnType.INTEGER), eq(0), + eq((PREFIX + "*").getBytes())); + } +}