使用redis设置店铺状态
知识点:
将前端传过来的status(0,1)通过redis对象以key,values值的形式存放在redis中。
#设置店铺状态
redisTemplate.opsForValue().set(KEY,status);
#获取店铺状态
Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
#设置一个静态常量
public final static String KEY="SHOP_STATUS";
package com.sky.controller.admin;
import com.sky.result.Result;
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.*;
@Slf4j
@RestController("adminShopController")
@RequestMapping("/admin/shop")
public class ShopController {
public final static String KEY="SHOP_STATUS";
@Autowired
private RedisTemplate redisTemplate;
@PutMapping("/{status}")
public Result setStatus(@PathVariable Integer status){
log.info("设置店铺的营业状态为:{}",status == 1 ? "营业中":"大洋中");
redisTemplate.opsForValue().set(KEY,status);
return Result.success();
}
@GetMapping("/status")
public Result<Integer> getStatus(){
Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
log.info("获取店铺的状态:{}",status == 1 ? "营业中":"大洋中");
return Result.success(status);
}
}