package cn.eat.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author: zjl
* @datetime: 2024/4/26
* @desc:
*/
@Component
public class RedisStringUtil {
private static StringRedisTemplate redisTemplate;
@Autowired
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
RedisStringUtil.redisTemplate = redisTemplate;
}
/** -------------------string相关操作--------------------- */
/**
* 设置指定 key 的值
*
* @param key
* @param value
*/
public static void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 获取指定 key 的值
*
* @param key
* @return
*/
public static String get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 返回 key 中字符串值的子字符
*
* @param key
* @param start
* @param end
* @return
*/
public static String getRange(String key, long start, long end) {
return redisTemplate.opsForValue().get(key, start, end);
}
/**
* 将给定 key 的值设为 value ,并返回 key 的旧值(old value)
*
* @param key
* @param value
* @return
*/
public static String getAndSet(String key, String value) {
return redisTemplate.opsForValue().getAndSet(key, value);
}
/**
* 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
* @param key
* @param offset
* @return
*/
public static Boolean getBit(String key, long offset) {
return redisTemplate.opsForValue().getBit(key, offset);
}
/**
* 批量获取
*
* @param keys
* @return
*/
public static List<String> multiGet(Collection<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
/**
* 设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
*
* @param key 位置
* @param value 值,true为1, false为0
* @return
*/
public static boolean setBit(String key, long offset, boolean value) {
return redisTemplate.opsForValue().setBit(key, offset, value);
}
/**
* 将值 value 关联到 key ,并将 key 的过期时间设为 timeout
*
* @param key
* @param value
* @param timeout 过期时间
* @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES
* 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
*/
public static void setEx(String key, String value, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
}
/**
* 只有在 key 不存在时设置 key 的值
*
* @param key
* @param value
* @return 之前已经存在返回false, 不存在返回true
*/
public static boolean setIfAbsent(String key, String value) {
return redisTemplate.opsForValue().setIfAbsent(key, value);
}
/**
* 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
*
* @param key
* @param value
* @param offset 从指定位置开始覆写
*/
public static void setRange(String key, String value, long offset) {
redisTemplate.opsForValue().set(key, value, offset);
}
/**
* 获取字符串的长度
*
* @param key
* @return
*/
public static Long size(String key) {
return redisTemplate.opsForValue().size(key);
}
/**
* 批量添加
*
* @param maps
*/
public static void multiSet(Map<String, String> maps) {
redisTemplate.opsForValue().multiSet(maps);
}
/**
* 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
*
* @param maps
* @return 之前已经存在返回false, 不存在返回true
*/
public static boolean multiSetIfAbsent(Map<String, String> maps) {
return redisTemplate.opsForValue().multiSetIfAbsent(maps);
}
/**
* 增加(自增长), 负数则为自减
*
* @param key
* @return
*/
public static Long incrBy(String key, long increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
/**
* @param key
* @return
*/
public static Double incrByFloat(String key, double increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
/**
* 追加到末尾
*
* @param key
* @param value
* @return
*/
public static Integer append(String key, String value) {
return redisTemplate.opsForValue().append(key, value);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
springboot整合rabbit+redis+mysql实现用户购买商品时提交订单后的过程; 用户提交后,生成订单,用户有XX分钟时间完成支付 如果用户XX分钟内完成支付,则更新订单为已支付,仓库扣库存,发送短信 如果用户XX分钟后未完成支付,则更新订单为未支付,仓库恢复库存 技术栈:springboot+redis+rabbitmq+mysql 业务核心: 1.延迟队列做用户支付 2.库存扣除与恢复 3.订单状态更新 4.短信提示 异步、解耦
资源推荐
资源详情
资源评论
收起资源包目录
eating.rar (39个子文件)
eating
HELP.md 2KB
mvnw.cmd 7KB
pom.xml 3KB
src
test
java
cn
eat
EatingApplicationTests.java 207B
main
resources
templates
static
application.yml 830B
java
cn
eat
EatingApplication.java 389B
mapper
EatMapper.java 1KB
controller
EatController.java 2KB
utils
RedisStringUtil.java 5KB
Constants.java 890B
RedisKeyUtil.java 4KB
consumer
ReceiverLinstener.java 5KB
service
EatService.java 3KB
model
Goods.java 278B
Order.java 351B
config
RabbitConfig.java 2KB
.mvn
wrapper
maven-wrapper.properties 233B
maven-wrapper.jar 61KB
.idea
jarRepositories.xml 881B
uiDesigner.xml 9KB
workspace.xml 4KB
misc.xml 819B
compiler.xml 709B
.gitignore 184B
encodings.xml 191B
target
classes
cn
eat
mapper
EatMapper.class 1KB
EatingApplication.class 800B
controller
EatController.class 1KB
utils
RedisKeyUtil.class 4KB
Constants.class 888B
RedisStringUtil.class 5KB
consumer
ReceiverLinstener.class 5KB
service
EatService.class 4KB
model
Order.class 3KB
Goods.class 2KB
config
RabbitConfig.class 4KB
application.yml 830B
generated-sources
annotations
mvnw 11KB
.gitignore 395B
共 39 条
- 1
资源评论
苏生Susheng
- 粉丝: 2673
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功