Spring Boot整合Spring Data Redis-存取JSON格式Java对象

如何通过Spring Data Redis,以JSON格式来存储我们的实体对象,其实上节课我们讲了基于JDK的序列化器,

来帮助我们做一个序列化,做一个对象的存储,那么这种方式需要注意一点的是,如果我们用的是JDK的序列化器,

来对我们的对象做序列化,存到Redis当中的时候,对于序列化以后的结果,相比较JSON格式,起码要比JSON格式大

5倍以上,就是在Redis当中存的内容,要比我们以JSON格式转换完的内容,要大5倍以上,所以这样就会对Redis空间

造成一个浪费的一个问题,所以这个转换的方式,根据自己的开发情况来做选择,他有这样的一个缺陷,我们怎么以JSON

的格式来存储我们的对象,首先我们在测试类当中,直接去写一个测试方法,基于JSON格式存

/**
 * 基于JSON格式存Users对象
 */
@Test
public void testSetUsersUseJSON(){
	Users users = new Users();
	users.setAge(20);
	users.setName("李四丰");
	users.setId(1);
	this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
	this.redisTemplate.opsForValue().set("users_json", users);
}

你想我们要把对象做存储,之前是需要这个对象,所以我先把这个对象给拿过来,我们给改个名,我们这里肯定还要

换一个序列化器,还是打开这个包

org.springframework.data.redis.serializer

看我们的序列化器,这里其实提供了一个JacksonJsonRedisSerializer,有两个,一个是Jackson,一个是Jackson2,这里我们

需要注意的,不带2的已经过时了

/**
 * {@link RedisSerializer} that can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson's</a>
 * {@link ObjectMapper}.
 * <p>
 * This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
 * <b>Note:</b>Null objects are serialized as empty arrays and vice versa.
 * 
 * @author Costin Leau
 * @author Thomas Darimont
 * @author Christoph Strobl
 * @deprecated ince 1.7. Will be removed in subsequent version.
 */
@Deprecated
public class JacksonJsonRedisSerializer<T> implements RedisSerializer<T> {

我们可以使用Jackson2JsonRedisSerializer,还有GenericJackson2JsonRedisSerializer,我们用哪个都可以,但是千万不要用

JacksonJsonRedisSerializer,用这个org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer<T>来做,

我们肯定要切换序列化器

this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));

你得告诉我类的信息,这样就可以对我们的User对象进行JSON转换了,添加到redis当中,比如我们的这个key叫user_json,然后

value就是我们要做匹配的对象,看我们是否可以以JSON的格式存到redis当中,测试通过,我们打开可视化工具,我们可以看到value

是没有问题的,以JSON格式来存储的,这是存放,我们再来看取出

@Test
public void testGetUseJSON(){
	this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
	Users users = (Users)this.redisTemplate.opsForValue().get("users_json");
	System.out.println(users);
}

我们还是先设置一个序列化器,现在不仅仅是序列化器相同的问题了,还有后面的模板也必须相同,返回来的时候

也是User.class,然后去取里面的内容,没有问题的,现在这个对象反序列化回来,他是可以正常输出的,以上就是通过redisTemplate,

设置一个新的序列话化器,来对我们User对象做JSON处理,以及JSON格式的反序列化处理,所以这个操作还是比较简单的,只是最关键的

一点是,序列化器的一个使用而已
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-total=20
#
#aaa.bbb.max-idle=10
#aaa.bbb.min-idle=5
#aaa.bbb.max-total=20

spring.redis.hostName=10.40.8.152
spring.redis.port=6379
spring.redis.password=1234
package com.learn.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;

/**
 * 完成对Redis的整合的一些配置
 *
 *
 */
@Configuration
public class RedisConfig {

	/**
	 * 1.创建JedisPoolConfig对象。在该对象中完成一些链接池配置
	 * @ConfigurationProperties:会将前缀相同的内容创建一个实体。
	 */
	@Bean
	@ConfigurationProperties(prefix="spring.redis.pool")
//	@ConfigurationProperties(prefix="aaa.bbb")
	public JedisPoolConfig jedisPoolConfig(){
		JedisPoolConfig config = new JedisPoolConfig();
		/*//最大空闲数
		config.setMaxIdle(10);
		//最小空闲数
		config.setMinIdle(5);
		//最大链接数
		config.setMaxTotal(20);*/
		System.out.println("默认值:"+config.getMaxIdle());
		System.out.println("默认值:"+config.getMinIdle());
		System.out.println("默认值:"+config.getMaxTotal());
		return config;
	}
	
	/**
	 * 2.创建JedisConnectionFactory:配置redis链接信息
	 */
	@Bean
	@ConfigurationProperties(prefix="spring.redis")
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
		System.out.println("配置完毕:"+config.getMaxIdle());
		System.out.println("配置完毕:"+config.getMinIdle());
		System.out.println("配置完毕:"+config.getMaxTotal());
		
		JedisConnectionFactory factory = new JedisConnectionFactory();
		//关联链接池的配置对象
		factory.setPoolConfig(config);
		//配置链接Redis的信息
		//主机地址
		/*factory.setHostName("10.40.8.152");
		//端口
		factory.setPort(6379);*/
		return factory;
	}
	
	/**
	 * 3.创建RedisTemplate:用于执行Redis操作的方法
	 */
	@Bean
	public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
		RedisTemplate<String, Object> template = new RedisTemplate<>();
		//关联
		template.setConnectionFactory(factory);
		
		//为key设置序列化器
		template.setKeySerializer(new StringRedisSerializer());
		//为value设置序列化器
		template.setValueSerializer(new StringRedisSerializer());
		
		return template;
	}
}
package com.learn.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.learn.RedisApp;
import com.learn.pojo.Users;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=RedisApp.class)
public class RedisJSONSerializationTest {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 基于JSON格式存Users对象
	 */
	@Test
	public void testSetUsersUseJSON(){
		Users users = new Users();
		users.setAge(20);
		users.setName("李四丰");
		users.setId(1);
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
		this.redisTemplate.opsForValue().set("users_json", users);
	}
	
	/**
	 * 基于JSON格式取Users对象
	 */
	@Test
	public void testGetUseJSON(){
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
		Users users = (Users)this.redisTemplate.opsForValue().get("users_json");
		System.out.println(users);
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值