-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathRedisUtil.java
More file actions
94 lines (77 loc) · 2.06 KB
/
RedisUtil.java
File metadata and controls
94 lines (77 loc) · 2.06 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
88
89
90
91
92
93
94
package Common;
import RedisDataBase.AbstractPooledObject;
import RedisDataBase.RedisString;
/* 辅助类 */
public class RedisUtil {
// 因为key不可能为null,所以不需要检查
// 只检查10进制的情况
public static boolean isInteger(String s) {
int len = s.length();
if(len == 0) return false;
if( s.charAt(0) == '-' && len == 1) {
return false;
}
for(int i = 0; i < len; i++) {
char c = s.charAt(i);
if(c > '9' || c < '0'){
return false;
}
}
return true;
}
public static boolean isInteger(RedisString s) {
int len = s.size();
if(len == 0) return false;
if( s.getByte(0) == '-' && len == 1) {
return false;
}
for(int i = 0; i < len; i++) {
byte c = s.getByte(i);
if(c > '9' || c < '0'){
return false;
}
}
return true;
}
// 根据lazy是否延迟释放
public static void doRelease(Object o,boolean lazy){
if(o instanceof AbstractPooledObject) {
AbstractPooledObject op = (AbstractPooledObject)o;
if(lazy){
op.enableLazyReleased();
}else{
op.release();
}
}
}
public static Integer parseInt(RedisString s){
int len = s.size();
int i = 0;
int sign = 1;
int sum = 0;
if(s.getByte(0) == '-'){
i++;
sign = -1;
}
for(; i < len; i++) {
byte c = s.getByte(i);
if(c > '9' || c < '0') {
return null;
}else{
sum = sum * 10 + c - '0';
}
}
return sum * sign;
}
// 获取最接近二次幂
public static int sizeForTable(int cap){
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
n = (n < 0) ? 1 : n + 1;
return n;
}
}