为什么要把对象转为Byte数组
- 可以跨JVM传输对象:一旦对象被转换为字节数组,就可以在不同的JVM或系统之间轻松传输。
- 序列化:对象转换为字节数组进行序列化,这使得对象能够被持久化到存储系统中,或通过网络传输。
- 提高效率:字节数组比对象本身占用更少的内存和处理资源。
Redis依赖和版本
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
Redis配置
#Redis配置
# Redis数据库索引(默认为0)
# Redis服务器地址
# Redis服务器连接端口
# Redis服务器连接密码(默认为空)
# 链接超时时间 单位 ms(毫秒)
# 连接池最大连接数(使用负值表示没有限制) 默认 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
# 连接池中的最大空闲连接 默认 8
# 连接池中的最小空闲连接 默认 0
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
connect-timeout: 3000
password:
lettuce:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
RedisTemplate配置类
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//设置key序列化方式String
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置value的序列化方式json,使用JdkSerializationRedisSerializer序列化,因为项目中User默认JDK序列化所以保持一致
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
Bean对象(注意要可序列化,即实现Serializable接口)
public class User implements Serializable {
private static final long serialVersionUID = 202406281601L;
private String userId;
private String userName;
private String password;
public User() {
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//介绍自己
public String Speak() {
return "我的信息是:{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
实现方法:
public String getUserByRedis() {
User user = new User();
user.setUserId("123456");
user.setUserName("zhangSan");
user.setPassword("666666");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
objectOutputStream.writeObject(user);
byte[] bytes = bos.toByteArray();
objectOutputStream.close();
redisTemplate.opsForValue().set(GET_USER_KEY, bytes);
} catch (IOException e) {
e.printStackTrace();
}
try {
byte[] value = (byte[]) redisTemplate.opsForValue().get(GET_USER_KEY);
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(value));
User currUser = (User) objectInputStream.readObject();
objectInputStream.close();
return JSON.toJSONString(currUser);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
上面代码中用到了JSON
<!-- JSON -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.69</version>
</dependency>