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/617480464
资源推荐
资源详情
资源评论






























收起资源包目录





































































































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

- F_buffoon2024-11-23果断支持这个资源,资源解决了当前遇到的问题,给了新的灵感,感谢分享~
- tianhao9092024-03-26简直是宝藏资源,实用价值很高,支持!

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


最新资源
- 年电子商务实习报告.docx
- 沪宁高速公路BENNETT加油站管理网络系统设计说明.doc
- 智能小区网络案例.ppt
- 计算机应用中职教师专业实训培训方案.doc
- 2023年新版最新网络安全法知识竞赛试题及答案.docx
- 网站未经允许提供下载违法吗?.doc
- 洽洽电子商务网络托管合作方案.doc
- 国家开放大学电大《桥梁工程技术》《金融企业会计》网络课形考网考作业(合集)答案.docx
- 我分局正版软件使用情况自查报告.docx
- 现代设计理论与方法-计算机辅助设计ppt课件.ppt
- 咖啡屋网络营销专项方案.doc
- 公安网即时通信系统项目.doc
- 学院自动化专业毕业设计任务书.doc
- 互联网背景下高校体育课程改革与发展探索优秀科研论文报告.docx
- (源码)基于C++Qt框架的原创棋类对战游戏.zip
- 高端自动化加工装备和精密零部件制造建设项目环境影响报告表.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



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