Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B38FGKKam5SCGgwgduVAh3
96 lines
4.3 KiB
Java
96 lines
4.3 KiB
Java
package com.ankurm.redis;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import java.util.List;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
/**
|
|
* StringRedisTemplate, and why two templates over one Redis do not share keys.
|
|
* Writes docs/output/04-string-template.txt and 05-two-templates-two-keyspaces.txt.
|
|
*/
|
|
@RedisTest
|
|
class StringTemplateTest {
|
|
|
|
@Autowired
|
|
StringRedisTemplate stringTemplate;
|
|
|
|
@Autowired
|
|
RedisTemplate<Object, Object> redisTemplate;
|
|
|
|
@BeforeEach
|
|
void clean() {
|
|
LocalRedis.flush();
|
|
}
|
|
|
|
@Test
|
|
void stringTemplateWritesWhatRedisCliCanRead() {
|
|
try (Transcript t = new Transcript("04-string-template.txt",
|
|
"StringRedisTemplate: every key and value is UTF-8 text")) {
|
|
|
|
t.section("Which serializers is it using?");
|
|
t.line("key serializer: %s", stringTemplate.getKeySerializer().getClass().getSimpleName());
|
|
t.line("value serializer: %s", stringTemplate.getValueSerializer().getClass().getSimpleName());
|
|
t.line("hash key serializer: %s", stringTemplate.getHashKeySerializer().getClass().getSimpleName());
|
|
t.line("hash value serializer: %s", stringTemplate.getHashValueSerializer().getClass().getSimpleName());
|
|
|
|
t.section("A string value");
|
|
stringTemplate.opsForValue().set("user:1", "Ankur");
|
|
t.line("stringTemplate.opsForValue().set(\"user:1\", \"Ankur\")");
|
|
t.cli("--no-raw", "GET", "user:1");
|
|
|
|
t.section("A counter");
|
|
stringTemplate.opsForValue().increment("hits");
|
|
stringTemplate.opsForValue().increment("hits");
|
|
t.line("stringTemplate.opsForValue().increment(\"hits\") twice = %s", stringTemplate.opsForValue().get("hits"));
|
|
t.cli("--no-raw", "GET", "hits");
|
|
assertThat(stringTemplate.opsForValue().get("hits")).isEqualTo("2");
|
|
|
|
t.section("A hash");
|
|
stringTemplate.opsForHash().put("user:1:profile", "city", "Pune");
|
|
stringTemplate.opsForHash().put("user:1:profile", "editor", "vim");
|
|
t.line("stringTemplate.opsForHash().put(\"user:1:profile\", \"city\", \"Pune\") and (\"editor\", \"vim\")");
|
|
t.cli("--no-raw", "HGETALL", "user:1:profile");
|
|
|
|
t.section("A list, a set and a sorted set");
|
|
stringTemplate.opsForList().rightPushAll("queue", "a", "b", "c");
|
|
stringTemplate.opsForSet().add("tags", "java", "spring");
|
|
stringTemplate.opsForZSet().add("scores", "ankur", 42);
|
|
t.cli("--no-raw", "LRANGE", "queue", "0", "-1");
|
|
t.cliSorted("SMEMBERS", "tags");
|
|
t.cli("--no-raw", "ZRANGE", "scores", "0", "-1", "WITHSCORES");
|
|
t.cliSorted("KEYS", "*");
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void twoTemplatesOverOneRedisDoNotSeeEachOthersKeys() {
|
|
try (Transcript t = new Transcript("05-two-templates-two-keyspaces.txt",
|
|
"Same key name, two templates, two different keys")) {
|
|
|
|
t.section("Write user:1 with StringRedisTemplate");
|
|
stringTemplate.opsForValue().set("user:1", "from-string-template");
|
|
t.line("stringTemplate.opsForValue().get(\"user:1\") = %s", stringTemplate.opsForValue().get("user:1"));
|
|
Object viaDefault = redisTemplate.opsForValue().get("user:1");
|
|
t.line("redisTemplate.opsForValue().get(\"user:1\") = %s", viaDefault);
|
|
assertThat(viaDefault).isNull();
|
|
|
|
t.section("Write user:1 with the default RedisTemplate");
|
|
redisTemplate.opsForValue().set("user:1", "from-default-template");
|
|
t.line("stringTemplate.opsForValue().get(\"user:1\") = %s", stringTemplate.opsForValue().get("user:1"));
|
|
t.line("redisTemplate.opsForValue().get(\"user:1\") = %s", redisTemplate.opsForValue().get("user:1"));
|
|
assertThat(stringTemplate.opsForValue().get("user:1")).isEqualTo("from-string-template");
|
|
|
|
t.section("What Redis holds");
|
|
List<String> size = t.cli("--no-raw", "DBSIZE");
|
|
assertThat(size.get(0)).isEqualTo("(integer) 2");
|
|
t.cli("--no-raw", "GET", "user:1");
|
|
}
|
|
}
|
|
}
|