redis主要存储类型最常用的五种数据类型:

  • String
  • Hash
  • List
  • Set
  • Sorted set

redis不能直接存取对象,如何解决呢?

两种方式

1、利用序列化和反序列化的方式

两层对象存取到redis 示例:

序列化工具类

public class SerializeUtil {
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
//序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
}
return null;
} public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
//反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
}
public class Person implements Serializable {
private String pid;
private String pName;
private String nation;
private String age;
private Animal animal;

  

 public class Animal implements Serializable {
private String aId;
private String color;
private String hobby;
private String aName;

在此强调必须让类实现序列化

 public class RedisObject {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("10.0.34.52",6379);
jedis.auth("");
System.out.println("Connection to server sucessfully");
//查看服务是否运行
System.out.println("Server is running: " + jedis.ping());
Person person = new Person();
person.setAge("15");
person.setNation("美国");
person.setpName("jim");
Animal animal = new Animal();
animal.setaId("21");
animal.setaName("小白");
animal.setColor("red");
person.setAnimal(animal);
System.out.println("序列化中");
jedis.set("person".getBytes(),SerializeUtil.serialize(person));
System.out.println("序列化成功");
byte[] bytes = jedis.get("person".getBytes());
Object object = SerializeUtil.unserialize(bytes);
Person person1= (Person)object;
System.out.println(person1);

2:将java对象转换为json字符串,利用json与java对象之间可以相互转换的方式进行存值和取值

try {
String s = JacksonUtils.getInstance().writeValueAsString(person);
System.out.println("对象转化字符串:"+s);
jedis.set("person2",s);
String person2 = jedis.get("person2");
Person person3 = JacksonUtils.getInstance().readValue(person2, Person.class);
System.out.println(person3);
} catch (IOException e) {
e.printStackTrace();
}


												

redis存取对象的更多相关文章

  1. redis通过json方案存取对象com.alibaba.fastjson.JSONException: syntax error, expect

    问题描述: redis基于json方案存取对象时报错:com.alibaba.fastjson.JSONException: syntax error, expect com.alibaba.fast ...

  2. redis 缓存对象、列表

    在spring boot环境下有个StringRedisTemplate对象,默认已经为我们配置好了,只需要自动注入过来就能用,但是使用它只能在Redis中存放字符串.具体操作如下: @RunWith ...

  3. redis object 对象系统

    redis object对象系统 概述 redis 当中, sds字符串, adlist双向链表, dict字典, ziplist压缩链表, intset整数集合等均为底层数据结构 redis 并没有 ...

  4. redis存储对象

      redis主要存储类型最常用的五种数据类型: String Hash List Set Sorted set redis存储对象序列化和反序列化 首先来了解一下为什么要实现序列化 为什么要实现序列 ...

  5. redis存储对象与对象序列化详解

    redis主要存储类型最常用的五种数据类型: String Hash List Set Sorted set redis存储对象序列化和反序列化 首先来了解一下为什么要实现序列化 为什么要实现序列化接 ...

  6. redis的对象

    简介:redis并没有直接使用前面所提到的基本数据结构,而是基于基本的数据结构构造了一个对象系统.这个系统包含了字符串对象,列表对象,哈希对象,集合对象,有序集合对象五种类型的对象.每种对象都用到了至 ...

  7. Redis | 使用redis存储对象反序列化异常SerializationFailedException

    案例 使用Redis进行对象存储,在处理业务逻辑的时候,丛Redis获取对象发现反序列化失败,抛出如下异常: Caused by: org.springframework.data.redis.ser ...

  8. 深入了解Redis(3)-对象

    Redis主要的数据结构有简单动态字符串(SDS).双端链表.字典.压缩列表.整数集合,等等.但Redis并没有直接使用这些数据结构来实现键值对数据库, 而是基于这些数据结构创建了一个对象系统, 这个 ...

  9. Redis 存储对象信息是用 Hash 还是 String

    Redis 内部使用一个 RedisObject 对象来表示所有的 key 和 value,RedisObject 中的 type,则是代表一个 value 对象具体是何种数据类型,它包含字符串(St ...

随机推荐

  1. Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.commons.EmptyVisitor

    1.错误描述 2014-7-13 1:45:53 org.apache.struts2.spring.StrutsSpringObjectFactory info 信息: ... initialize ...

  2. Linux显示登入系统的帐号名称和总人数

    Linux显示登入系统的帐号名称和总人数 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ who -q youhaidong youhaidong # 用户数= ...

  3. lvs简单使用

    LB集群实现 硬件 F5 BIG-IP Citrix NetScaler A10 Redware 软件 1 lvs 2 haproxy 3 nginx 4 ats apache traffic ser ...

  4. POI 的API大全二

    1.POI结构与常用类 (1)POI介绍 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发 ...

  5. json省市县数据源

    {cityData,"Code":""},{"Name":"牡丹江市",","Code": ...

  6. RobotFramework下的http接口自动化Set Request Header 关键字的使用

    Set Request Header 关键字用来设置http请求时的请求头部信息. 该关键字接收两个参数,[ header_name | header_value ] 示例1:设置http请求时的Re ...

  7. [POI2000]病毒

    题面 传送门 Sol 建出AC自动机后DFS能走的点,如果能走回来就可行 # include <bits/stdc++.h> # define IL inline # define RG ...

  8. [BZOJ1507] [NOI2003] Editor (splay)

    Description Input 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它 ...

  9. js实现对树深度优先遍历与广度优先遍历

    深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...

  10. which命令实战及原理详解-PATH实战配置

    Which查找命令所在的路径,搜索范围来自全局环境PATH变量对应的路径. 其他方法: find / -type f -name “useradd” whereis -b useradd PATH的路 ...