diff --git a/persistence-modules/redis/README.md b/persistence-modules/redis/README.md index 71d009241a..75c1c18de4 100644 --- a/persistence-modules/redis/README.md +++ b/persistence-modules/redis/README.md @@ -4,3 +4,4 @@ - [Introduction to Lettuce – the Java Redis Client](https://www.baeldung.com/java-redis-lettuce) - [List All Available Redis Keys](https://www.baeldung.com/redis-list-available-keys) - [Spring Data Redis’s Property-Based Configuration](https://www.baeldung.com/spring-data-redis-properties) +- [Delete Everything in Redis](https://www.baeldung.com/redis-delete-data) diff --git a/persistence-modules/redis/pom.xml b/persistence-modules/redis/pom.xml index c9206e5f92..dab7fc5654 100644 --- a/persistence-modules/redis/pom.xml +++ b/persistence-modules/redis/pom.xml @@ -33,7 +33,8 @@ redis.clients jedis - + 3.3.0 + com.github.kstyrc embedded-redis diff --git a/persistence-modules/redis/src/test/java/com/baeldung/redis/deleteeverything/DeleteEverythingInRedisIntegrationTest.java b/persistence-modules/redis/src/test/java/com/baeldung/redis/deleteeverything/DeleteEverythingInRedisIntegrationTest.java new file mode 100644 index 0000000000..e0376fc6a5 --- /dev/null +++ b/persistence-modules/redis/src/test/java/com/baeldung/redis/deleteeverything/DeleteEverythingInRedisIntegrationTest.java @@ -0,0 +1,91 @@ +package com.baeldung.redis.deleteeverything; + +import org.junit.*; +import redis.clients.jedis.Jedis; +import redis.embedded.RedisServer; + +import java.io.IOException; +import java.net.ServerSocket; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class DeleteEverythingInRedisIntegrationTest { + private Jedis jedis; + private RedisServer redisServer; + private int port; + + @Before + public void setUp() throws IOException { + + // Take an available port + ServerSocket s = new ServerSocket(0); + port = s.getLocalPort(); + s.close(); + + redisServer = new RedisServer(port); + redisServer.start(); + + // Configure JEDIS + jedis = new Jedis("localhost", port); + } + + @After + public void destroy() { + redisServer.stop(); + } + + @Test + public void whenPutDataIntoRedis_thenCanBeFound() { + String key = "key"; + String value = "value"; + + jedis.set(key, value); + String received = jedis.get(key); + + assertEquals(value, received); + } + + @Test + public void whenPutDataIntoRedisAndThenFlush_thenCannotBeFound() { + String key = "key"; + String value = "value"; + + jedis.set(key, value); + + jedis.flushDB(); + + String received = jedis.get(key); + + assertNull(received); + } + + @Test + public void whenPutDataIntoMultipleDatabases_thenFlushAllRemovesAll() { + // add keys in different databases + jedis.select(0); + jedis.set("key1", "value1"); + jedis.select(1); + jedis.set("key2", "value2"); + + // we'll find the correct keys in the correct dbs + jedis.select(0); + assertEquals("value1", jedis.get("key1")); + assertNull(jedis.get("key2")); + + jedis.select(1); + assertEquals("value2", jedis.get("key2")); + assertNull(jedis.get("key1")); + + // then, when we flush + jedis.flushAll(); + + // the keys will have gone + jedis.select(0); + assertNull(jedis.get("key1")); + assertNull(jedis.get("key2")); + jedis.select(1); + assertNull(jedis.get("key1")); + assertNull(jedis.get("key2")); + } +} diff --git a/persistence-modules/spring-data-redis/README.md b/persistence-modules/spring-data-redis/README.md index 175634376b..95cba2c159 100644 --- a/persistence-modules/spring-data-redis/README.md +++ b/persistence-modules/spring-data-redis/README.md @@ -4,7 +4,6 @@ - [Introduction to Spring Data Redis](https://www.baeldung.com/spring-data-redis-tutorial) - [PubSub Messaging with Spring Data Redis](https://www.baeldung.com/spring-data-redis-pub-sub) - [An Introduction to Spring Data Redis Reactive](https://www.baeldung.com/spring-data-redis-reactive) -- [Delete Everything in Redis](https://www.baeldung.com/redis-delete-data) ### Build the Project with Tests Running ``` @@ -15,4 +14,3 @@ mvn clean install ``` mvn test ``` - diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java index fdc279be42..497e1506bd 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java @@ -35,18 +35,6 @@ public class RedisConfig { template.setValueSerializer(new GenericToStringSerializer(Object.class)); return template; } - - @Bean - public LettuceConnectionFactory lettuceConnectionFactory() { - return new LettuceConnectionFactory(); - } - - @Bean(name = "flushRedisTemplate") - public RedisTemplate flushRedisTemplate() { - RedisTemplate template = new RedisTemplate<>(); - template.setConnectionFactory(lettuceConnectionFactory()); - return template; - } @Bean MessageListenerAdapter messageListener() { diff --git a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java deleted file mode 100644 index 1f56cbb25d..0000000000 --- a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.baeldung.spring.data.redis.delete; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.IOException; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.dao.DataAccessException; -import org.springframework.data.redis.connection.RedisConnection; -import org.springframework.data.redis.core.RedisCallback; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ValueOperations; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.DirtiesContext.ClassMode; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.baeldung.spring.data.redis.config.RedisConfig; - -import redis.embedded.RedisServer; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { RedisConfig.class }) -@DirtiesContext(classMode = ClassMode.BEFORE_CLASS) -public class RedisFlushDatabaseIntegrationTest { - - private RedisServer redisServer; - - @Autowired - @Qualifier("flushRedisTemplate") - private RedisTemplate flushRedisTemplate; - - @Before - public void setup() throws IOException { - redisServer = new RedisServer(6390); - redisServer.start(); - } - - @After - public void tearDown() { - redisServer.stop(); - } - - @Test - public void whenFlushDB_thenAllKeysInDatabaseAreCleared() { - - ValueOperations simpleValues = flushRedisTemplate.opsForValue(); - String key = "key"; - String value = "value"; - simpleValues.set(key, value); - assertThat(simpleValues.get(key)).isEqualTo(value); - - flushRedisTemplate.execute(new RedisCallback() { - - @Override - public Void doInRedis(RedisConnection connection) throws DataAccessException { - connection.flushDb(); - return null; - } - }); - - assertThat(simpleValues.get(key)).isNull(); - - } - - @Test - public void whenFlushAll_thenAllKeysInDatabasesAreCleared() { - - ValueOperations simpleValues = flushRedisTemplate.opsForValue(); - String key = "key"; - String value = "value"; - simpleValues.set(key, value); - assertThat(simpleValues.get(key)).isEqualTo(value); - - flushRedisTemplate.execute(new RedisCallback() { - - @Override - public Void doInRedis(RedisConnection connection) throws DataAccessException { - connection.flushAll(); - return null; - } - }); - - assertThat(simpleValues.get(key)).isNull(); - - } -} \ No newline at end of file