RedisTemplate测试类
本文最后更新于97 天前,其中的信息可能已经过时,如有错误请发送邮件到z18096561915@outlook.com
package com.sky.test;

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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
* redisTemplate测试类
*/
//@SpringBootTest
public class SpringDateRedisTest {
// @Autowired
private RedisTemplate redisTemplate;


/**
* 测试RedisTemplate
*/
@Test
public void testRedisTemplate() {
ValueOperations valueOperations = redisTemplate.opsForValue();
HashOperations hashOperations = redisTemplate.opsForHash();
ListOperations listOperations = redisTemplate.opsForList();
SetOperations setOperations = redisTemplate.opsForSet();
ZSetOperations zSetOperations = redisTemplate.opsForZSet();

}

/**
* 测试String类型
*/
@Test
public void testString() {
//set get setex setnx
redisTemplate.opsForValue().set("city", "beijing");
String city = (String) redisTemplate.opsForValue().get("city");
System.out.println(city);
redisTemplate.opsForValue().set("code", "12345", 3, TimeUnit.MINUTES);
redisTemplate.opsForValue().setIfAbsent("lock", "1");
redisTemplate.opsForValue().setIfAbsent("lock", "2");
}

/**
* 测试Hash类型
*/

@Test
public void testHash() {
//put get putAll entries
redisTemplate.opsForHash().put("user", "name", "zhangsan");
redisTemplate.opsForHash().put("user", "age", "20");
Object name = redisTemplate.opsForHash().get("user", "name");
System.out.println(name);

Set keys = redisTemplate.opsForHash().keys("user");
System.out.println(keys);

List user = redisTemplate.opsForHash().values("user");
System.out.println(user);

redisTemplate.opsForHash().delete("user", "age");
}

/**
* 测试List类型
*/
@Test
public void testList() {
//leftPush rightPush leftPop rightPop size range
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("list", "1", "2", "3");
listOperations.leftPush("list", "4");

List mylist = listOperations.range("list", 0, -1);
System.out.println(mylist);
listOperations.rightPop("list");
Long list = listOperations.size("list");

System.out.println(list);
}

/**
* 测试Set类型
*/
@Test
public void testSet() {
//add remove size members
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1", "a", "b", "c");
setOperations.add("set2", "a", "b", "x");


Set set1 = setOperations.members("set1");
System.out.println(set1);
setOperations.size("set1");
Set union = setOperations.union("set1", "set2");

System.out.println(union);
setOperations.remove("set1", "a");

}

/**
* 测试ZSet类型
*/

@Test
public void testZSet() {
//zadd zincrby zrange zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset1", "a", 1);
zSetOperations.add("zset1", "b", 2);
zSetOperations.add("zset1", "c", 3);

Set zset1 = zSetOperations.range("zset1", 0, -1);
System.out.println(zset1);
zSetOperations.incrementScore("zset1", "a", 10);
zSetOperations.remove("zset1", "b");

}

/**
* 通用命令操作
*/
@Test
public void testCommon(){
//keys exists type del
Set keys = redisTemplate.keys("*");
System.out.println(keys);
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("set1");

for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(key);
}
redisTemplate.delete("mylist");
}
}
觉得有帮助可以投喂下博主哦~感谢!
作者:JoyBoy
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0协议
转载请注明文章地址及作者哦~
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇