Skip to content

Commit 23c7c7d

Browse files
committed
New redis module
We have created this module to work with Redis Java libraries Jedis tested so far
1 parent d9223b2 commit 23c7c7d

3 files changed

Lines changed: 267 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<module>xml</module>
7272

7373
<module>lombok</module>
74+
<module>redis</module>
7475
</modules>
7576

7677
</project>

‎redis/pom.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>redis</artifactId>
9+
<version>0.1-SNAPSHOT</version>
10+
11+
<name>redis</name>
12+
<url>http://maven.apache.org</url>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>redis.clients</groupId>
17+
<artifactId>jedis</artifactId>
18+
<version>2.8.1</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>com.github.kstyrc</groupId>
23+
<artifactId>embedded-redis</artifactId>
24+
<version>0.6</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>${junit.version}</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<properties>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
38+
<!-- testing -->
39+
<junit.version>4.12</junit.version>
40+
</properties>
41+
</project>
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
package com.baeldung;
2+
3+
import java.io.IOException;
4+
import java.time.Duration;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
import org.junit.After;
10+
import org.junit.AfterClass;
11+
import org.junit.Assert;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import redis.clients.jedis.Jedis;
16+
import redis.clients.jedis.JedisPool;
17+
import redis.clients.jedis.JedisPoolConfig;
18+
import redis.clients.jedis.Pipeline;
19+
import redis.clients.jedis.Response;
20+
import redis.clients.jedis.Transaction;
21+
import redis.embedded.RedisServer;
22+
23+
/**
24+
* Unit test for Redis Java library - Jedis.
25+
*/
26+
public class JedisTest {
27+
28+
private Jedis jedis;
29+
private static RedisServer redisServer;
30+
31+
public JedisTest() {
32+
jedis = new Jedis();
33+
}
34+
35+
@BeforeClass
36+
public static void setUp() throws IOException {
37+
redisServer = new RedisServer(6379);
38+
redisServer.start();
39+
}
40+
41+
@AfterClass
42+
public static void destroy() {
43+
redisServer.stop();
44+
}
45+
46+
@After
47+
public void flush() {
48+
jedis.flushAll();
49+
}
50+
51+
@Test
52+
public void givenAStringThenSaveItAsRedisStrings() {
53+
String key = "key";
54+
String value = "value";
55+
56+
jedis.set(key, value);
57+
String value2 = jedis.get(key);
58+
59+
Assert.assertEquals(value, value2);
60+
}
61+
62+
@Test
63+
public void givenListElementsThenSaveThemInRedisList() {
64+
String queue = "queue#tasks";
65+
66+
String taskOne = "firstTask";
67+
String taskTwo = "secondTask";
68+
String taskThree = "thirdTask";
69+
70+
jedis.lpush(queue, taskOne, taskTwo);
71+
72+
String taskReturnedOne = jedis.rpop(queue);
73+
74+
jedis.lpush(queue, taskThree);
75+
Assert.assertEquals(taskOne, taskReturnedOne);
76+
77+
String taskReturnedTwo = jedis.rpop(queue);
78+
String taskReturnedThree = jedis.rpop(queue);
79+
80+
Assert.assertEquals(taskTwo, taskReturnedTwo);
81+
Assert.assertEquals(taskThree, taskReturnedThree);
82+
83+
String taskReturnedFour = jedis.rpop(queue);
84+
Assert.assertNull(taskReturnedFour);
85+
}
86+
87+
@Test
88+
public void givenSetElementsThenSaveThemInRedisSet() {
89+
String countries = "countries";
90+
91+
String countryOne = "Spain";
92+
String countryTwo = "Ireland";
93+
String countryThree = "Ireland";
94+
95+
jedis.sadd(countries, countryOne);
96+
97+
Set<String> countriesSet = jedis.smembers(countries);
98+
Assert.assertEquals(1, countriesSet.size());
99+
100+
jedis.sadd(countries, countryTwo);
101+
countriesSet = jedis.smembers(countries);
102+
Assert.assertEquals(2, countriesSet.size());
103+
104+
jedis.sadd(countries, countryThree);
105+
countriesSet = jedis.smembers(countries);
106+
Assert.assertEquals(2, countriesSet.size());
107+
108+
boolean exists = jedis.sismember(countries, countryThree);
109+
Assert.assertTrue(exists);
110+
}
111+
112+
@Test
113+
public void givenObjectFieldsThenSaveThemInRedisHash() {
114+
String key = "user#1";
115+
116+
String field = "name";
117+
String value = "William";
118+
119+
String field2 = "job";
120+
String value2 = "politician";
121+
122+
jedis.hset(key, field, value);
123+
jedis.hset(key, field2, value2);
124+
125+
String value3 = jedis.hget(key, field);
126+
Assert.assertEquals(value, value3);
127+
128+
Map<String, String> fields = jedis.hgetAll(key);
129+
String value4 = fields.get(field2);
130+
Assert.assertEquals(value2, value4);
131+
}
132+
133+
@Test
134+
public void givenARankingThenSaveItInRedisSortedSet() {
135+
String key = "ranking";
136+
137+
Map<String, Double> scores = new HashMap<>();
138+
139+
scores.put("PlayerOne", 3000.0);
140+
scores.put("PlayerTwo", 1500.0);
141+
scores.put("PlayerThree", 8200.0);
142+
143+
for (String player : scores.keySet()) {
144+
jedis.zadd(key, scores.get(player), player);
145+
}
146+
147+
Set<String> players = jedis.zrevrange(key, 0, 1);
148+
Assert.assertEquals("PlayerThree", players.iterator().next());
149+
150+
long rank = jedis.zrevrank(key, "PlayerOne");
151+
Assert.assertEquals(1, rank);
152+
}
153+
154+
@Test
155+
public void givenMultipleOperationsThatNeedToBeExecutedAtomicallyThenWrapThemInATransaction() {
156+
String friendsPrefix = "friends#";
157+
158+
String userOneId = "4352523";
159+
String userTwoId = "5552321";
160+
161+
Transaction t = jedis.multi();
162+
t.sadd(friendsPrefix + userOneId, userTwoId);
163+
t.sadd(friendsPrefix + userTwoId, userOneId);
164+
t.exec();
165+
166+
boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId);
167+
Assert.assertTrue(exists);
168+
169+
exists = jedis.sismember(friendsPrefix + userTwoId, userOneId);
170+
Assert.assertTrue(exists);
171+
}
172+
173+
@Test
174+
public void givenMultipleIndependentOperationsWhenNetworkOptimizationIsImportantThenWrapThemInAPipeline() {
175+
String userOneId = "4352523";
176+
String userTwoId = "4849888";
177+
178+
Pipeline p = jedis.pipelined();
179+
p.sadd("searched#" + userOneId, "paris");
180+
p.zadd("ranking", 126, userOneId);
181+
p.zadd("ranking", 325, userTwoId);
182+
Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris");
183+
Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1);
184+
p.sync();
185+
186+
Assert.assertTrue(pipeExists.get());
187+
Assert.assertEquals(2, pipeRanking.get().size());
188+
}
189+
190+
@Test
191+
public void givenAPoolConfigurationThenCreateAJedisPool() {
192+
final JedisPoolConfig poolConfig = buildPoolConfig();
193+
194+
try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost");
195+
Jedis jedis = jedisPool.getResource()) {
196+
197+
// do simple operation to verify that the Jedis resource is working properly
198+
String key = "key";
199+
String value = "value";
200+
201+
jedis.set(key, value);
202+
String value2 = jedis.get(key);
203+
204+
Assert.assertEquals(value, value2);
205+
206+
// flush Redis
207+
jedis.flushAll();
208+
}
209+
}
210+
211+
private JedisPoolConfig buildPoolConfig() {
212+
final JedisPoolConfig poolConfig = new JedisPoolConfig();
213+
poolConfig.setMaxTotal(128);
214+
poolConfig.setMaxIdle(128);
215+
poolConfig.setMinIdle(16);
216+
poolConfig.setTestOnBorrow(true);
217+
poolConfig.setTestOnReturn(true);
218+
poolConfig.setTestWhileIdle(true);
219+
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
220+
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
221+
poolConfig.setNumTestsPerEvictionRun(3);
222+
poolConfig.setBlockWhenExhausted(true);
223+
return poolConfig;
224+
}
225+
}

0 commit comments

Comments
 (0)