环境搭建
在pom.xml中导入Spring Data Redis的maven坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
在application-dec.yml中添加配置信息
sky:
redis:
host: localhost
port: 6379
database: 2
在application.yml中添加读取application-dev.yml中的相关Redis配置
spring:
profiles:
active: dev
redis:
host: ${sky.redis.host}
port: ${sky.redis.port}
database: ${sky.redis.database}
创建RedisTemplate对象
import lombok.extern.slf4j.Slf4j;
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.StringRedisSerializer;
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("开始创建redis模板对象...");
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis key、value的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
//value值支持中文正常显示
redisTemplate.setValueSerializer(new FastJsonRedisSerializer<Object>(Object.class));
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
解释说明:
当前配置类不是必须的,因为 Spring Boot 框架会自动装配 RedisTemplate 对象,但是默认的key序列化器为
JdkSerializationRedisSerializer,导致我们存到Redis中后的数据和原始数据有差别,故设置为StringRedisSerializer序列化器。
RedisTemplate默认使用JDK的序列化机制,因此从程序中输入的字符串经过序列化看起来像是乱码;而StringRedisTemplate使用了String的序列化机制,因此有一种所见即所得的效果。
操作常见类型数据
操作字符串类型的数据
/**
* 操作字符串类型的数据
*/
@Test
public void testString(){
redisTemplate.opsForValue().set("name","小明");//设置值
String city =(String) redisTemplate.opsForValue().get("name");//获取值
System.out.println(city);
redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);//设置过期时间
redisTemplate.opsForValue().setIfAbsent("lock","1");//设置锁
redisTemplate.opsForValue().setIfAbsent("lock","2");//设置锁
}
操作哈希类型的数据
/**
* 操作哈希类型的数据
*/
@Test
public void testHash(){
//hset hget hdel hkeys hvals
HashOperations hashOperations = redisTemplate.opsForHash();//获取哈希操作对象
hashOperations.put("100","name","tom");//设置值
hashOperations.put("100","age","18");//设置值
String name = (String) hashOperations.get("100", "name");
System.out.println(name);
Set keys = hashOperations.keys("100");//获取所有的key
System.out.println(keys);
List values = hashOperations.values("100");//获取所有的value
System.out.println(values);
hashOperations.delete("100","age");
}
操作列表类型数据
/**
* 操作列表类型的数据
*/
@Test
public void testList(){
//lpush lrange rpop llen
ListOperations listOperations = redisTemplate.opsForList();//获取列表操作对象
listOperations.leftPushAll("mylist","a","b","c");//添加数据
listOperations.leftPush("mylist","d");//添加数据
List mylist = listOperations.range("mylist",0,-1);//获取所有数据
System.out.println(mylist);
listOperations.rightPop("mylist");//移除最后一个元素并返回
Long size = listOperations.size("mylist");//获取列表长度
System.out.println(size);
}
操作集合类型的数据
/**
* 操作集合类型的数据
*/
@Test
public void testSet(){
// sadd smembers scard sinter sunion srem
SetOperations setOperations = redisTemplate.opsForSet();//获取集合操作对象
setOperations.add("set1","a","b","c","d");//添加数据
setOperations.add("set2","a","b","x","y");//添加数据
Set members = setOperations.members("set1");//获取所有数据
System.out.println(members);
Long size = setOperations.size("set1");//获取集合长度
System.out.println(size);
Set intersect = setOperations.intersect("set1","set2");//求交集
System.out.println(intersect);
Set union = setOperations.union("set1","set2");//求并集
System.out.println(union);
setOperations.remove("set1","a","b");//移除数据
}
操作有序集合类型数据
/**
* 操作有序集合类型的数据
*/
@Test
public void testZSet(){
//zadd zrange zincrby zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();//获取有序集合操作对象
zSetOperations.add("zset1","a",10);//添加数据,zset1是集合名,a是元素,10是分数
zSetOperations.add("zset1","b",12);//添加数据,zset1是集合名,b是元素,12是分数
zSetOperations.add("zset1","c",9);//添加数据,zset1是集合名,c是元素,9是分数
Set zset1 = zSetOperations.range("zset1",0,-1);//获取所有数据
System.out.println(zset1);
zSetOperations.incrementScore("zset1","c",10);//分数加10
Set zset2 = zSetOperations.range("zset1",0,-1);//获取所有数据
System.out.println(zset2);
zSetOperations.remove("zset1","a","b");//移除数据
}
通用命令操作
/**
* 通用命令操作
*/
@Test
public void testCommon(){
// keys exists type del
Set keys = redisTemplate.keys("*");//获取所有的key
System.out.println(keys);
ListOperations listOperations = redisTemplate.opsForList();
Long size = listOperations.size("mylist");
System.out.println(size);
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("mylist");
System.out.println(name.toString()+set1);
for (Object key : keys){
DataType type = redisTemplate.type(key);
System.out.println(type.name());
}
redisTemplate.delete("mylist");
}
总结
//获取字符串类型操作变量
ValueOperations valueOperations = redisTemplate.opsForValue();
//获取哈希操作对象
HashOperations hashOperations = redisTemplate.opsForHash();
//获取列表操作对象
ListOperations listOperations = redisTemplate.opsForList();
//获取集合操作对象
SetOperations setOperations = redisTemplate.opsForSet();
//获取有序集合操作对象
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//----------------------------------------------------------
//字符串类型设置值
valueOperations.set("name","小明");
//字符串类型获取值
String city =(String) valueOperations.get("name");
//字符串类型设置过期时间
valueOperations.set("code","1234",3, TimeUnit.MINUTES);
//字符串类型设置锁
valueOperations.setIfAbsent("lock","1");
//---------------------------------------------------------
//哈希类型设置值
hashOperations.put("100","name","tom");
//哈希类型获取值
String name = (String) hashOperations.get("100", "name");
//哈希类型获取所有的key
Set keys = hashOperations.keys("100");
//哈希类型获取所有的value
List values = hashOperations.values("100");
//哈希类型移除数据
hashOperations.delete("100","age");
//---------------------------------------------------------
//列表类型添加数据
listOperations.leftPushAll("mylist","a","b","c");
listOperations.leftPush("mylist","d");
//列表类型获取所有数据
List mylist = listOperations.range("mylist",0,-1);
//列表类型移除最后一个元素并返回
listOperations.rightPop("mylist");
//列表类型移除获取列表长度
Long size = listOperations.size("mylist");
//---------------------------------------------------------
//集合类型添加数据
setOperations.add("set1","a","b","c","d");
//集合类型获取所有数据
Set members = setOperations.members("set1");
//集合类型获取集合长度
Long size = setOperations.size("set1");
//集合类型求交集
Set union = setOperations.union("set1","set2");
//集合类型移除数据
setOperations.remove("set1","a","b");
//---------------------------------------------------------
//有序集合类型添加数据
zSetOperations.add("zset1","a",10);
//有序集合类型获取所有数据
Set zset1 = zSetOperations.range("zset1",0,-1);
/有序集合类型分数加10
zSetOperations.incrementScore("zset1","c",10);
//有序集合类型获取所有数据
et zset2 = zSetOperations.range("zset1",0,-1);
//有序集合类型移除数据
zSetOperations.remove("zset1","a","b");