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

Day05 店铺营业状态设置 Redis

Redis 入门

Redis 简介

Redis 是一个基于内存的 key-value 结构数据库。

·基于内存存储,读写性能高

·适合存储热点数据(热点商品,资讯,新闻)

企业应用广泛

redis 中文网:Redis中文网

Redis 下载与安装

Redis 的 Windows版属于绿色软件,直接解压即可使用,解压后目录结构如下:

Redis 服务启动与停止

 启动:在解压目录下进入命令行窗口,输入以下命令即可启动

结束:直接 Ctrl + C 即可

客户端连接服务器:

注意服务端不能关,否则连接不上

客户端断开:输入 exit 即可

补充如果我们要连接外部的服务器:

-h 后面写域名,-p 后面写端口名

密码修改:在 conf 文件里找到 443 行这一行,

requirepass 后面跟的就是你需要的密码

这时我们重启 Redis,并重新连接客户端,会发现一个 error 告诉我们未认证

我们要想连接,在之前的命令下加一个 -a yourPassWord 即可

当然我们可以直接下载图形化工具进行连接

服务端记得不要关

Redis 数据类型

5种常用数据类型介绍

各种数据类型的特点

Redis 常用命令

字符串操作命令

        

哈希操作命令

列表操作命令

集合操作命令

有序集合操作命令

通用命令

在 Java 中操作 Redis

Redis 的 Java 客户端

Redis 的 Java 客户端很多,常用的几种:
· Jedis

· Lettuce

· Spring Data Redis

Spring Data Redis 是 Spring 的一部分,对 Redis 底层开发包进行了高度封装。在 Spring 项目中,可以使用 Spring Data 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 RedisConfiguration {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){RedisTemplate redisTemplate = new RedisTemplate();//设置 redis 的连接工厂对象redisTemplate.setConnectionFactory(redisConnectionFactory);//设置 redis key 的序列化器redisTemplate.setKeySerializer(new StringRedisSerializer());return redisTemplate;}
}
package com.sky.test;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;@SpringBootTest
public class SpringDataRedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testRedisTemplate(){System.out.println(redisTemplate);ValueOperations valueOperations = redisTemplate.opsForValue();HashOperations hashOperations = redisTemplate.opsForHash();ListOperations listOperations = redisTemplate.opsForList();SetOperations setOperations = redisTemplate.opsForSet();ZSetOperations zSetOperations = redisTemplate.opsForZSet();}
}

最后是完整的测试代码,数据库里面的乱码是正常的,因为序列化的问题没有处理

package com.sky.test;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;import javax.xml.crypto.Data;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;@SpringBootTest
public class SpringDataRedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testRedisTemplate(){System.out.println(redisTemplate);ValueOperations valueOperations = redisTemplate.opsForValue();HashOperations hashOperations = redisTemplate.opsForHash();ListOperations listOperations = redisTemplate.opsForList();SetOperations setOperations = redisTemplate.opsForSet();ZSetOperations zSetOperations = redisTemplate.opsForZSet();}@Testpublic void testString(){//set get setex setnxredisTemplate.opsForValue().set("city","北京");String city = (String) redisTemplate.opsForValue().get("city");System.out.println(city);redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);redisTemplate.opsForValue().setIfAbsent("local", "1");redisTemplate.opsForValue().setIfAbsent("local", "2");}@Testpublic void testHash(){//hset hget hdel hkeys hvalsHashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("100", "name", "tom");hashOperations.put("100", "age", "20");String name = (String) hashOperations.get("100", "name");System.out.println(name);Set keys = hashOperations.keys("100");System.out.println(keys);List values = hashOperations.values("100");System.out.println(values);hashOperations.delete("100", "name");}@Testpublic void testList(){//lpush lrange rpop llenListOperations listOperations= redisTemplate.opsForList();listOperations.leftPushAll("mylist","a","b","c");listOperations.leftPush("mylist", "d");List list = listOperations.range("mylist", 0, -1);System.out.println(list);listOperations.rightPop("mylist");Long size = listOperations.size("mylist");System.out.println(size);}@Testpublic void testSet(){//sadd smembers scard sinter sunion sremSetOperations setOperations = redisTemplate.opsForSet();setOperations.add("set1", "a","b","c","d");setOperations.add("set2", "a", "b", "x", "y");Set members = setOperations.members("set1");System.out.println(members);Long size = setOperations.size("set1");System.out.println(size);Set intersect = setOperations.intersect("set1", "set2");System.out.println(intersect);Set union = setOperations.union("set1", "set2");System.out.println(union);setOperations.remove("set1", "a", "b");}@Testpublic void testZset(){// zadd zrange zincrby zremZSetOperations zSetOperations = redisTemplate.opsForZSet();zSetOperations.add("zset1", "a", 10);zSetOperations.add("zset1", "b", 12);zSetOperations.add("zset1", "c", 9);Set zset1 = zSetOperations.range("zset1", 0, -1);System.out.println(zset1);zSetOperations.incrementScore("zset1", "c", 10);zSetOperations.remove("zset1", "a", "b");}@Testpublic void testCommon(){//keys exists type delSet keys = redisTemplate.keys("*");System.out.println(keys);Boolean name = redisTemplate.hasKey("name");Boolean set1 = redisTemplate.hasKey("set1");System.out.println(name);System.out.println(set1);for (Object key : keys) {DataType type = redisTemplate.type(key);System.out.println(type.name());}redisTemplate.delete("mylist");}
}

店铺营业状态设置

需求分析和设计

产品原型

接口设计

营业状态数据存储方式:基于 Redis 的字符串来进行存储

代码开发

package com.sky.controller.admin;import com.sky.result.Result;
import io.swagger.annotations.Api;
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.*;@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Slf4j
@Api(tags = "管理端店铺营业状态相关接口")
public class ShopController {@AutowiredRedisTemplate redisTemplate;public static final String  KEY = "SHOP_STATUS";@GetMapping("/status")@ApiOperation("营业状态查询")public Result<Integer> getByStatus(){Integer status = (Integer) redisTemplate.opsForValue().get(KEY);log.info("当前店铺的营业状态为:{}", status == 1 ? "营业中": "打样中");return Result.success(status);}@PutMapping("/{status}")@ApiOperation("营业状态设置")public Result SetStatus(@PathVariable Integer status){log.info("将当前店铺的状态设置为:{}", status);redisTemplate.opsForValue().set(KEY, status);return Result.success();}
}
package com.sky.controller.user;import com.sky.result.Result;
import io.swagger.annotations.Api;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController("userShopController")
@RequestMapping("/user/shop")
@Slf4j
@Api(tags = "用户端店铺营业状态相关接口")
public class ShopController {@AutowiredRedisTemplate redisTemplate;public static final String  KEY = "SHOP_STATUS";@GetMapping("/status")@ApiOperation("营业状态查询")public Result<Integer> result(){Integer status = (Integer) redisTemplate.opsForValue().get(KEY);log.info("当前店铺的营业状态为:{}", status == 1 ? "营业中": "打样中");return Result.success(status);}}

功能测试

http://www.lryc.cn/news/618573.html

相关文章:

  • 【C++】迭代器失效问题
  • THCV215一种高速视频数据收发器,采用低电压差分信号(LVDS)技术支持高速串行数据传输,支持1080p/60Hz高分辨率传输
  • 软考备考(三)
  • 2-1〔O҉S҉C҉P҉ ◈ 研记〕❘ 漏洞扫描▸理论基础与NSE脚本
  • 26 届秋招建议指南
  • Git与CI/CD相关知识点总结
  • [激光原理与应用-251]:理论 - 几何光学 - 长焦与短焦的比较
  • k8s-scheduler 解析
  • 【Java项目与数据库、Maven的关系详解】
  • 正向传播与反向传播(神经网络思维的逻辑回归)
  • Gradient Descent for Logistic Regression|逻辑回归梯度下降
  • B站 韩顺平 笔记 (Day 16)
  • 微软发布GPT-5赋能的Copilot:重构办公场景的智能革命
  • MODBUS RTU协议:工业物联网的“普通话“(Android开发实战指南)
  • C++ Rust与Go
  • LeetCode算法领域经典入门题目之“Two Sum”问题
  • Springboot3多数据源案例
  • Springboot注册过滤器的三种方式(Order 排序)
  • 亚马逊后台功能风险解构:“清除并替换库存” 的致命陷阱与全链路防控策略
  • 第五章 特征值与特征向量
  • Wireshark专家模式定位网络故障:14种TCP异常深度解剖
  • 【Altium designer】快速建立原理图工程的步骤
  • 深度学习-卷积神经网络-NIN
  • Nginx反向代理功能
  • 【实时Linux实战系列】复杂实时系统中的需求管理
  • 【无标题】centos 配置阿里云的yum源
  • CS2服务器是何方神圣
  • linux 执行ls命令文件夹显示全白色
  • C++——高性能组件
  • 深度学习与遥感入门(六)|轻量化 MobileNetV2 高光谱分类