ProtostuffIOUtil+Redis 对于java对象存redis的操作

 1 package com;
 2 
 3 import io.protostuff.LinkedBuffer;
 4 import io.protostuff.ProtostuffIOUtil;
 5 import io.protostuff.Schema;
 6 import io.protostuff.runtime.RuntimeSchema;
 7 import redis.clients.jedis.Jedis;
 8 import redis.clients.jedis.JedisPool;
 9 
10 /**
11  * @Author: K0570187-ry
12  * @CreateDate: 2019/4/2$ 14:02$
13  */
14 public class Test {
15     public static void main(String[] args) throws IllegalAccessException, InstantiationException {
16         User user = new User(){
17             {
18                 this.setPassword("password");
19             }
20         };
21         user.setId(1L);
22         user.setName("name");
23         user.setPassword("password");
24         Class<User>  t = User.class;
25         Schema<User> schema = RuntimeSchema.getSchema(t);
26 
27         JedisPool jedisPool = null;
28         Jedis jedis = jedisPool.getResource();
29         // redis
30 
31         // save
32         jedis.set(user.getId().toString().getBytes(),ProtostuffIOUtil.toByteArray(user, schema, LinkedBuffer.allocate()));
33         // get from redis
34         User redisUser = t.newInstance();
35         ProtostuffIOUtil.mergeFrom(jedis.get(user.getId().toString().getBytes()),redisUser,schema);
36 
37         jedis.close();
38     }
39 
40 
41 }

 

posted @ 2019-06-20 15:25  ronniery  阅读(753)  评论(0编辑  收藏  举报