抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

Redis有很多数据结构:string、hash、list、set等,但是在实际来发中,我们往往保存的是对象在redis中,所以键值对更加常用。

Redis在保存对象时,需要满足序列化,由于java的原生序列化方式效率较低,故更加常用的是Protostuff这个序列化框架。

一、封装Protostuff工具类

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

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtobufIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

public class SerializeUtil {
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<>();

@SuppressWarnings("unchecked")
public static <T> byte[] serializer(T obj) {
Class<T> clazz = (Class<T>) obj.getClass();
Schema<T> schema = getSchema(clazz);
return ProtobufIOUtil.toByteArray(obj, schema, LinkedBuffer.allocate(256));
}

public static <T> T deSerializer(byte[] bytes, Class<T> clazz) {
T message;
try {
message = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
Schema<T> schema = getSchema(clazz);
ProtobufIOUtil.mergeFrom(bytes, message, schema);
return message;
}

@SuppressWarnings("unchecked")
public static <T> Schema<T> getSchema(Class<T> clazz) {
Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
if (schema == null) {
schema = RuntimeSchema.createFrom(clazz);
if (schema != null) {
cachedSchema.put(clazz, schema);
}
}
return schema;
}
}

二、封装RedisUtil工具类

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {

private static String ADDR = "localhost";
private static int PORT = 6379;
private static String AUTH = "123456";

// 最大连接实例数,默认为8,-1表示无限制,如果pool已经分配了超过max_active个jedis实例,则此时pool为耗尽
private static int MAX_ACTIVE = 1024;

// 最大空闲实例,默认为8
private static int MAX_IDLE = 200;

// 最大等待连接时间,单位毫秒默认为-1,表示永不超时,超时会抛出JedisConnectionException
private static int MAX_WAIT = 10 * 1000;

private static int TIMEOUT = 10 * 1000;

// 在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的
private static boolean TEST_ON_BORROW = true;

private static JedisPool jedisPool = null;
/**
* 初始化连接池
*/
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);//带密码
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);//不带密码。
}

/**
* 获取jedis实例
*/
public synchronized static Jedis getJedis() {
if (jedisPool != null) {

Jedis resource = jedisPool.getResource();
return resource;
}
return null;
}

/**
* 释放资源
*
* @param jedis
*/
public static void close(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}

/**
* 保存对象(键-值)
*
* @param <T>
*
* @param key
* @param object
* @return
*/
public static <T> String setObject(String key, T object) {
Jedis jedis = getJedis();
try {
return jedis.set(key.getBytes(), SerializeUtil.serializer(object));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}

/**
* 带时间保存(键值)
*
* @param key
* @param object
* @param expiretime
* @return
*/
public static <T> String setObject(String key, T object, long expiretime) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.psetex(key.getBytes(), expiretime, SerializeUtil.serializer(object));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}

/**
* 读取对象(键-值)
*
* @param key
* @param clazz
* @return
*/
public static <T> T getObject(String key, Class<T> clazz) {
Jedis jedis = getJedis();
try {
byte[] bytes = jedis.get(key.getBytes());
T object = SerializeUtil.deSerializer(bytes, clazz);
return object;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}

/**
* 删除key
*/
public static Long deleteObject(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.del(key.getBytes());
} catch (Exception e) {
return null;
} finally {
close(jedis);
}
}

/**
* 检查存在
*
* @param key
* @return
*/
public static Boolean existsObject(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(key.getBytes());
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
close(jedis);
}
}

}

三、测试

User:

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

public class User{

private String name;

private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}

}
1
2
3
4
5
6
7
8
9
10
11
12

public class Main {
public static void main(String[] args) {
User user = new User();
user.setName("旭旭宝宝");
user.setAge(33);
RedisUtil.setObject("user", user);
User obj = RedisUtil.getObject("user", User.class);
System.out.println(obj);
System.out.println(RedisUtil.existsObject("user"));
}
}

参考文章

RedisUtil工具类封装

评论