spring data redis
在spring boot项目中,可以使用spring data redis来简化redis操作,maven坐标:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring data redis 中提供了一个高度封装的类:redisTemplate,针对jedis客户端中大量api进行归类封装,将同一类型操作封装为operation接口,具体分类如下:
- ValueOperations:简单K-V操作
- SetOperations:set类型数据操作
- ZSetOperations:zset类型数据操作
- HashOperations:针对map类型的数据操作
- ListOperations:针对list类型的数据操作
1.配置redis配置类
默认的key序列化器为:JdkSerializationRedisSerializer(例如:“\xac\xed\x00\ x05t{x00\x04city"),为了能在redis客户端的key可读性高,建议配置
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
/**
* Redis配置类
*/
@Component
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
//默认的key序列化器为:JdkSerializationRedisSerializer(例如:“\xac\xed\x00\ x05t{x00\x04city")
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
2.操作string类型的数据
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class SpringdataredisTestDemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
/**
* 操作string类型的数据
*/
@Test
void testString() {
redisTemplate.opsForValue().set("city123","重庆");
String value=(String) redisTemplate.opsForValue().get("city123");
System.out.println(value);
//设置超时时间10s
redisTemplate.opsForValue().set("site","扬州",10l, TimeUnit.SECONDS);
//当key不存在的时候进行操作,存在不进行任何操作操作
Boolean aBoolean=redisTemplate.opsForValue().setIfAbsent("city1234","四川");
System.out.println(aBoolean);
}
}
3.操作hash类型数据
@Test
public void testHash(){
HashOperations hashOperations=redisTemplate.opsForHash();
//存值
hashOperations.put("myHash","name","大耳朵图图");
hashOperations.put("myHash","age","3");
hashOperations.put("myHash","city","重庆");
//取值
String age=(String) hashOperations.get("myHash","age");
System.out.println("age"+age);
//获取hash结构中所有字段
System.out.println("获取hash结构中所有字段:");
hashOperations.keys("myHash").forEach((item)->{
System.out.println(item);
});
//获取hash结构中的所有值
System.out.println("获取hash结构中的所有值:");
hashOperations.values("myHash").forEach((item)->{
System.out.println(item);
});
}
4.操作set类型的数据(无序,且不可以重复)
@Test
public void testSet(){
SetOperations setOperations=redisTemplate.opsForSet();
//存值
setOperations.add("mySet","a","b","c","a");
//取值
setOperations.members("mySet").forEach((item)->{
System.out.print(item);
});
//删除成员
setOperations.remove("mySet","a","b");
//取值
System.out.println();
setOperations.members("mySet").forEach((item)->{
System.out.println(item);
});
}
5.list
/**
* 操作list类型数据(可以重复)
*/
@Test
public void testList(){
ListOperations listOperations= redisTemplate.opsForList();
//存值
listOperations.leftPush("myList","a");
listOperations.leftPushAll("myList","d","b","c");
//取值
listOperations.range("myList",0,-1).forEach((item)->{
System.out.println(item);
});
//获得列表的长度
int size=listOperations.size("myList").intValue();
for (int i=0;i<size;i++){
//获取的同时,出队列
String elment=(String) listOperations.rightPop("myList");
System.out.println(elment);
}
}
6.操作ZSet类型的数据
@Test
public void testZSet(){
ZSetOperations zSetOperations=redisTemplate.opsForZSet();
//存值
zSetOperations.add("myZSet","a",10.0);
zSetOperations.add("myZSet","b",11.0);
zSetOperations.add("myZSet","c",9.0);
zSetOperations.add("myZSet","d",12.0);
//取值
System.out.println("第一次");
zSetOperations.range("myZSet",0,-1).forEach((item)->{
System.out.println(item);
});
//修改分数,给b加20分
zSetOperations.incrementScore("myZSet","b",20);
System.out.println("第二次");
zSetOperations.range("myZSet",0,-1).forEach((item)->{
System.out.println(item);
});
//删除成员
zSetOperations.remove("myZSet","a","c");
//取值
System.out.println("第三次");
zSetOperations.range("myZSet",0,-1).forEach((item)->{
System.out.println(item);
});
}
7.通用操作
@Test
public void testCommon(){
//获取Redis中所有的key
System.out.println("获取Redis中所有的key:");
redisTemplate.keys("*").forEach((key)->{
System.out.println(key);
});
//判断某个key是否存在
Boolean result=redisTemplate.hasKey("myZSet");
System.out.println("判断某个key是否存在:"+result); //如果存在输出true,如果不存在输出false
//删除指定key
Boolean key= redisTemplate.delete("city");
System.out.println("删除指定key:"+key);
//获取指定key对应的value的数据类型
DataType dataType=redisTemplate.type("mySet");
System.out.println("获取指定key对应的value的数据类型:"+dataType);
}
}
版权归原作者 哐当kuangdang 所有, 如有侵权,请联系我们删除。