Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): Redis Improvements #26932 #26974

Merged
merged 9 commits into from
Feb 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,13 @@ public class LettuceCacheTest {
public static void startup() throws Exception {
cache = new RedisCache();
}


/**
* all key values that are put into redis are prefixed by a random key.
* When a flushall is called, this key is cycled, which basically invalidates
* all cached entries. The prefix key is stored in redis itself so multiple
* servers in the cluster
*/
@Test
public void test_prefix_key_cycling() {

if (RedisClientFactory.getClient("cache").ping()) {


final String prefix1 = cache.loadPrefix();
assert (prefix1 != null && prefix1.length() > 3);

cache.cycleKey();

final String prefix2 = cache.loadPrefix();
assert (prefix2 != null);

// keys don't match after they have been cycled
assert (!prefix1.equals(prefix2));

// getting the key again and the keys match
final String prefix3 = cache.loadPrefix();
assert (prefix2.equals(prefix3));
}
}

@Test
public void test_cache_key_generation() {

if (RedisClientFactory.getClient("cache").ping()) {
final String prefix = cache.loadPrefix();
final String prefix = cache.REDIS_PREFIX_KEY;
assert (prefix != null && prefix.length() > 3);
String group = "group";
String key = "key";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static io.lettuce.core.ScriptOutputType.STATUS;

/**
* Master replica implementation of redis cache. It works as a replicator when there is more than 1 URIs as part of the
* {@code REDIS_LETTUCECLIENT_URLS} config. This implementation wraps keys, members and channels by prefixing them with
Expand Down Expand Up @@ -152,7 +154,7 @@ protected List<RedisURI> createRedisConnection() {
.map(RedisURI::create)
.collect(Collectors.toList());
}
final String redisSessionEnabled = envVarService.getenv().getOrDefault("TOMCAT_REDIS_SESSION_ENABLED", "false");
final String redisSessionEnabled = envVarService.getenv().getOrDefault("TOMCAT_REDIS_SESSION_ENABLED", "false");
if (Boolean.parseBoolean(redisSessionEnabled)) {
final RedisURI redisURI = RedisURI.builder()
.withHost(envVarService.getenv().getOrDefault("TOMCAT_REDIS_SESSION_HOST", "localhost"))
Expand All @@ -164,7 +166,7 @@ protected List<RedisURI> createRedisConnection() {
.build();
return List.of(redisURI);
}
return List.of(RedisURI.create("redis://password@oboxturbo"));
return List.of(RedisURI.create("redis://localhost"));
}

/**
Expand Down Expand Up @@ -900,6 +902,19 @@ public Collection<K> getChannels() {
return channelReferenceMap.keySet().stream().map(this::unwrapKey).collect(Collectors.toList());
}

@Override
public void deleteFromPattern(final String pattern) {

try (StatefulRedisConnection<String,V> conn = this.getConn()) {

if (this.isOpen(conn)) {

conn.async().eval("return redis.call('del', unpack(redis.call('keys', '" + pattern + "')))",
STATUS, new String[0]);
}
}
}

@Override
public Future<Long> publishMessage (final V message, final K channelIn) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public long getIncrement (final Object key) {
return -1;
}

@Override
public void deleteFromPattern(String pattern) {

}


@Override
public void scanKeys(String matchesPattern, int keyBatchingSize, Consumer keyConsumer) {
Expand Down