学习怎么在Spring boot中使用redis
第一步、引入redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第二步、配置redis
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=123456
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=30000
第三步、如何使用redis操作
当添加Redis依赖后,SpringBoot会自动帮我们在容器中生成一个RedisTemplate和一个StringRedisTemplate,但是,这个RedisTemplate的泛型是<Object,Object>。
如何使用StringRedisTemplate
package com.study.string;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
import org.springframework.data.redis.core.ClusterOperations;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
@Component
public class StirngRedisTemplateDemo {
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 测试常用数据结构和操作
* @描述:
* @作者:严磊
* @时间:2020年11月25日 下午11:11:15
*/
public void commonDataStructures(){
//String 类型
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
opsForValue.set("key.string", "6666");
//hash 类型
HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
opsForHash.put("key.hash", "key1", "1");
opsForHash.put("key.hash", "key2", "2");
//list 类型
ListOperations<String, String> opsForList = redisTemplate.opsForList();
opsForList.leftPush("key.list", "1");
opsForList.leftPush("key.list", "2");
opsForList.leftPush("key.list", "3");
//set 类型
SetOperations<String, String> opsForSet = redisTemplate.opsForSet();
opsForSet.add("key.set", "1");
opsForSet.add("key.set", "2");
opsForSet.add("key.set", "3");
opsForSet.add("key.set", "1");
//zset 类型
ZSetOperations<String, String> opsForZSet = redisTemplate.opsForZSet();
opsForZSet.add("key.zset", "value1", 1);
opsForZSet.add("key.zset", "value2", 2);
opsForZSet.add("key.zset", "value3", 3);
opsForZSet.add("key.zset", "value4", 4);
//常用操作
Boolean delete = redisTemplate.delete("key.string");
System.out.println("delete:"+delete);
Boolean hasKey = redisTemplate.hasKey("key.string");
System.out.println("hasKey:"+hasKey);
Set<String> keys = redisTemplate.keys("*key*");
System.out.println("keys:"+keys);
Boolean expire = redisTemplate.expire("key.string", 100L, TimeUnit.SECONDS);
System.out.println("expire:"+expire);
}
}
如何使用RedisTemplate
首先,配置RedisTemplate,我们需要配置一个泛型为<Stirng,Object>的RedisTemplate。并设置序列化的方式。
package com.study.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
然后我们就可以对数据库进行操作了
package com.study.object;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ClusterOperations;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
@Component
public class RedisTemplateDemo {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void methodTest() {
User user1 = new User("小红", 1);
User user2 = new User("小明", 2);
// String 类型
ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
opsForValue.set("key.string", user2);
System.out.println(opsForValue.get("key.string"));
// hash 类型
HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
opsForHash.put("key.hash", "小红", user1);
opsForHash.put("key.hash", "小明", user2);
System.out.println(opsForHash.get("key.hash", "小红"));
System.out.println(opsForHash.get("key.hash", "小明"));
// list 类型
ListOperations<String, Object> opsForList = redisTemplate.opsForList();
opsForList.leftPush("key.list", "1");
opsForList.leftPush("key.list", "2");
opsForList.leftPush("key.list", "3");
// set 类型
SetOperations<String, Object> opsForSet = redisTemplate.opsForSet();
opsForSet.add("key.set", "1");
opsForSet.add("key.set", "2");
opsForSet.add("key.set", "3");
opsForSet.add("key.set", "1");
// zset 类型
ZSetOperations<String, Object> opsForZSet = redisTemplate.opsForZSet();
opsForZSet.add("key.zset", "value1", 1);
opsForZSet.add("key.zset", "value2", 2);
opsForZSet.add("key.zset", "value3", 3);
opsForZSet.add("key.zset", "value4", 4);
//常用操作
Boolean delete = redisTemplate.delete("key.string");
System.out.println("delete:" + delete);
Boolean hasKey = redisTemplate.hasKey("key.string");
System.out.println("hasKey:" + hasKey);
Set<String> keys = redisTemplate.keys("*key*");
System.out.println("keys:" + keys);
Boolean expire = redisTemplate.expire("key.string", 100L, TimeUnit.SECONDS);
System.out.println("expire:" + expire);
//其他数据类型
GeoOperations<String, Object> opsForGeo = redisTemplate.opsForGeo();
HyperLogLogOperations<String, Object> opsForHyperLogLog = redisTemplate.opsForHyperLogLog();
StreamOperations<String, Object, Object> opsForStream = redisTemplate.opsForStream();
ClusterOperations<String, Object> opsForCluster = redisTemplate.opsForCluster();
// redisTemplate.opsForStream(hashMapper);
}
static class User implements Serializable{
private static final long serialVersionUID = 1898398234L;
private String name;
private Integer age;
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
常用方法整理
跟数据类型无关的操作直接通过redisTemplate对象即可执行。若和数据结构类型有关,则需要使用opsFor…返回对应的操作对象来执行。
方法 | 对应redis中的数据类型 |
---|---|
redisTemplate.opsForValue(); | string 类型 |
redisTemplate.opsForHash() | hash 类型 |
redisTemplate.opsForList(); | list 类型 |
redisTemplate.opsForSet(); | set 类型 |
redisTemplate.opsForZSet(); | zset 类型 |
其他的一些常用操作:
//删除
Boolean delete = redisTemplate.delete(“key.string”);
//检查key是否存在
Boolean hasKey = redisTemplate.hasKey(“key.string”);
//模糊查询key
Set keys = redisTemplate.keys(“key”);
//设置过期时间
Boolean expire = redisTemplate.expire(“key.string”, 100L, TimeUnit.SECONDS);