RedisCache.java
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.jflyfox.util.cache;
import com.jflyfox.jfinal.component.redis.JedisClient;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class RedisCache implements Cache {
protected String name;
protected JedisClient client = null;
public RedisCache() {
client = JedisClient.getInstance();
}
public String name() {
return name;
}
public RedisCache name(String name) {
this.name = name;
return this;
}
public RedisCache add(String key, Object value) {
client.hsetObj(this.name, key, value);
return this;
}
@Override
public Cache add(String key, Object value, Long time, TimeUnit unit) {
client.setObj(this.name + key, time.intValue(), value);
return this;
}
@SuppressWarnings("unchecked")
public <T> T get(String key) {
return (T) client.hgetObj(this.name, key);
}
@SuppressWarnings("unchecked")
public <T> T getObj(String key) {
return (T) client.getObj(this.name + key);
}
public Object remove(String key) {
client.hdel(this.name, key);
return null;
}
public void clear() {
client.hdelObjAll(this.name);
}
public int size() {
return (int) client.hlenObj(this.name);
}
public Set<String> keys() {
Map<String, Object> map = client.hgetAllObj(this.name);
if (map.size() == 0) {
return null;
}
return map.keySet();
}
public <T> Collection<T> values() {
Map<String, T> map = client.hgetAllObj(this.name);
if (map.size() == 0) {
return null;
}
return map.values();
}
@Override
public <T> T getCacheByKey(String key) {
return (T) client.getObj(this.name + key);
}
}