店铺营业状态设置
Redis MySQL Java并发 JavaMVC 计算机网络 操作系统 算法,后端面试主要是这些,外加项目
Redis
数据库,基于内存存储的key-value结构。
mysql是磁盘存储,通过二维表存储。
在文件夹目录打开cmd
服务端:
开启
redis-server.exe redis.windows.conf
客户端:
普通启动连接
redis-cli.exe
显示的指定服务端和端口号
redis-cli.exe -h localhost -p 6379
默认客户端不需要密码就能连接上。
修改配置文件
redis.windows.conf 里 指定密码为123456
requirepass 123456
不需要用户名,客户端链接需要添加上密码
redis-cli.exe -h localhost -p 6379 -a 123456
常用数据类型
value的数据类型,key是字符串
常用命令:
1.字符串
不区分大小写
在图形化界面里按左边栏上的>_
set
setex
2.哈希
hset
hget
hdel
hkeys
kvals
3.列表操作
lpush mylist a b c//从左侧插入即从头
lrange mylist 0 -1 //从头到尾
rpop mylist //删最后一个
llen mylist
4.集合操作
>sadd set1 a b c d//表内顺序和插入顺序无关
4
>smembers set1
>scard set1
4
>sadd set2 a b x y
>sinter set1 set2
b
a
>sunion set1 set3
c
a
b
x
y
5.有序集合操作
会按照分数排序
>zrange zset1 0 -1
a
b
c
>zrange zset1 0 -1 withscores
a
10
b
10.1
c
11.1
>zincrby zset1 5.0 a//给a的值+5.0
6.通用命令
>type mylist
list
>type set1
set
>del name set1 zset1
3
>
Redis 的Java 客户端
用java操作
。。。
(1)操作字符串类型数据
@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void test01(){redisTemplate.opsForValue().set("name","小明"); // 设置k vString city = (String) redisTemplate.opsForValue().get("name"); // 拿到key为name的值System.out.println(city);redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES); // 设置过期时间为三分钟redisTemplate.opsForValue().setIfAbsent("lock","1"); // 设置lock为k的唯一值redisTemplate.opsForValue().setIfAbsent("lock","2");}
小明
(2)操作哈希类型数据
@Testpublic void test02(){//hset hget hdel hkeys hvalsHashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("100","name","tom"); // 设置k为100,哈希k为name v为100hashOperations.put("100","age","20");String name = (String) hashOperations.get("100", "name"); // 获取k为100,hashk为name的vSystem.out.println(name);Set keys = hashOperations.keys("100"); // 获取k为100的集合kSystem.out.println(keys);List values = hashOperations.values("100"); // 获取k为100的集合vSystem.out.println(values);hashOperations.delete("100","age"); // 删除age字段}
tom
[name,age]
[tom,20]
(3)操作列表类型数据
@Testpublic void test03(){//lpush lrange rpop llenListOperations listOperations = redisTemplate.opsForList();listOperations.leftPushAll("mylist","a","b","c"); // 往k为mylist插入a,b,clistOperations.leftPush("mylist","d"); // 插入dList mylist = listOperations.range("mylist", 0, -1);System.out.println(mylist); // 输出listOperations.rightPop("mylist"); // 删除Long size = listOperations.size("mylist");System.out.println(size);System.out.println(listOperations.range("mylist", 0, -1));}
[d,c,b,a]
3
[d,c,b]
(4)操作集合类型数据
@Testpublic void test4(){//sadd smembers scard sinter sunion sremSetOperations setOperations = redisTemplate.opsForSet();setOperations.add("set1","a","b","c","d"); // 往set1 插入abcdsetOperations.add("set2","a","b","x","y"); // 往set2 插入abxySet 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"); // 在set1里面删除ab}
[a,b,c,d]
4
[a,b]
[a,b,c,d,x,y]
(5)操作有序集合类型数据
/*** 操作有序集合类型的数据*/@Testpublic void test05(){//zadd zrange zincrby zremZSetOperations zSetOperations = redisTemplate.opsForZSet();zSetOperations.add("zset1","a",10); // 设置k为zset1v为a,分数为10zSetOperations.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); // 为k为zset1v为c的加10分zSetOperations.remove("zset1","a","b"); // 删除ab}
[c,a,b]
(6)通用命令操作
@Testpublic void test06(){//keys exists type delSet keys = redisTemplate.keys("*"); // 查找所有符合给定模式( pattern)的 key System.out.println(keys);Boolean name = redisTemplate.hasKey("name");Boolean set1 = redisTemplate.hasKey("set1");for (Object key : keys) {DataType type = redisTemplate.type(key); // 查找他们的类型是什么System.out.println(type.name());}redisTemplate.delete("mylist"); // 删除mylist}
[set2,zset1,set1]
SET
ZSET
SET
总结:
Spring Data Redis中提供了一个高度封装的类:RedisTemplate,对相关api进行了归类封装,将同一类型操作封装为operation接口,具体分类如下:
ValueOperations:string数据操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:hash类型的数据操作
ListOperations:list类型的数据操作
回归正题:店铺营业状态设置
用户端与管理端一样只是路径变为/user/shop/status
admin.ShopController.java
@RestController("adminShopController")//为了区分他俩
@RequestMapping("/admin/shop")
@Slf4j
@Api(tags = "店铺相关接口")
public class ShopController {public static final String KEY="SHOP_STATUS";@Autowiredprivate RedisTemplate redisTemplate;//注入redis对象@PutMapping("/{status}")@ApiOperation("设置店铺运营状态")public Result setStatus(@PathVariable Integer status){redisTemplate.opsForValue().set("SHOP_STATUS",status);//保存状态值return Result.success();}@GetMapping("/status")@ApiOperation("显示店铺运营状态")public Result<Integer> getStatus(){Integer status = (Integer) redisTemplate.opsForValue().get(KEY);log.info("获取到店铺的营业状态为:{}",status == 1 ? "营业中":"打烊中");return Result.success();}}
为了避免两个Bean都叫shopController的冲突,所以用@RestController(“UserShopController”)//为了区分他俩
user.ShopController.java
@RestController("UserShopController")//为了区分他俩
@RequestMapping("/user/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {public static final String KEY = "SHOP_STATUS";@Autowiredprivate RedisTemplate redisTemplate;@GetMapping("/status")@ApiOperation("显示店铺运营状态")public Result<Integer> getStatus() {log.info("店铺查询状态");return Result.success((Integer) redisTemplate.opsForValue().get(KEY));}}
另外:为了将接口文档的用户和admin的接口区分开
修改配置类,分别扫描两个包,分组展示,形成两份接口文档
@Beanpublic Docket docketOne() {ApiInfo apiInfo = new ApiInfoBuilder().title("苍穹外卖项目接口文档").version("2.0").description("苍穹外卖项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("管理端接口")//分组.apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin")).paths(PathSelectors.any()).build();return docket;}@Beanpublic Docket docketTwo() {ApiInfo apiInfo = new ApiInfoBuilder().title("苍穹外卖项目接口文档").version("2.0").description("苍穹外卖项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("用户端接口")//分组.apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.sky.controller.user")).paths(PathSelectors.any()).build();return docket;}