Skip to content

Commit b242d4a

Browse files
DATAREDIS-369 - Avoid resource leak in RedisCache.
We now check the presence of a prefix which allows to identify the keys associated with the cache to either keep track of keys or use the prefix to remove them on clear. This is a small fix intended to be back ported to the Evans (1.4.x) release train. Additional refactoring will be done for the Fowler release. Original Pull Request: spring-projects#122
1 parent 0ea0c4b commit b242d4a

File tree

3 files changed

+187
-17
lines changed

3 files changed

+187
-17
lines changed

src/asciidoc/reference/redis.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ NOTE: By default `RedisCacheManager` will lazily initialize `RedisCache` wheneve
399399

400400
NOTE: By default `RedisCacheManager` will not participate in any ongoing transaction. Use `setTransactionAware` to enable transaction support.
401401

402+
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.
403+
402404
[[redis:future]]
403405
== Roadmap ahead
404406

src/main/java/org/springframework/data/redis/cache/RedisCache.java

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.cache.support.SimpleValueWrapper;
2424
import org.springframework.dao.DataAccessException;
2525
import org.springframework.data.redis.connection.RedisConnection;
26+
import org.springframework.data.redis.connection.ReturnType;
2627
import org.springframework.data.redis.core.RedisCallback;
2728
import org.springframework.data.redis.core.RedisTemplate;
2829
import org.springframework.data.redis.serializer.RedisSerializer;
@@ -39,6 +40,8 @@
3940
@SuppressWarnings("unchecked")
4041
public class RedisCache implements Cache {
4142

43+
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;"
44+
.getBytes();
4245
private static final int PAGE_SIZE = 128;
4346
private final String name;
4447
@SuppressWarnings("rawtypes") private final RedisTemplate template;
@@ -124,12 +127,17 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
124127
connection.multi();
125128

126129
connection.set(keyBytes, valueBytes);
127-
connection.zAdd(setName, 0, keyBytes);
128130

131+
if (shouldTrackKeys()) {
132+
connection.zAdd(setName, 0, keyBytes);
133+
}
129134
if (expiration > 0) {
130135
connection.expire(keyBytes, expiration);
131136
// update the expiration of the set of keys as well
132-
connection.expire(setName, expiration);
137+
138+
if (shouldTrackKeys()) {
139+
connection.expire(setName, expiration);
140+
}
133141
}
134142
connection.exec();
135143

@@ -151,11 +159,18 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
151159
Object resultValue = value;
152160
boolean valueWasSet = connection.setNX(keyBytes, valueBytes);
153161
if (valueWasSet) {
154-
connection.zAdd(setName, 0, keyBytes);
162+
163+
if (shouldTrackKeys()) {
164+
connection.zAdd(setName, 0, keyBytes);
165+
}
155166
if (expiration > 0) {
167+
156168
connection.expire(keyBytes, expiration);
157169
// update the expiration of the set of keys as well
158-
connection.expire(setName, expiration);
170+
171+
if (shouldTrackKeys()) {
172+
connection.expire(setName, expiration);
173+
}
159174
}
160175
} else {
161176
resultValue = deserializeIfNecessary(template.getValueSerializer(), connection.get(keyBytes));
@@ -177,7 +192,9 @@ public void evict(Object key) {
177192
public Object doInRedis(RedisConnection connection) throws DataAccessException {
178193
connection.del(k);
179194
// remove key from set
180-
connection.zRem(setName, k);
195+
if (shouldTrackKeys()) {
196+
connection.zRem(setName, k);
197+
}
181198
return null;
182199
}
183200
}, true);
@@ -198,19 +215,27 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
198215
int offset = 0;
199216
boolean finished = false;
200217

201-
do {
202-
// need to paginate the keys
203-
Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE, (offset + 1) * PAGE_SIZE - 1);
204-
finished = keys.size() < PAGE_SIZE;
205-
offset++;
206-
if (!keys.isEmpty()) {
207-
connection.del(keys.toArray(new byte[keys.size()][]));
208-
}
209-
} while (!finished);
210-
211-
connection.del(setName);
218+
if (shouldTrackKeys()) {
219+
do {
220+
// need to paginate the keys
221+
Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE, (offset + 1) * PAGE_SIZE - 1);
222+
finished = keys.size() < PAGE_SIZE;
223+
offset++;
224+
if (!keys.isEmpty()) {
225+
connection.del(keys.toArray(new byte[keys.size()][]));
226+
}
227+
} while (!finished);
228+
229+
connection.del(setName);
230+
} else {
231+
232+
byte[] wildCard = "*".getBytes();
233+
byte[] prefixToUse = Arrays.copyOf(prefix, prefix.length + wildCard.length);
234+
System.arraycopy(wildCard, 0, prefixToUse, prefix.length, wildCard.length);
235+
236+
connection.eval(REMOVE_KEYS_BY_PATTERN_LUA, ReturnType.INTEGER, 0, prefixToUse);
237+
}
212238
return null;
213-
214239
} finally {
215240
connection.del(cacheLockName);
216241
}
@@ -270,4 +295,15 @@ private Object deserializeIfNecessary(RedisSerializer<byte[]> serializer, byte[]
270295
return value;
271296
}
272297

298+
/**
299+
* A set of keys needs to be maintained in case no prefix is provided. Clear cache depends on either a pattern to
300+
* identify keys to remove or a maintained set of known keys, otherwise the entire db would be flushed potentially
301+
* removing non cache entry values.
302+
*
303+
* @return
304+
*/
305+
private boolean shouldTrackKeys() {
306+
return (prefix == null || prefix.length == 0);
307+
}
308+
273309
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.cache;
17+
18+
import static org.mockito.Matchers.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Mock;
25+
import org.mockito.runners.MockitoJUnitRunner;
26+
import org.springframework.data.redis.connection.RedisConnection;
27+
import org.springframework.data.redis.connection.RedisConnectionFactory;
28+
import org.springframework.data.redis.connection.ReturnType;
29+
import org.springframework.data.redis.core.RedisTemplate;
30+
import org.springframework.data.redis.serializer.RedisSerializer;
31+
32+
/**
33+
* @author Christoph Strobl
34+
*/
35+
@SuppressWarnings("rawtypes")
36+
@RunWith(MockitoJUnitRunner.class)
37+
public class RedisCacheUnitTests {
38+
39+
private static final String CACHE_NAME = "foo";
40+
private static final String PREFIX = "prefix:";
41+
private static final byte[] PREFIX_BYTES = "prefix:".getBytes();
42+
private static final byte[] KNOWN_KEYS_SET_NAME_BYTES = (CACHE_NAME + "~keys").getBytes();
43+
44+
private static final String KEY = "key";
45+
private static final byte[] KEY_BYTES = KEY.getBytes();
46+
private static final byte[] KEY_WITH_PREFIX_BYTES = (PREFIX + KEY).getBytes();
47+
48+
private static final String VALUE = "value";
49+
private static final byte[] VALUE_BYTES = VALUE.getBytes();
50+
51+
private static final byte[] NO_PREFIX_BYTES = new byte[] {};
52+
private static final long EXPIRATION = 1000;
53+
54+
RedisTemplate<?, ?> templateSpy;
55+
@Mock RedisSerializer keySerializerMock;
56+
@Mock RedisSerializer valueSerializerMock;
57+
@Mock RedisConnectionFactory connectionFactoryMock;
58+
@Mock RedisConnection connectionMock;
59+
60+
RedisCache cache;
61+
62+
@SuppressWarnings("unchecked")
63+
@Before
64+
public void setUp() {
65+
66+
RedisTemplate template = new RedisTemplate();
67+
template.setConnectionFactory(connectionFactoryMock);
68+
template.setKeySerializer(keySerializerMock);
69+
template.setValueSerializer(valueSerializerMock);
70+
template.afterPropertiesSet();
71+
72+
templateSpy = spy(template);
73+
74+
when(connectionFactoryMock.getConnection()).thenReturn(connectionMock);
75+
76+
when(keySerializerMock.serialize(any(byte[].class))).thenReturn(KEY_BYTES);
77+
when(valueSerializerMock.serialize(any(byte[].class))).thenReturn(VALUE_BYTES);
78+
}
79+
80+
/**
81+
* @see DATAREDIS-369
82+
*/
83+
@Test
84+
public void putShouldNotKeepTrackOfKnownKeysWhenPrefixIsSet() {
85+
86+
cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION);
87+
cache.put(KEY, VALUE);
88+
89+
verify(connectionMock, times(1)).set(eq(KEY_WITH_PREFIX_BYTES), eq(VALUE_BYTES));
90+
verify(connectionMock, times(1)).expire(eq(KEY_WITH_PREFIX_BYTES), eq(EXPIRATION));
91+
verify(connectionMock, never()).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), any(byte[].class));
92+
}
93+
94+
/**
95+
* @see DATAREDIS-369
96+
*/
97+
@Test
98+
public void putShouldKeepTrackOfKnownKeysWhenNoPrefixIsSet() {
99+
100+
cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION);
101+
cache.put(KEY, VALUE);
102+
103+
verify(connectionMock, times(1)).set(eq(KEY_BYTES), eq(VALUE_BYTES));
104+
verify(connectionMock, times(1)).expire(eq(KEY_BYTES), eq(EXPIRATION));
105+
verify(connectionMock, times(1)).zAdd(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0D), eq(KEY_BYTES));
106+
}
107+
108+
/**
109+
* @see DATAREDIS-369
110+
*/
111+
@Test
112+
public void clearShouldRemoveKeysUsingKnownKeysWhenNoPrefixIsSet() {
113+
114+
cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, EXPIRATION);
115+
cache.clear();
116+
117+
verify(connectionMock, times(1)).zRange(eq(KNOWN_KEYS_SET_NAME_BYTES), eq(0L), eq(127L));
118+
}
119+
120+
/**
121+
* @see DATAREDIS-369
122+
*/
123+
@Test
124+
public void clearShouldCallLuaScritpToRemoveKeysWhenPrefixIsSet() {
125+
126+
cache = new RedisCache(CACHE_NAME, PREFIX_BYTES, templateSpy, EXPIRATION);
127+
cache.clear();
128+
129+
verify(connectionMock, times(1)).eval(any(byte[].class), eq(ReturnType.INTEGER), eq(0),
130+
eq((PREFIX + "*").getBytes()));
131+
}
132+
}

0 commit comments

Comments
 (0)