当前位置: 首页 > news >正文

苍穹外卖05 Redis常用命令在Java中操作Redis_Spring Data Redis使用方式店铺营业状态设置

2-8 Redis常用命令

02 02-Redis入门

ctrl+c :快捷结束进程

配置密码:

以后再启动客户端的时候就需要进行密码的配置了。使用-a

在图形化界面中创建链接:

启动成功了。

03 03-Redis常用数据类型

04 04-Redis常用命令_字符串操作命令 

05 05-Redis常用命令_哈希操作命令

06 06-Redis常用命令_列表操作命令

07 07-Redis常用命令_集合操作命令

08 08-Redis常用命令_有序集合操作命令

09 09-Redis常用命令_通用命令

2-9 在Java中操作Redis_Spring Data Redis使用方法 操作步骤

02 11-在Java中操作Redis_Spring Data Redis使用方式_环境搭建

package com.sky.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@Slf4j
public class RedisConfigation {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {log.info("开始创建redis模板对象...");RedisTemplate redisTemplate = new RedisTemplate();// 设置连接工厂redisTemplate.setConnectionFactory(redisConnectionFactory);// 设置key序列化器redisTemplate.setKeySerializer(new StringRedisSerializer());return redisTemplate;}
}

用这五个对象来操作redis当中的数据。

操作字符串类型的数据:

操作字符串功能测试代码如下:

/*** 测试redis操作字符串*/@Testpublic void testString() {//setredisTemplate.opsForValue().set("city", "北京");//设定key的值//getString city = (String) redisTemplate.opsForValue().get("city");//获取key的值System.out.println(city);// setxredisTemplate.opsForValue().set("code", "123",3, TimeUnit.MINUTES);//设定key的值,并且设定过期时间// setnxredisTemplate.opsForValue().setIfAbsent("lock","1");//设定key的值,如果key不存在则设置成功,如果key已存在则设置失败redisTemplate.opsForValue().setIfAbsent("lock","2");//校验组}

操作哈希类型的数据:

操作哈希表代码实现如下:

   @Testpublic void testHash() {HashOperations hashOperations =redisTemplate.opsForHash();//hset   将哈希表key中的字段field的值设为valuehashOperations.put("100","name","tom");hashOperations.put("100","age","20");//hget   获取存储在指定哈希表中指定字段的值String name = (String)hashOperations.get("100", "name");System.out.println(name);//hkey   获取指定哈希表所有字段Set keys = hashOperations.keys("100");System.out.println(keys);//hvals   获取指定哈希表中所有值List values = hashOperations.values("100");System.out.println(values);//hdel   删除指定哈希表字段hashOperations.delete("100","age");}

其他类型的操作效果大差不差,就是注意好引用参数的形参限制就好了。不过多赘述,用到了多用几次就熟练了。

06 15-店铺营业状态设置_需求分析和设计

由于下方两个接口你只需要调用一个参数status ,又没必要为了这一个参数创建一张表,所以我们可以选择将这个参数创建在redis当中。

07 16-店铺营业状态设置_代码开发

三个接口实现如下:

package com.sky.controller.admin;import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
@ApiOperation("店铺管理")
@RequestMapping("/admin/shop")
public class ShopController {@Autowiredprivate RedisTemplate redisTemplate;/*** 设置店铺的营业状态* @param status* @return*/@PutMapping("/{status}")@ApiOperation("设置店铺的营业状态")public Result setStatus(@PathVariable Integer status){log.info("设置店铺的营业状态为:{}", status==1? "营业中":"打烊中");redisTemplate.opsForValue().set("SHOP_STATUS", status);return Result.success();}/***  获取营业状态* @return*/@RequestMapping("/status")@ApiOperation("获取店铺的营业状态")public Result<Integer> getStatus(){Integer status = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");log.info("获取营业状态为:{}", status==1? "营业中":"打烊中");return Result.success(status);}
}package com.sky.controller.user;import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@ApiOperation("店铺相关接口")
@RestController
@RequestMapping("/user/shop")
@Slf4j
public class ShopController {@Autowiredprivate RedisTemplate redisTemplate;/*** 获取店铺的营业状态* @return*/@ApiOperation("获取店铺的营业状态")@RequestMapping("/status")public Result<Integer> getStatus(){Integer status = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");log.info("获取营业状态为:{}", status==1? "营业中":"打烊中");return Result.success(status);}
}

测试成功!

http://www.xdnf.cn/news/573877.html

相关文章:

  • 本特利内华达125768-01 RIM i/o模块规范
  • ESP.wdtFeed();的作用与功能,以及使用方法
  • 「AR智慧应急」新时代:当AR眼镜遇上智能监控,打造立体化应急指挥系统
  • AskTable 集成 Databend:结构化数据的 AI 查询新体验
  • 项目自启动文件配置
  • quickbi实现关联度分析(复刻PowerBI展示)
  • 【深度学习:理论篇】--Pytorch之nn.Module详解
  • 嵌入式开发学习日志(linux系统编程--文件读写函数(2))Day25
  • 算法——数组代码
  • RECCV检测人脸伪造项目尝试与扩展
  • 深度学习实战108-基于通义千问Qwen2.5-Omni的智能数字人实时对话系统实现
  • 免费使用GPU的探索笔记
  • 采用排除法进行EMC问题定位
  • 采用线性优化改进评估配电网的灵活性范围
  • Linux服务器SOS Report完全指南:收集方法、作用解析与最佳实践
  • git checkout HEAD
  • C++11语言级别的多线程
  • 5月21日
  • 云渲染技术解析与渲酷平台深度测评:如何实现高效3D创作?
  • 为什么可以不重写m1方法
  • Multi-Query Attention:传统自注意力( Self-Attention)优化显存和加速方案
  • IP核警告,Bus Interface ‘AD_clk‘: ASSOCIATED_BUSIF bus parameter is missing.
  • python生成requirements.txt文件
  • ABC 353
  • ROS2 CV_bridge与opencv版本冲突
  • 学习 Pinia 状态管理【Plan - May - Week 2】
  • 创建一个element plus项目
  • [C++入门]类和对象下
  • 东莞一锂离子电池公司IPO终止,客户与供应商重叠,社保缴纳情况引疑
  • GitLab 配置 webhook