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 }

 

转载于:https://www.cnblogs.com/ronniery/p/11058959.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值