package cn.zwz.basics.redis;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* @author 郑为中
* CSDN: Designer 小郑
*/
@ApiOperation(value = "Redis工具类")
@Component
public class RedisTemplateHelper {
@Autowired
private StringRedisTemplate redisTemplate;
@ApiOperation(value = "scan实现")
private void scan(String wayForScan, Consumer<byte[]> consumableList) {
redisTemplate.execute((RedisConnection connection) -> {
try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().count(Long.MAX_VALUE).match(wayForScan).build())) {
cursor.forEachRemaining(consumableList);
return null;
} catch (Exception exception) {
throw new RuntimeException(exception);
}
});
}
@ApiOperation(value = "scan获取符合条件的key")
public Set<String> scan(String pattern) {
Set<String> keys = new HashSet<>();
this.scan(pattern, item -> {
String key = new String(item, StandardCharsets.UTF_8);
keys.add(key);
});
return keys;
}
@ApiOperation(value = "通过通配符表达式删除所有")
public void deleteByPattern(String pattern) {
Set<String> keys = this.scan(pattern);
redisTemplate.delete(keys);
}
@ApiOperation(value = "删除key")
public void delete(String key) {
redisTemplate.delete(key);
}
@ApiOperation(value = "批量删除key")
public void delete(Collection<String> keys) {
redisTemplate.delete(keys);
}
@ApiOperation(value = "序列化key")
public byte[] dump(String key) {
return redisTemplate.dump(key);
}
@ApiOperation(value = "是否存在key")
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
@ApiOperation(value = "设置过期时间")
public Boolean expire(String key, long timeout, TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
@ApiOperation(value = "设置过期时间")
public Boolean expireAt(String key, Date date) {
return redisTemplate.expireAt(key, date);
}
@ApiOperation(value = "查找匹配的key")
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
@ApiOperation(value = "将当前数据库的 key 移动到给定的数据库 db 当中")
public Boolean move(String key, int dbIndex) {
return redisTemplate.move(key, dbIndex);
}
@ApiOperation(value = "移除 key 的过期时间,key 将持久保持")
public Boolean persist(String key) {
return redisTemplate.persist(key);
}
@ApiOperation(value = "返回 key 的剩余的过期时间")
public Long getExpire(String key, TimeUnit unit) {
return redisTemplate.getExpire(key, unit);
}
@ApiOperation(value = "返回 key 的剩余的过期时间")
public Long getExpire(String key) {
return redisTemplate.getExpire(key);
}
@ApiOperation(value = "从当前数据库中随机返回一个 key")
public String randomKey() {
return redisTemplate.randomKey();
}
@ApiOperation(value = "修改 key 的名称")
public void rename(String oldKey, String newKey) {
redisTemplate.rename(oldKey, newKey);
}
@ApiOperation(value = "仅当 newkey 不存在时,将 oldKey 改名为 newkey")
public Boolean renameIfAbsent(String oldKey, String newKey) {
return redisTemplate.renameIfAbsent(oldKey, newKey);
}
@ApiOperation(value = "返回 key 所储存的值的类型")
public DataType type(String key) {
return redisTemplate.type(key);
}
/** -------------------string相关操作--------------------- */
@ApiOperation(value = "设置指定 key 的值")
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
@ApiOperation(value = "将值 value 关联到 key ,并将 key 的过期时间设为 timeout")
public void set(String key, String value, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
}
@ApiOperation(value = "获取指定 key 的值")
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
@ApiOperation(value = "返回 key 中字符串值的子字符")
public String getRange(String key, long start, long end) {
return redisTemplate.opsForValue().get(key, start, end);
}
@ApiOperation(value = "将给定 key 的值设为 value ,并返回 key 的旧值(old value)")
public String getAndSet(String key, String value) {
return redisTemplate.opsForValue().getAndSet(key, value);
}
@ApiOperation(value = "对 key 所储存的字符串值,获取指定偏移量上的位(bit)")
public Boolean getBit(String key, long offset) {
return redisTemplate.opsForValue().getBit(key, offset);
}
@ApiOperation(value = "批量获取")
public List<String> multiGet(Collection<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
@ApiOperation(value = "设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value",notes = "offset 位置, value: 值,true为1, false为0")
public boolean setBit(String key, long offset, boolean value) {
return redisTemplate.opsForValue().setBit(key, offset, value);
}
@ApiOperation(value = "只有在 key 不存在时设置 key 的值",notes = "之前已经存在返回false, 不存在返回true")
public boolean setIfAbsent(String key, String value) {
return redisTemplate.opsForValue().setIfAbsent(key, value);
}
@ApiOperation(value = "用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始",notes = "offset:从指定位置开始覆写")
public void setRange(String key, String value, long offset) {
redisTemplate.opsForValue().set(key, value, offset);
}
@ApiOperation(value = "获取字符串的长度")
public Long size(String key) {
return redisTemplate.opsForValue().size(key);
}
@ApiOperation(value = "批量添加")
public void multiSet(Map<String, String> maps) {
redisTemplate.opsForValue().multiSet(maps);
}
@ApiOperation(value = "同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在")
public boolean multiSetIfAbsent(Map<String, String> maps) {
return redisTemplate.opsForValue().multiSetIfAbsent(maps);
}
@ApiOperation(value = "增加(自增长), 负数则为自减")
public Long incrBy(String key, long increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
@ApiOperation(value = "增加(自增长)")
public Double incrByFloat(String key, double increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
@ApiOperation(value = "追加到末尾")
public Integer append
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于JAVA+Vue+SpringBoot+MySQL的就医保险管理系统,包含了科室档案模块、医生档案模块、预约挂号模块、我的挂号模块,还包含系统自带的用户管理、部门管理、角色管理、菜单管理、日志管理、数据字典管理、文件管理、图表展示等基础模块,就医保险管理系统基于角色的访问控制,给挂号管理员、患者角色使用,可将权限精确到按钮级别,您可以自定义角色并分配权限,系统适合设计精确的权限约束需求。 项目讨论帖:https://bbs.csdn.net/topics/617595574 项目启动教程:https://edu.csdn.net/course/detail/38632
资源推荐
资源详情
资源评论
























收起资源包目录





































































































共 383 条
- 1
- 2
- 3
- 4
资源评论


Designer小郑
- 粉丝: 9w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 综合布线教程第4章.pptx
- 国家开放大学电大本科《儿童发展问题的咨询与辅导》网络课形考作业题库及答案.docx
- 聚焦核心素养--协助深度学习2022.5.13公开课.pptx
- 综合项目管理人员安全关键技术交底.doc
- 网络逻辑结构物理拓扑图例公开课一等奖优质课大赛微课获奖课件.pptx
- 在全市文化和旅游系统安全生产培训会议上的讲话.doc
- 微信小程序示例教程完整版详解
- 项目管理月报.docx
- 江苏科技大学操作系统实验.pdf
- 小学语文网络课程资源开发方案.doc
- 猫扑:树立网络营销新维度.pptx
- 数据库技术及应用实验指导书.doc
- 双电伺服数控转塔冲床控制系统软件.doc
- 音视频智能系统集成工程资质管理规定.doc
- 自动化仪表施工组织设计.pdf
- 投资估算法可行性研究报告.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
