关于redis的存储实体类的方式

  • 由于对象的存储的方式:环境的配置看上一篇的blog
    • redis主要是存储的数据的存储的在内存中
    • 由于redis的存储的方式分为一种字符串 和byte[] 两种的存储方式:所以讲对象分为两种的方式:
        1. 转换为字符串的方式:转换为json的字符串和xml的方式
        1. 转换为byte[]的数组: 对实体类进行序列化和反序列化的方式
  • 这里我们主要对byte的 :
    • 对于单个对象的存储
      image
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
/**
* 序列化
*/
public static byte[] ser(Object o)
{

ObjectOutput oos=null;
ByteArrayOutputStream baos=null;
try {
//创建一个byte的数组的流
baos=new ByteArrayOutputStream();
//创建对象流将对象写入到byte数组里
oos=new ObjectOutputStream(baos);
//进行写入操作
oos.writeObject(o);
//将byte数组的对象进行转换为byte的数组
return baos.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



return null;


}
/**
* 反序列化
*/
public static Object unser(byte[]bs)
{
ByteArrayInputStream bais=null;
//创建一个byte的数组的读入流对其byte数组
bais =new ByteArrayInputStream(bs);
try {
//对象的输入流,用于读取对象
ObjectInputStream objectInputStream=new ObjectInputStream(bais);
//将byte格式的转化为对象
return objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

return bs;

}
-  对于list集合的对象的存储的方式
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

/**
* 序列化的集合
*/
public static byte[] serlist(List<?>list)
{
ObjectOutputStream oos= null;
//
ByteArrayOutputStream byteArrayOutputStream=null;
byte[] bytes =null;
byteArrayOutputStream=new ByteArrayOutputStream();
try {
oos=new ObjectOutputStream(byteArrayOutputStream);
for (Object o : list) {
oos.writeObject(o);

}
bytes =byteArrayOutputStream.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return bytes;
}
/**
* 反序列化的集合
*/
public static List<?> unserList(byte[] bytes)
{
List<Object>list=new ArrayList<>();
ByteArrayInputStream bais =null;
ObjectInputStream ois=null;
bais=new ByteArrayInputStream(bytes);
try {
ois =new ObjectInputStream(bais);
while (bais.available()>0)
{
Object object =ois.readObject();
if (object==null) {
break;

}
list.add(object);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值