30
30
*/
31
31
public final class RedisCache implements Cache {
32
32
33
- private final ReadWriteLock readWriteLock = new DummyReadWriteLock ();
34
-
35
- private String id ;
36
-
37
- private static JedisPool pool ;
38
-
39
- public RedisCache (final String id ) {
40
- if (id == null ) {
41
- throw new IllegalArgumentException ("Cache instances require an ID" );
42
- }
43
- this .id = id ;
44
- ConfigWithHost configWithHost = RedisConfigurationBuilder .getInstance ().parseConfiguration ();
45
- pool = new JedisPool (configWithHost , configWithHost .getHost ());
46
- }
47
-
48
- private Object execute (RedisCallback callback ) {
49
- Jedis jedis = pool .getResource ();
50
- try {
51
- return callback .doWithRedis (jedis );
52
- } finally {
33
+ private final ReadWriteLock readWriteLock = new DummyReadWriteLock ();
34
+
35
+ private String id ;
36
+
37
+ private static JedisPool pool ;
38
+
39
+ public RedisCache (final String id ) {
40
+ if (id == null ) {
41
+ throw new IllegalArgumentException ("Cache instances require an ID" );
42
+ }
43
+ this .id = id ;
44
+ ConfigWithHost configWithHost = RedisConfigurationBuilder .getInstance ().parseConfiguration ();
45
+ pool = new JedisPool (configWithHost , configWithHost .getHost ());
46
+ }
47
+
48
+ private Object execute (RedisCallback callback ) {
49
+ Jedis jedis = pool .getResource ();
50
+ try {
51
+ return callback .doWithRedis (jedis );
52
+ } finally {
53
53
jedis .close ();
54
- }
54
+ }
55
55
}
56
56
57
- public String getId () {
58
- return this .id ;
59
- }
57
+ public String getId () {
58
+ return this .id ;
59
+ }
60
60
61
- public int getSize () {
61
+ public int getSize () {
62
62
return (Integer ) execute (new RedisCallback () {
63
63
public Object doWithRedis (Jedis jedis ) {
64
64
Map <byte [], byte []> result = jedis .hgetAll (id .toString ().getBytes ());
@@ -67,56 +67,48 @@ public Object doWithRedis(Jedis jedis) {
67
67
});
68
68
}
69
69
70
- public void putObject (final Object key , final Object value ) {
71
- execute (new RedisCallback () {
72
- public Object doWithRedis (Jedis jedis ) {
73
- jedis .hset (id .toString ().getBytes (), key .toString ().getBytes (), SerializeUtil .serialize (value ));
74
- return null ;
75
- }
76
- });
77
- }
78
-
79
- public Object getObject (final Object key ) {
80
- return execute (new RedisCallback () {
81
- public Object doWithRedis (Jedis jedis ) {
82
- return SerializeUtil .unserialize (jedis .hget (id .toString ().getBytes (), key .toString ().getBytes ()));
83
- }
84
- });
85
- }
86
-
87
- public Object removeObject (final Object key ) {
88
- return execute (new RedisCallback () {
89
- public Object doWithRedis (Jedis jedis ) {
90
- return jedis .hdel (id .toString (), key .toString ());
91
- }
92
- });
93
- }
94
-
95
- public void clear () {
96
- execute (new RedisCallback () {
97
- public Object doWithRedis (Jedis jedis ) {
98
- jedis .del (id .toString ());
99
- return null ;
100
- }
101
- });
102
-
103
- }
104
-
105
- public ReadWriteLock getReadWriteLock () {
106
- return readWriteLock ;
107
- }
108
-
109
- @ Override
110
- public String toString () {
111
- return "Redis {"
112
- + id
113
- + "}" ;
114
- }
70
+ public void putObject (final Object key , final Object value ) {
71
+ execute (new RedisCallback () {
72
+ public Object doWithRedis (Jedis jedis ) {
73
+ jedis .hset (id .toString ().getBytes (), key .toString ().getBytes (), SerializeUtil .serialize (value ));
74
+ return null ;
75
+ }
76
+ });
77
+ }
78
+
79
+ public Object getObject (final Object key ) {
80
+ return execute (new RedisCallback () {
81
+ public Object doWithRedis (Jedis jedis ) {
82
+ return SerializeUtil .unserialize (jedis .hget (id .toString ().getBytes (), key .toString ().getBytes ()));
83
+ }
84
+ });
85
+ }
86
+
87
+ public Object removeObject (final Object key ) {
88
+ return execute (new RedisCallback () {
89
+ public Object doWithRedis (Jedis jedis ) {
90
+ return jedis .hdel (id .toString (), key .toString ());
91
+ }
92
+ });
93
+ }
94
+
95
+ public void clear () {
96
+ execute (new RedisCallback () {
97
+ public Object doWithRedis (Jedis jedis ) {
98
+ jedis .del (id .toString ());
99
+ return null ;
100
+ }
101
+ });
102
+
103
+ }
104
+
105
+ public ReadWriteLock getReadWriteLock () {
106
+ return readWriteLock ;
107
+ }
115
108
116
- @ Override
117
- protected void finalize () throws Throwable {
118
- pool .destroy ();
119
- super .finalize ();
120
- }
109
+ @ Override
110
+ public String toString () {
111
+ return "Redis {" + id + "}" ;
112
+ }
121
113
122
114
}
0 commit comments