4
4
import org .javalite .activejdbc .InitException ;
5
5
import org .slf4j .Logger ;
6
6
import org .slf4j .LoggerFactory ;
7
- import redis .clients .jedis .BinaryJedis ;
8
- import redis .clients .jedis .Jedis ;
7
+ import redis .clients .jedis .JedisPool ;
9
8
10
9
import java .io .*;
11
10
import java .util .Properties ;
20
19
* with two properties: <code>redist-host</code> and <code>redist-port</code>.
21
20
* </p>
22
21
* <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.
24
23
* </p>
25
24
* <p><strong>Limitation:</strong> Does not support {@link #flush(CacheEvent)} with value 'ALL'.</p>
26
25
*
29
28
public class RedisCacheManager extends CacheManager {
30
29
private static final Logger LOGGER = LoggerFactory .getLogger (RedisCacheManager .class );
31
30
32
- private BinaryJedis jedis ;
31
+ private JedisPool jedis ;
33
32
34
33
35
- public RedisCacheManager () {
34
+ public RedisCacheManager () {
36
35
Properties props = new Properties ();
37
36
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 {
41
40
try {
42
- props .load (in );String host = props .getProperty ("redis-host" );
41
+ props .load (in );
42
+ String host = props .getProperty ("redis-host" );
43
43
String port = props .getProperty ("redis-port" );
44
- jedis = new Jedis (host , toInteger (port ));
44
+ jedis = new JedisPool (host , toInteger (port ));
45
45
} catch (Exception e ) {
46
46
throw new InitException ("Failed to configure connection to Redis server" , e );
47
47
}
@@ -51,11 +51,11 @@ public RedisCacheManager() {
51
51
@ Override
52
52
public Object getCache (String group , String key ) {
53
53
try {
54
- byte [] bytes = jedis .hget (group .getBytes (), key .getBytes ());
54
+ byte [] bytes = jedis .getResource (). hget (group .getBytes (), key .getBytes ());
55
55
56
- if (bytes == null ){
56
+ if (bytes == null ) {
57
57
return null ;
58
- }else {
58
+ } else {
59
59
ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (bytes ));
60
60
return in .readObject ();
61
61
}
@@ -73,18 +73,18 @@ public void addCache(String group, String key, Object cache) {
73
73
objectOutput .writeObject (cache );
74
74
objectOutput .close ();
75
75
byte [] bytes = bout .toByteArray ();
76
- jedis .hset (group .getBytes (), key .getBytes (), bytes );
76
+ jedis .getResource (). hset (group .getBytes (), key .getBytes (), bytes );
77
77
} catch (Exception e ) {
78
78
LOGGER .error ("Failed to add object to cache with group: " + group + " and key: " + key , e );
79
79
}
80
80
}
81
81
82
82
@ Override
83
83
public void doFlush (CacheEvent event ) {
84
- if (event .getType ().equals (CacheEvent .CacheEventType .ALL )){
84
+ if (event .getType ().equals (CacheEvent .CacheEventType .ALL )) {
85
85
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 ());
88
88
}
89
89
}
90
90
0 commit comments