前面简单学习了一下redis的数据类型,这次我们来学习一下如何在SpringBoot中使用实践
开始
环境:JDK1.8,Maven3+,Redis3
需要预先安装好Redis
新建一个SpringBoot项目,引入需要用到的相关maven依赖
<!--SpringBoot的Redis支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--SpringBoot缓存支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
再yml文件中配置redis连接
spring:
redis:
#Redis服务器地址,默认localhost
host: localhost
#Redis服务器端口,默认6379
port: 6379
pool:
#连接池最大连接数
max-active: 8
#最大阻塞等待时间,-1表示没有限制
max-wait: -1
#最大空闲连接
max-idle: 8
#最小空闲连接
min-idle: 0
#连接超时时间
timeout: 0
使用RedisTemplate操作Redis
spring-data-redis提供了一个RedisTemplate类,这个类封装了对Redis基本数据结构的常用操作,它的子类StringRedisTemplate提供了对字符串的常用
操作,接下来将使用StringRedisTemplate来操作Redis中的String和List类型。
注入StringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 操作字符串
*/
private void operateString() {
stringRedisTemplate.opsForValue().set("author", "luoliang");
String value = stringRedisTemplate.opsForValue().get("author");
log.info("stringRedisTemplate输出值:{}", value);
}
/**
* Redis List操作,Redis列表是简单的字符串列表,按照插入顺序排序。可以添加一个元素到列表的头部(左边)或者尾部(右边)
*/
private void operateList() {
String key = "website";
ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
//从左压入栈
listOperations.leftPush(key, "Github");
listOperations.leftPush(key, "CSDN");
//从右压入栈
listOperations.rightPush(key, "SegmentFault");
log.info("list size:{}", listOperations.size(key));
List<String> list = listOperations.range(key, 0, 2);
list.forEach(log::info);
}
上面涉及到的两种类型的操作,都是针对的字符串,可不可以存取对象呢?答案当然是可以的。我们使用Hash来存取对象,首先新建一个User类,用于存取
@Data
@Builder
public class User implements Serializable {
private String id;
private String name;
private Integer age;
}
这时候就不能再使用StringRedisTemplate了,所以需要配置针对Object的RedisTemplate实例,这里可以使用默认的JdkSerializationRedisSerializer序列化,也可以自己实现RedisSerializer接口来自定义序列化
创建实例
@Configuration
public class RedisConfig {
@Resource
private JedisConnectionFactory jedisConnectionFactory;
@Bean
public RedisTemplate<String, Object> objRedisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
return template;
}
}
操作Hash
@Resource
private RedisTemplate<String, Object> objRedisTemplate;
/**
* 操作hash,存放User对象
*/
private void operateHash() {
String key = "user";
HashOperations<String, String, User> hashOperations = objRedisTemplate.opsForHash();
hashOperations.put(key, "user1", User.builder().name("Hulk").age(50).build());
hashOperations.put(key, "user2", User.builder().name("Thor").age(1500).build());
hashOperations.put(key, "user3", User.builder().name("Rogers").age(150).build());
log.info("hash size:{}", hashOperations.size(key));
log.info("--------拿到Map的key集合--------");
Set<String> keys = hashOperations.keys(key);
keys.forEach(log::info);
log.info("--------拿到Map的value集合--------");
List<User> users = hashOperations.values(key);
users.forEach(user -> log.info(user.toString()));
log.info("--------拿到user1的value--------");
User user = hashOperations.get(key, "user1");
log.info(user.toString());
}
最后,验证我们的操作,可以看到,结果和预期相同。
2019-07-12 10:45:07.754 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : ----------Operate String----------
2019-07-12 10:45:07.820 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : stringRedisTemplate输出值:luoliang
2019-07-12 10:45:07.821 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : ----------Operate List----------
2019-07-12 10:45:07.832 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : list size:57
2019-07-12 10:45:07.836 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : CSDN
2019-07-12 10:45:07.836 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : Github
2019-07-12 10:45:07.836 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : CSDN
2019-07-12 10:45:07.836 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : ----------Operate Hash----------
2019-07-12 10:45:07.858 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : hash size:3
2019-07-12 10:45:07.858 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : --------拿到Map的key集合--------
2019-07-12 10:45:07.865 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : user2
2019-07-12 10:45:07.866 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : user1
2019-07-12 10:45:07.866 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : user3
2019-07-12 10:45:07.866 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : --------拿到Map的value集合--------
2019-07-12 10:45:07.870 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : User(id=null, name=Thor, age=1500)
2019-07-12 10:45:07.870 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : User(id=null, name=Hulk, age=50)
2019-07-12 10:45:07.870 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : User(id=null, name=Rogers, age=150)
2019-07-12 10:45:07.870 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : --------拿到user1的value--------
2019-07-12 10:45:07.873 INFO 42127 --- [ main] org.boot.redis.BootRedisApplication : User(id=null, name=Hulk, age=50)
到这里基本上就可以上手使用redis了,下一篇研究一下 使用Annotation缓存数据。