SpringDataRedis 基本使用

news/2025/2/19 13:38:03/

1.1 简介

1.1.1 概述

  Spring Data 中有一个成员 Spring Data Redis,他提供了 RedisTemplate 可以在 Spring 应用中更简便的访问 Redis 以及异常处理及序列化,支持发布订阅等操作。

1.2 RedisTemplate 常见 API
  RedisTemplate 针对 jedis 客户端中大量 API 进行了归类封装,将同一类型操作封装为 operation 接口

   ♞ ValueOperations: 简单 string 操作
 ♞ ListOperations:    针对 list 类型的数据操作
 ♞ HashOperations: 针对 hash 即 map 类型的数据操作
 ♞ SetOperations:    set 类型数据操作
 ♞ ZSetOperations:  zset 类型数据操作

☞ 示例


@SpringBootTest
public class RedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void redis() {redisTemplate.opsForValue().set("name", "张三");Object name = redisTemplate.opsForValue().get("name");System.out.println(name);}
}

1.2.2 BoundKeyOperations
  RedisTemplate 提供了对 key 的 bound(绑定) 便捷化操作 API,可以通过 bound 封装指定的 key,然后进行一系列的操作而无须显式的再次指定 Key。

    ♞ BoundValueOperations:  绑定 string 类型的 key
♞ BoundListOperations:      绑定 list 类型的 key
 ♞ BoundHashOperations:   绑定 hash 即 map 类型的 key
 ♞ BoundSetOperations:      绑定 set 类型的 key
♞ BoundZSetOperations:    绑定 zset 类型的 key

☞ 示例


@SpringBootTest
public class RedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void redis() {Object name = redisTemplate.boundValueOps("name").get();System.out.println(name);}
}

1.3 数据操作

1.3.1 通用方法
(通用方法)删除 key
// 删除单个 key,返回布尔值
redisTemplate.delete(K key);// 删除多个 key,返回删除的个数
redisTemplate.delete(Collection<K> keys);
(通用方法)判断 key 是否存在
// 返回布尔值
redisTemplate.hasKey(key);
(通用方法) key 有效时间
// 指定有效时间
redisTemplate.expire(key, time, TimeUnit.MINUTES);// 获取有效时间,返回值单位为秒
redisTemplate.getExpire(key);
1.3.2 操作 string
(string类型)添加数据
// 通过 ValueOperations 设置值
ValueOperations ops = redisTemplate.opsForValue();
// 存入数据
ops.set(key, value);	
// 设置过期时间
ops.set(key, value, time, TimeUnit.SECONDS);	// 通过 BoundValueOperations 设置值
BoundValueOperations key = redisTemplate.boundValueOps(key);
key.set(value);
key.set(value, time, TimeUnit.SECONDS);
 (string类型)获取数据
// 通过 ValueOperations 获取值
redisTemplate.opsForValue().get(key);// 通过 BoundValueOperations 获取值
redisTemplate.boundValueOps(key).get();
1.3.3 操作 list
(list类型) 添加数据
// 通过 ValueOperations 设置值
ListOperations opsList = redisTemplate.opsForList();
opsList.leftPush(listKey, listLeftValue);
opsList.rightPush(listKey, listRightValue);
// 存入集合
opsList.rightPushAll(list);	
opsList.leftPushAll(list);// BoundValueOperations 操作类似
(list类型) 获取数据
// 获取集合中的数据
redisTemplate.boundListOps(listKey).range(startIndex, endindex); // 根据索引获取数据
redisTemplate.boundListOps(listKey).index(index);// 集合长度
redisTemplate.boundListOps(listKey).size();
(list类型) 删除数据
// 从左侧弹出一个元素并返回
redisTemplate.boundListOps(listKey).leftPop();  // 从右侧弹出一个元素并返回
redisTemplate.boundListOps(listKey).rightPop(); // 移出 N 个值为 value 的元素
redisTemplate.boundListOps(listKey).remove(long, value); 
(list类型)修改数据
// 根据索引修改数据
redisTemplate.boundListOps(listKey).set(index, listLeftValue);
1.3.4 hash
(hash类型)添加数据
// 通过 BoundValueOperations 设置值
BoundHashOperations hashKey = redisTemplate.boundHashOps(HashKey);
hashKey.put(key, Vaue);
// 添加一个集合
hashKey.putAll(hashMap); // 通过 ValueOperations 设置值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put(HashKey, key, Vaue);
(hash类型)获取数据
// 获取所有小 key
redisTemplate.boundHashOps(HashKey).keys();// 根据小 key 获取值
redisTemplate.boundHashOps(HashKey)get(key);// 获取所有键值对集合
redisTemplate.boundHashOps(HashKey).entries();
(hash类型)删除数据
// 判断 hash 中是否存在小 key
redisTemplate.boundHashOps(HashKey).hasKey(key);// 根据小 key 删除值
redisTemplate.boundHashOps(HashKey).delete(key);
1.3.5 set
(hash类型) 添加数据
// 通过 BoundValueOperations 设置值
redisTemplate.boundSetOps(setKey).add(setValue1, setValue2, setValue3);// 通过 ValueOperations 设置值
redisTemplate.opsForSet().add(setKey, SetValue1, setValue2, setValue");
(hash类型) 获取数据
// 获取所有值
redisTemplate.boundSetOps(setKey).members();// 获取 set 的长度
redisTemplate.boundSetOps(setKey).size();
(hash类型)删除数据
// 判断 set 中是否存在改值
redisTemplate.boundSetOps(setKey).isMember(setValue);// 移出指定的值
redisTemplate.boundSetOps(setKey).remove(setValue);
1.3.6 zset
(Zset类型) 添加数据
// 通过 BoundValueOperations 设置值
redisTemplate.boundZSetOps(zSetKey).add(zSetVaule, score);// 通过 ValueOperations 设置值
redisTemplate.opsForZSet().add(zSetKey, zSetVaule, score);
(Zset类型)获取数据
// 获取元素集合, 按照排名先后(从小到大)
redisTemplate.boundZSetOps(zSetKey).range(key, startIndex, endIndex);// 获取指定值的分数(权重)
redisTemplate.boundZSetOps(zSetKey).score(zSetVaule);// 获取 zset 长度
redisTemplate.boundZSetOps(zSetKey).size();
(Zset类型)修改分数
// 修改指定元素的分数
redisTemplate.boundZSetOps(zSetKey).incrementScore(zSetVaule, score);

(Zset类型)删除数据

// 删除指定元素
redisTemplate.boundZSetOps(zSetKey).remove(zSetVaule);// 删除指定索引范围的元素
redisTemplate.boundZSetOps(zSetKey).removeRange(strat, end);// 删除指定分数范围的元素
redisTemplate.boundZSetOps(zSetKey).removeRangeByScorssse(strat, end);


http://www.ppmy.cn/news/1267418.html

相关文章

看图识药,python开发实现基于VisionTransformer的119种中草药图像识别系统

中药药材图像识别相关的实践在前面的系列博文中已经有了相应的实践了&#xff0c;感兴趣的话可以自行移步阅读即可&#xff0c;每篇文章的侧重点不同&#xff1a; 《python基于轻量级GhostNet模型开发构建23种常见中草药图像识别系统》 《基于轻量级MnasNet模型开发构建40种常…

gin投票系统3

对应视频v1版本 1.优化登陆接口 将同步改为异步 原login前端代码&#xff1a; <!doctype html> <html lang"en"> <head><meta charset"utf-8"><title>香香编程-投票项目</title> </head> <body> <m…

抓包工具:Sunny网络中间件

Sunny网络中间件 和 Fiddler 类似。 是可跨平台的网络分析组件 可用于HTTP/HTTPS/WS/WSS/TCP/UDP网络分析 为二次开发量身制作 支持 获取/修改 HTTP/HTTPS/WS/WSS/TCP/TLS-TCP/UDP 发送及返回数据 支持 对 HTTP/HTTPS/WS/WSS 指定连接使用指定代理 支持 对 HTTP/HTTPS/WS/WSS/T…

Failed to connect to github.com port 443 after 21055 ms: Timed out

目前自己使用了梯*子还是会报这样的错误&#xff0c;连接不到的github。 查了一下原因&#xff1a; 是因为这个请求没有走代理。 解决方案&#xff1a; 设置 -> 网络和Internet -> 代理 -> 编辑 记住这个IP和端口 使用以下命令&#xff1a; git config --global h…

CentOS使用kkFileView实现在线预览word excel pdf等

一、环境安装 1、安装LibreOffice wget https://downloadarchive.documentfoundation.org/libreoffice/old/7.5.3.2/rpm/x86_64/LibreOffice_7.5.3.2_Linux_x86-64_rpm.tar.gz # 解压缩 tar -zxf LibreOffice_7.5.3.2_Linux_x86-64_rpm.tar cd LibreOffice_7.5.3.2_Linux_x86…

Backtrader 文档学习-Quickstart

Backtrader 文档学习-Quickstart 0. 前言 backtrader&#xff0c;功能十分完善&#xff0c;有完整的使用文档&#xff0c;安装相对简单&#xff08;直接pip安装即可&#xff09;。 优点是运行速度快&#xff0c;支持pandas的矢量运算&#xff1b;支持参数自动寻优运算&#x…

Pytest自动化测试用例中的断言详解

前言 测试的主要工作目标就是验证实际结果与预期结果是否一致&#xff1b;在接口自动化测试中&#xff0c;通过断言来实现这一目标。Pytest中断言是通过assert语句实现的&#xff08;pytest对Python原生的assert语句进行了优化&#xff09;&#xff0c;确定实际情况是否与预期一…

三天精通Selenium Web 自动化 - Selenium(Java)环境搭建 (new)

0 背景 开发工具idea代码管理mavenjdk1.8webdriver chrome 1 chromedriver & chrome chromedriver和chrome要对应上&#xff1a; chomedriver下载地址&#xff1a;淘宝镜像 这里用的是 chromedriver88-0-4324-96.zipchrome下载地址&#xff1a;如何降级和安装旧版本的C…