Skip to content

Commit d1a7c20

Browse files
author
Igor Polevoy
committed
#559 Use of pool in RedisCacheManager
1 parent f82af3f commit d1a7c20

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/cache/RedisCacheManager.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import org.javalite.activejdbc.InitException;
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
7-
import redis.clients.jedis.BinaryJedis;
8-
import redis.clients.jedis.Jedis;
7+
import redis.clients.jedis.JedisPool;
98

109
import java.io.*;
1110
import java.util.Properties;
@@ -20,7 +19,7 @@
2019
* with two properties: <code>redist-host</code> and <code>redist-port</code>.
2120
* </p>
2221
* <p>
23-
* The properties file needs to be at the root of classpath.
22+
* The properties file needs to be at the root of classpath.
2423
* </p>
2524
* <p><strong>Limitation:</strong> Does not support {@link #flush(CacheEvent)} with value 'ALL'.</p>
2625
*
@@ -29,19 +28,20 @@
2928
public class RedisCacheManager extends CacheManager {
3029
private static final Logger LOGGER = LoggerFactory.getLogger(RedisCacheManager.class);
3130

32-
private BinaryJedis jedis;
31+
private JedisPool jedis;
3332

3433

35-
public RedisCacheManager() {
34+
public RedisCacheManager() {
3635
Properties props = new Properties();
3736
InputStream in = getClass().getResourceAsStream("/activejdbc-redis.properties");
38-
if(in == null){
39-
jedis = new Jedis("localhost");
40-
}else{
37+
if (in == null) {
38+
jedis = new JedisPool("localhost");
39+
} else {
4140
try {
42-
props.load(in);String host = props.getProperty("redis-host");
41+
props.load(in);
42+
String host = props.getProperty("redis-host");
4343
String port = props.getProperty("redis-port");
44-
jedis = new Jedis(host, toInteger(port));
44+
jedis = new JedisPool(host, toInteger(port));
4545
} catch (Exception e) {
4646
throw new InitException("Failed to configure connection to Redis server", e);
4747
}
@@ -51,11 +51,11 @@ public RedisCacheManager() {
5151
@Override
5252
public Object getCache(String group, String key) {
5353
try {
54-
byte[] bytes = jedis.hget(group.getBytes(), key.getBytes());
54+
byte[] bytes = jedis.getResource().hget(group.getBytes(), key.getBytes());
5555

56-
if(bytes == null){
56+
if (bytes == null) {
5757
return null;
58-
}else{
58+
} else {
5959
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
6060
return in.readObject();
6161
}
@@ -73,18 +73,18 @@ public void addCache(String group, String key, Object cache) {
7373
objectOutput.writeObject(cache);
7474
objectOutput.close();
7575
byte[] bytes = bout.toByteArray();
76-
jedis.hset(group.getBytes(), key.getBytes(), bytes);
76+
jedis.getResource().hset(group.getBytes(), key.getBytes(), bytes);
7777
} catch (Exception e) {
7878
LOGGER.error("Failed to add object to cache with group: " + group + " and key: " + key, e);
7979
}
8080
}
8181

8282
@Override
8383
public void doFlush(CacheEvent event) {
84-
if(event.getType().equals(CacheEvent.CacheEventType.ALL)){
84+
if (event.getType().equals(CacheEvent.CacheEventType.ALL)) {
8585
throw new UnsupportedOperationException("Flushing all caches not supported");
86-
}else if (event.getType().equals(CacheEvent.CacheEventType.GROUP)) {
87-
jedis.del(event.getGroup().getBytes());
86+
} else if (event.getType().equals(CacheEvent.CacheEventType.GROUP)) {
87+
jedis.getResource().del(event.getGroup().getBytes());
8888
}
8989
}
9090

0 commit comments

Comments
 (0)