SpringBoot学习4.1-springboot集成Redis

1.maven坐标

<!--依赖Redis -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
	<exclusions>
		<!--不依赖Redis的异步客户端lettuce -->
		<exclusion>
			<groupId>io.lettuce</groupId>
			<artifactId>lettuce-core</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!--引入Redis的客户端驱动jedis -->
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

客户端驱动:一般使用jedis,故将默认的驱动lettuce排除。

2.redis连接服务器配置

#redis配置连接池属性
spring.redis.jedis.pool.min-idle=5
spring.redis.jedis.pool.max-active=10
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-wait=2000
#redis配置服务器属性
spring.redis.port=6379
spring.redis.host=localhost
spring.redis.password=123456
#redis配置连接超时,毫秒
spring.redis.timeout=2000

3.使用RedisTemplate操作Redis

package com.zyf.springTrans.redisconnection;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisService {

	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	public void redisOperate() {
		redisTemplate.opsForValue().set("key1", "value1");
		Object value1 = redisTemplate.opsForValue().get("key1");
		System.out.println("key1在redis中对应的值=" + value1);
		Set<Object> sts = redisTemplate.keys("*");
		System.out.println(sts);
		// 删除,根据key
		redisTemplate.delete("key1");
		// 删除,根据key组成的集合
		redisTemplate.delete(sts);
	}
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	public void stringRedisOperate() {
		stringRedisTemplate.opsForValue().set("key11", "value11");
		Object value11 = stringRedisTemplate.opsForValue().get("key11");
		System.out.println("key11在redis中对应的值=" + value11);
		Set<String> sts = stringRedisTemplate.keys("*");
		System.out.println(sts);
	}
}

结果发现 RedisTemplate存入的key1在Redis中的key如下:

这样不好识别,需要给RedisTemplate增加序列化器。

StringRedisTemplate操作不会有上述问题。

4.RedisTemplate增加序列化器

package com.zyf.springTrans.redisconfig;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**redis配置类 */
@Configuration
public class RedisConfig {

	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	/** 修改RedisTemplate的序列化器:用字符串存储key,避免乱码*/
	@PostConstruct // 自定义后初始化方法,在加载完bean到IoC后执行
	public void initRedisSerializer() {
		RedisSerializer<String> stringRedisSerializer = redisTemplate.getStringSerializer();
		redisTemplate.setKeySerializer(stringRedisSerializer);
		redisTemplate.setHashKeySerializer(stringRedisSerializer);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值