Delete everything in Redis (#8823)
This commit is contained in:
parent
bcef7cb251
commit
84c78797f5
|
@ -1,13 +1,11 @@
|
||||||
package com.baeldung.spring.data.redis.config;
|
package com.baeldung.spring.data.redis.config;
|
||||||
|
|
||||||
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
|
||||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
|
||||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.listener.ChannelTopic;
|
import org.springframework.data.redis.listener.ChannelTopic;
|
||||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||||
|
@ -15,6 +13,10 @@ import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
|
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
|
||||||
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
||||||
|
|
||||||
|
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
||||||
|
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||||
|
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("com.baeldung.spring.data.redis")
|
@ComponentScan("com.baeldung.spring.data.redis")
|
||||||
@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo")
|
@EnableRedisRepositories(basePackages = "com.baeldung.spring.data.redis.repo")
|
||||||
|
@ -34,6 +36,18 @@ public class RedisConfig {
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LettuceConnectionFactory lettuceConnectionFactory() {
|
||||||
|
return new LettuceConnectionFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "flushRedisTemplate")
|
||||||
|
public RedisTemplate<String, String> flushRedisTemplate() {
|
||||||
|
RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||||
|
template.setConnectionFactory(lettuceConnectionFactory());
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
MessageListenerAdapter messageListener() {
|
MessageListenerAdapter messageListener() {
|
||||||
return new MessageListenerAdapter(new RedisMessageSubscriber());
|
return new MessageListenerAdapter(new RedisMessageSubscriber());
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
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<String, String> 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<String, String> simpleValues = flushRedisTemplate.opsForValue();
|
||||||
|
String key = "key";
|
||||||
|
String value = "value";
|
||||||
|
simpleValues.set(key, value);
|
||||||
|
assertThat(simpleValues.get(key)).isEqualTo(value);
|
||||||
|
|
||||||
|
flushRedisTemplate.execute(new RedisCallback<Void>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void doInRedis(RedisConnection connection) throws DataAccessException {
|
||||||
|
connection.flushDb();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThat(simpleValues.get(key)).isNull();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFlushAll_thenAllKeysInDatabasesAreCleared() {
|
||||||
|
|
||||||
|
ValueOperations<String, String> simpleValues = flushRedisTemplate.opsForValue();
|
||||||
|
String key = "key";
|
||||||
|
String value = "value";
|
||||||
|
simpleValues.set(key, value);
|
||||||
|
assertThat(simpleValues.get(key)).isEqualTo(value);
|
||||||
|
|
||||||
|
flushRedisTemplate.execute(new RedisCallback<Void>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void doInRedis(RedisConnection connection) throws DataAccessException {
|
||||||
|
connection.flushAll();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThat(simpleValues.get(key)).isNull();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue