简单测试步骤
1、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
我安装的是2.6.3的版本,该版本下默认的使用lettuce,而不是jedis,若想使用jedis,需要自己额外进行引用
2、配置文件
spring:
redis:
host: 127.0.0.1
port: 6379
password:
3、测试
package com.walker.utils;
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.core.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class RedisUtilsTest {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
public void testString(){
ValueOperations<String, String> stringOps = redisTemplate.opsForValue();
stringOps.set("name","hello");
System.out.println(stringOps.get("name"));
stringOps.set("string:time","1",10, TimeUnit.SECONDS);
System.out.println("获取过期时间:"+redisTemplate.getExpire("string:time",TimeUnit.SECONDS));
stringOps.append("name","11111111111111");
stringOps.set("num","10");
stringOps.decrement("num");
System.out.println(stringOps.get("num"));
stringOps.increment("num");
System.out.println(stringOps.get("num"));
stringOps.set("getAndDelete","11");
System.out.println("获取过期时间:"+redisTemplate.getExpire("string:time",TimeUnit.SECONDS));
System.out.println("获取过期时间:"+redisTemplate.getExpire("string:time",TimeUnit.SECONDS));
System.out.println("该key的长度为:"+stringOps.size("name"));
}
@Test
void testHash(){
HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
String key1="test:hash";
hashOps.put(key1,"name","walker");
hashOps.put(key1,"age","18");
hashOps.put(key1,"school","广药");
System.out.println(hashOps.get(key1, "name"));
System.out.println(hashOps.get(key1, "age"));
System.out.println(hashOps.get(key1, "school"));
hashOps.increment(key1,"age",5);
System.out.println("+5之后的结果"+hashOps.get(key1, "age"));
hashOps.increment(key1,"age",-10);
System.out.println("-10之后的结果:"+hashOps.get(key1, "age"));
hashOps.delete(key1,"school");
System.out.println(hashOps.get(key1, "school"));
Map<Object, Object> entries = hashOps.entries(key1);
System.out.println(entries);
System.out.println("是否存在该key:"+hashOps.hasKey(key1, "age"));
System.out.println("value的长度:"+hashOps.lengthOfValue(key1, "age"));
Set<Object> keys = hashOps.keys(key1);
System.out.println("hash中的key集合"+keys);
List<Object> values = hashOps.values(key1);
for (Object value : values) {
System.out.println(value);
}
System.out.println("获取长度"+hashOps.size(key1));
}
@Test
void testSet(){
SetOperations<String, String> setOps = redisTemplate.opsForSet();
String key="hash:set";
String key2="hash:set2";
String key3="hash:set3";
setOps.add(key,"1","2","3","4","5");
setOps.add(key2,"7","8","9","4","5");
System.out.println("key的所有成员"+setOps.members(key));
System.out.println("key2的所有成员"+setOps.members(key2));
System.out.println("key和key2的差集:"+setOps.difference(key, key2));
System.out.println("key2和key的差集:"+setOps.difference(key2, key));
setOps.differenceAndStore(key,key2,key3);
System.out.println("key3的所有成员"+setOps.members(key3));
System.out.println("随机打印成员"+setOps.distinctRandomMembers(key, 1));
System.out.println("key和key2的交集"+setOps.intersect(key, key2));
System.out.println("2是否是KEY集合的结果:"+setOps.isMember(key, "2"));
setOps.move(key,"5",key3);
System.out.println("key的所有成员"+setOps.members(key));
System.out.println("key3的所有成员"+setOps.members(key3));
System.out.println("key和key2的并集"+setOps.union(key, key2));
System.out.println("长度:"+setOps.size(key));
System.out.println("弹出key2的值:"+setOps.pop(key2));
System.out.println("移除key2中的8,结果为:"+setOps.remove(key2,"8"));
}
@Test
void testList(){
ListOperations<String, String> listOps = redisTemplate.opsForList();
String key="list";
String key2="list2";
listOps.leftPush(key,"0");
listOps.leftPush(key,"1");
listOps.leftPush(key,"2");
listOps.leftPush(key,"3");
listOps.leftPush(key,"2");
System.out.println(listOps.range(key, 0, 10));
System.out.println("长度"+listOps.size(key));
listOps.leftPush(key,"10");
listOps.rightPush(key,"111");
System.out.println(listOps.range(key, 0, 10));
listOps.leftPop(key);
listOps.rightPop(key);
System.out.println(listOps.range(key, 0, 10));
listOps.remove(key,1,"3");
System.out.println(listOps.range(key, 0, 10));
listOps.trim(key,2,4);
System.out.println(listOps.range(key, 0, 10));
}
@Test
public void testZset(){
ZSetOperations<String, String> zsetOps = redisTemplate.opsForZSet();
String key="test:zset";
String key2="test:zset2";
zsetOps.add(key,"小明",5);
zsetOps.add(key,"李四",10);
zsetOps.add(key,"老王",4);
zsetOps.add(key2,"老王",10);
zsetOps.add(key2,"钟老三",77);
System.out.println(zsetOps.range(key, 0, 10));
System.out.println(zsetOps.reverseRange(key, 0, 10));
System.out.println("按照分数排序[从小到大]"+zsetOps.rangeByScore(key, 0, 10));
System.out.println("按照分数排序[从大到小]"+zsetOps.reverseRangeByScore(key, 0, 10));
System.out.println("统计分数范围内的数目:"+zsetOps.count(key, 0, 10));
System.out.println(zsetOps.zCard(key));
System.out.println("key和key2的差集"+zsetOps.difference(key, key2));
System.out.println("分数差集"+zsetOps.differenceWithScores(key, key2));
System.out.println(zsetOps.union(key, key2));
System.out.println(zsetOps.unionWithScores(key, key2));
System.out.println(zsetOps.intersect(key, key2));
System.out.println(zsetOps.intersectWithScores(key, key2));
System.out.println(zsetOps.randomMember(key));
System.out.println(zsetOps.randomMembers(key, 2));
System.out.println(zsetOps.randomMemberWithScore(key));
System.out.println(zsetOps.randomMembersWithScore(key, 2));
System.out.println(zsetOps.size(key));
zsetOps.incrementScore(key,"老王",11);
System.out.println("老王分数:"+zsetOps.score(key, "老王"));
System.out.println(zsetOps.popMax(key));
System.out.println(zsetOps.popMin(key));
}
}
问题
?
1、nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.Object>’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 改成这个就可以使用了
|