RedisTemplate的基本使用方法

1,228次阅读
没有评论

1、pom.xml依赖

org.springframework.boot spring-boot-starter-data-redis

AI助手
2、配置文件

Redis服务器连接端口

spring.redis.port=6379

Redis服务器地址

spring.redis.host=127.0.0.1

或者

Redis集群

spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381

Redis最大重定向(默认为5)

spring.redis.cluster.max-redirects=5

Redis数据库索引(默认为0)

spring.redis.database=0

Redis服务器连接密码(默认为空)

spring.redis.password=

连接池最大连接数(使用负值表示没有限制)

spring.redis.jedis.pool.max-active=8

连接池最大阻塞等待时间(使用负值表示没有限制)

spring.redis.jedis.pool.max-wait=-1ms

连接池中的最大空闲连接

spring.redis.jedis.pool.max-idle=8

连接池中的最小空闲连接

spring.redis.jedis.pool.min-idle=0

连接超时时间(毫秒)

spring.redis.timeout=5000ms

AI助手
3、RedisTemplate的基本方法
首先使用@Autowired注入RedisTemplate(后面直接使用,就不特殊说明)

//导入redisTemplate
@Autowired
private RedisTemplate redisTemplate;

AI助手
1.删除单个key

// 删除单个key
public void delete(String key){
redisTemplate.delete(key);
}

AI助手
2.删除多个key

// 删除多个key
public void deleteKeys (String …keys){
redisTemplate.delete(keys);
}

AI助手
3.指定key的过期时间

// 指定key的失效时间
public void setExpire(String key,long time){
redisTemplate.expire(key, time, TimeUnit.MINUTES);
}

AI助手
4.获取key的过期时间

// 根据key获取过期时间
public long getExpire(String key){
Long expire = redisTemplate.getExpire(key);
return expire;
}

AI助手
5.判断key是否存在

// 判断key是否存在
public boolean hasKey(String key){
return redisTemplate.hasKey(key);
}

AI助手
4、String类型相关操作
1.添加缓存

//1、通过BoundValueOperations设置值
redisTemplate.boundValueOps(“StringKey”).set(“StringValue”);
redisTemplate.boundValueOps(“StringKey”).set(“StringValue”,1, TimeUnit.MINUTES);

//2、通过ValueOperations设置值
redisTemplate.opsForValue().set(“StringKey”, “StringVaule”);
redisTemplate.opsForValue().set(“StringValue”,”StringVaule”,1, TimeUnit.MINUTES);

AI助手
2.设置过期时间(单独设置)

redisTemplate.boundValueOps(“StringKey”).expire(1,TimeUnit.MINUTES);

redisTemplate.expire(“StringKey”,1,TimeUnit.MINUTES);

AI助手
3.获取缓存值

//1、通过BoundValueOperations获取值
String StringVaule= redisTemplate.boundValueOps(“StringKey”).get();

//2、通过ValueOperations获取值
String StringVaule= redisTemplate.opsForValue().get(“StringKey”);

AI助手
4.顺序递增

//每次自增3
redisTemplate.boundValueOps(“StringKey”).increment(3L);
//每次自增5
redisTemplate.opsForValue().increment(“StringKey”,5);
//每次自增1
redisTemplate.opsForValue().increment(“StringKey”);

AI助手
5.顺序递减

redisTemplate.boundValueOps(“StringKey”).increment(-3L);
redisTemplate.opsForValue().increment(“StringKey”,-5);

AI助手
5、Hash类型相关操作
1.添加缓存

//1、通过BoundValueOperations设置值
BoundHashOperations hashKey = redisTemplate.boundHashOps(“HashKey”);
hashKey.put(“SmallKey”, “HashVaue”);

//2、通过ValueOperations设置值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put(“HashKey”, “SmallKey”, “HashVaue”);

AI助手
2.设置过期时间(单独设置)

redisTemplate.boundValueOps(“HashKey”).expire(1,TimeUnit.MINUTES);

redisTemplate.expire(“HashKey”,1,TimeUnit.MINUTES);

AI助手
3.添加一个Map集合

HashMap hashMap = new HashMap<>();
redisTemplate.boundHashOps(“HashKey”).putAll(hashMap );

AI助手
4.获取所有key

//1、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps(“HashKey”);
Set keys1 = hashKey.keys();

//2、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Set keys2 = hashOps.keys(“HashKey”);

AI助手
5.获取所有value

//1、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps(“HashKey”);
List values1 = hashKey.values();

//2、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
List values2 = hashOps.values(“HashKey”);

AI助手
6.根据key获取value值

//1、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps(“HashKey”);
String value1 = hashKey.get(“SmallKey”);

//2、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
String value2 = hashOps.get(“HashKey”, “SmallKey”);

AI助手
7.获取所有的键值对集合

//1、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps(“HashKey”);
Map entries1 = hashKey.entries();

//2、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Map entries2 = hashOps.entries(“HashKey”);

AI助手
8.删除

//删除小key
redisTemplate.boundHashOps(“HashKey”).delete(“SmallKey”);
//删除大key
redisTemplate.delete(“HashKey”);

AI助手
9.判断Hash中是否含有该值

Boolean isEmpty = redisTemplate.boundHashOps(“HashKey”).hasKey(“SmallKey”);

Boolean isEmpty = redisTemplate.opsForHash().hasKey(“HashKey”,”SmallKey”);

AI助手
6、Set类型相关操作
1.添加Set缓存(值可以是一个,也可是多个)(2/3是1的递进值)

//1、通过BoundValueOperations设置值
BoundSetOperations setKey = redisTemplate.boundSetOps(“setKey”);
setKey.add(“setValue1”, “setValue2”, “setValue3”);

//2、通过ValueOperations设置值
SetOperations setOps = redisTemplate.opsForSet();
setOps.add(“setKey”, “SetValue1”, “setValue2”, “setValue3”);

AI助手
2.设置过期时间(单独设置)

redisTemplate.boundValueOps(“setKey”).expire(1,TimeUnit.MINUTES);

redisTemplate.expire(“setKey”,1,TimeUnit.MINUTES);

AI助手
3.根据key获取Set中的所有值

//1、通过BoundValueOperations获取值
BoundSetOperations setKey = redisTemplate.boundSetOps(“setKey”);
Set set1 = setKey.members();

//2、通过ValueOperations获取值
SetOperations setOps = redisTemplate.opsForSet();
Set set2 = setOps.members(“setKey”);

AI助手
4.根据value从一个set中查询,是否存在

Boolean isEmpty = redisTemplate.boundSetOps(“setKey”).isMember(“setValue2”);

AI助手
5.获取Set缓存的长度

Long size = redisTemplate.boundSetOps(“setKey”).size();

AI助手
6.移除指定的元素

Long result1 = redisTemplate.boundSetOps(“setKey”).remove(“setValue1”);

AI助手
7.移除指定的key

Boolean result2 = redisTemplate.delete(“setKey”);

AI助手
7、 LIST类型相关操作
1.添加缓存

//1、通过BoundValueOperations设置值
BoundListOperations listKey = redisTemplate.boundListOps(“listKey”);
listKey.leftPush(“listLeftValue3”);
listKey.rightPush(“listRightValue4”);

//2、通过ValueOperations设置值
ListOperations opsList = redisTemplate.opsForList();
opsList.leftPush(“listKey”, “listLeftValue5”);
opsList.rightPush(“listKey”, “listRightValue6”);

AI助手
2.将List放入缓存

ArrayList list = new ArrayList<>();
redisTemplate.boundListOps(“listKey”).rightPushAll(list);
redisTemplate.boundListOps(“listKey”).leftPushAll(list);

AI助手
3.设置过期时间(单独设置)

redisTemplate.boundValueOps(“listKey”).expire(1,TimeUnit.MINUTES);

redisTemplate.expire(“listKey”,1,TimeUnit.MINUTES);

AI助手
4.获取List缓存全部内容(起始索引,结束索引)

List listKey1 = redisTemplate.boundListOps(“listKey”).range(0, 10);

AI助手
5.从左或从右弹出一个元素

String listKey2 = (String) redisTemplate.boundListOps(“listKey”).leftPop(); //从左侧弹出一个元素
String listKey3 = (String) redisTemplate.boundListOps(“listKey”).rightPop(); //从右侧弹出一个元素

AI助手
6.根据索引查询元素

String listKey4 = (String) redisTemplate.boundListOps(“listKey”).index(1);

AI助手
7.获取List缓存的长度

Long size = redisTemplate.boundListOps(“listKey”).size();

AI助手
8.根据索引修改List中的某条数据(key,索引,值)

redisTemplate.boundListOps(“listKey”).set(3L,”listLeftValue3″);

AI助手
9.移除N个值为value(key,移除个数,值)

redisTemplate.boundListOps(“listKey”).remove(3L,”value”);

AI助手
8、Zset类型的相关操作
1.向集合中插入元素,并设置分数

//1、通过BoundValueOperations设置值
BoundZSetOperations zSetKey = redisTemplate.boundZSetOps(“zSetKey”);
zSetKey.add(“zSetVaule”, 100D);

//2、通过ValueOperations设置值
ZSetOperations zSetOps = redisTemplate.opsForZSet();
zSetOps.add(“zSetKey”, “zSetVaule”, 100D);

AI助手
2.向集合中插入多个元素,并设置分数

DefaultTypedTuple p1 = new DefaultTypedTuple<>(“zSetVaule1”, 2.1D);
DefaultTypedTuple p2 = new DefaultTypedTuple<>(“zSetVaule2”, 3.3D);
redisTemplate.boundZSetOps(“zSetKey”).add(new HashSet<>(Arrays.asList(p1,p2)));

AI助手
3.按照排名先后(从小到大)打印指定区间内的元素, -1为打印全部

Set range = redisTemplate.boundZSetOps(“zSetKey”).range(key, 0, -1);

AI助手
4.获得指定元素的分数

Double score = redisTemplate.boundZSetOps(“zSetKey”).score(“zSetVaule”);

AI助手
5.返回集合内的成员个数

Long size = redisTemplate.boundZSetOps(“zSetKey”).size();

AI助手
6.返回集合内指定分数范围的成员个数(Double类型)

Long COUNT = redisTemplate.boundZSetOps(“zSetKey”).count(0D, 2.2D);

AI助手
7.返回集合内元素在指定分数范围内的排名(从小到大)

Set byScore = redisTemplate.boundZSetOps(“zSetKey”).rangeByScore(0D, 2.2D);

AI助手
8.带偏移量和个数,(key,起始分数,最大分数,偏移量,个数)

Set ranking2 = redisTemplate.opsForZSet().rangeByScore(“zSetKey”, 0D, 2.2D 1, 3);

AI助手
9.返回集合内元素的排名,以及分数(从小到大)

Set> tuples = redisTemplate.boundZSetOps(“zSetKey”).rangeWithScores(0L, 3L);
for (TypedTuple tuple : tuples) {
System.out.println(tuple.getValue() + ” : ” + tuple.getScore());
}

AI助手
10.返回指定成员的排名

//从小到大
Long startRank = redisTemplate.boundZSetOps(“zSetKey”).rank(“zSetVaule”);
//从大到小
Long endRank = redisTemplate.boundZSetOps(“zSetKey”).reverseRank(“zSetVaule”);

AI助手
11.从集合中删除指定元素

redisTemplate.boundZSetOps(“zSetKey”).remove(“zSetVaule”);

AI助手
12.删除指定索引范围的元素(Long类型)

redisTemplate.boundZSetOps(“zSetKey”).removeRange(0L,3L);

AI助手
13.删除指定分数范围内的元素(Double类型)

redisTemplate.boundZSetOps(“zSetKey”).removeRangeByScorssse(0D,2.2D);

AI助手
14.为指定元素加分(Double类型)

Double score = redisTemplate.boundZSetOps(“zSetKey”).incrementScore(“zSetVaule”,1.1D);

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2024 年 12 月
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
手把手教你用AI 10分钟生成一个APP!零基础也能搞定

手把手教你用AI 10分钟生成一个APP!零基础也能搞定

今日,我将向大家展示DeepSeek的全新玩法——从零开始,利用AI创建一个完整的应用程序。借助DeepSee...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
手把手教你用AI 10分钟生成一个APP!零基础也能搞定

手把手教你用AI 10分钟生成一个APP!零基础也能搞定

今日,我将向大家展示DeepSeek的全新玩法——从零开始,利用AI创建一个完整的应用程序。借助DeepSee...