查询菜品;将查询数据先放入Redis;修改后,清理Redis缓存
This commit is contained in:
parent
97de686244
commit
39640025af
|
@ -1,17 +1,13 @@
|
||||||
package com.sky.config;
|
package com.sky.config;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Configurable;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
||||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class RedisConfiguration {
|
public class RedisConfiguration {
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
package com.sky.config;
|
package com.sky.config;
|
||||||
|
|
||||||
import ch.qos.logback.classic.pattern.MessageConverter;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
||||||
import com.sky.interceptor.JwtTokenUserInterceptor;
|
import com.sky.interceptor.JwtTokenUserInterceptor;
|
||||||
import com.sky.json.JacksonObjectMapper;
|
import com.sky.json.JacksonObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
@ -47,7 +44,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||||
.addPathPatterns("/admin/**")
|
.addPathPatterns("/admin/**")
|
||||||
.excludePathPatterns("/admin/employee/login");
|
.excludePathPatterns("/admin/employee/login");
|
||||||
|
|
||||||
registry.addInterceptor(jwtTokenAdminInterceptor)
|
registry.addInterceptor(jwtTokenUserInterceptor)
|
||||||
.addPathPatterns("/user/**")
|
.addPathPatterns("/user/**")
|
||||||
.excludePathPatterns("/user/user/login")
|
.excludePathPatterns("/user/user/login")
|
||||||
.excludePathPatterns("/user/shop/status");
|
.excludePathPatterns("/user/shop/status");
|
||||||
|
|
|
@ -10,10 +10,12 @@ import com.sky.vo.DishVO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/admin/dish")
|
@RequestMapping("/admin/dish")
|
||||||
|
@ -22,6 +24,8 @@ import java.util.List;
|
||||||
public class DishController {
|
public class DishController {
|
||||||
@Resource
|
@Resource
|
||||||
private DishService dishService;
|
private DishService dishService;
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增菜品和口味
|
* 新增菜品和口味
|
||||||
|
@ -34,6 +38,10 @@ public class DishController {
|
||||||
public Result<String> save(@RequestBody DishDTO dishDTO) {
|
public Result<String> save(@RequestBody DishDTO dishDTO) {
|
||||||
log.info("新增菜品:{}", dishDTO);
|
log.info("新增菜品:{}", dishDTO);
|
||||||
dishService.saveWithFlavor(dishDTO);
|
dishService.saveWithFlavor(dishDTO);
|
||||||
|
|
||||||
|
// 修改、新增清理Redis数据
|
||||||
|
String key = "dish_" + dishDTO.getCategoryId();
|
||||||
|
redisTemplate.delete(key);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,6 +70,9 @@ public class DishController {
|
||||||
public Result<String> delete(@RequestParam List<Long> ids) {
|
public Result<String> delete(@RequestParam List<Long> ids) {
|
||||||
log.info("菜品批量删除:{}", ids);
|
log.info("菜品批量删除:{}", ids);
|
||||||
dishService.deleteBatch(ids);
|
dishService.deleteBatch(ids);
|
||||||
|
|
||||||
|
// 以dish_开头全部删除
|
||||||
|
cleanRedisCache();
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +101,9 @@ public class DishController {
|
||||||
public Result<String> update(@RequestBody DishDTO dishDTO) {
|
public Result<String> update(@RequestBody DishDTO dishDTO) {
|
||||||
log.info("修改菜品:{}", dishDTO);
|
log.info("修改菜品:{}", dishDTO);
|
||||||
dishService.updateWithFlavor(dishDTO);
|
dishService.updateWithFlavor(dishDTO);
|
||||||
|
|
||||||
|
// 如果修改了就将所有的缓存数据全部删除
|
||||||
|
cleanRedisCache();
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,4 +133,14 @@ public class DishController {
|
||||||
List<Dish> list = dishService.list(categoryId);
|
List<Dish> list = dishService.list(categoryId);
|
||||||
return Result.success(list);
|
return Result.success(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果修改了就将所有的缓存数据全部删除
|
||||||
|
*/
|
||||||
|
private void cleanRedisCache() {
|
||||||
|
Set keys = redisTemplate.keys("dish_*");
|
||||||
|
if (keys != null && !keys.isEmpty()) {
|
||||||
|
redisTemplate.delete(keys);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,11 @@ import com.sky.result.Result;
|
||||||
import com.sky.service.CategoryService;
|
import com.sky.service.CategoryService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RestController("userCategoryController")
|
@RestController("userCategoryController")
|
||||||
|
@ -16,15 +17,16 @@ import java.util.List;
|
||||||
@Api(tags = "C端-分类接口")
|
@Api(tags = "C端-分类接口")
|
||||||
public class CategoryController {
|
public class CategoryController {
|
||||||
|
|
||||||
@Autowired
|
@Resource
|
||||||
private CategoryService categoryService;
|
private CategoryService categoryService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询分类
|
* 查询分类
|
||||||
* @param type
|
*
|
||||||
* @return
|
* @param type Integer
|
||||||
|
* @return Result<List < Category>>
|
||||||
*/
|
*/
|
||||||
@GetMapping("/list")
|
@GetMapping("list")
|
||||||
@ApiOperation("查询分类")
|
@ApiOperation("查询分类")
|
||||||
public Result<List<Category>> list(Integer type) {
|
public Result<List<Category>> list(Integer type) {
|
||||||
List<Category> list = categoryService.list(type);
|
List<Category> list = categoryService.list(type);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.sky.controller.user;
|
package com.sky.controller.user;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.sky.constant.StatusConstant;
|
import com.sky.constant.StatusConstant;
|
||||||
import com.sky.entity.Dish;
|
import com.sky.entity.Dish;
|
||||||
import com.sky.result.Result;
|
import com.sky.result.Result;
|
||||||
|
@ -8,35 +9,52 @@ import com.sky.vo.DishVO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@RestController("userDishController")
|
@RestController("userDishController")
|
||||||
@RequestMapping("/user/dish")
|
@RequestMapping("/user/dish")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Api(tags = "C端-菜品浏览接口")
|
@Api(tags = "C端-菜品浏览接口")
|
||||||
public class DishController {
|
public class DishController {
|
||||||
@Autowired
|
@Resource
|
||||||
private DishService dishService;
|
private DishService dishService;
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据分类id查询菜品
|
* 根据分类id查询菜品
|
||||||
*
|
*
|
||||||
* @param categoryId
|
* @param categoryId Long
|
||||||
* @return
|
* @return Result<List < DishVO>>
|
||||||
*/
|
*/
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
@ApiOperation("根据分类id查询菜品")
|
@ApiOperation("根据分类id查询菜品")
|
||||||
public Result<List<DishVO>> list(Long categoryId) {
|
public Result<List<DishVO>> list(@RequestParam Long categoryId) {
|
||||||
|
// 设置Redis中key
|
||||||
|
String key = "dish_" + categoryId;
|
||||||
|
// 查询Redis中数据
|
||||||
|
List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
|
||||||
|
// 如果不为空返回集合
|
||||||
|
if (list != null && !list.isEmpty()) {
|
||||||
|
return Result.success(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果Redis中没有,将值放入
|
||||||
Dish dish = new Dish();
|
Dish dish = new Dish();
|
||||||
dish.setCategoryId(categoryId);
|
dish.setCategoryId(categoryId);
|
||||||
dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
|
dish.setStatus(StatusConstant.ENABLE);// 查询起售中的菜品
|
||||||
|
// 将这个数据保存到redis中
|
||||||
List<DishVO> list = dishService.listWithFlavor(dish);
|
list = dishService.listWithFlavor(dish);
|
||||||
|
redisTemplate.opsForValue().set(key, JSON.toJSON(list), 7, TimeUnit.DAYS);
|
||||||
|
// 如果Redis中没有,返回这个数据并保存这个数据
|
||||||
return Result.success(list);
|
return Result.success(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ import com.sky.service.DishService;
|
||||||
import com.sky.vo.DishVO;
|
import com.sky.vo.DishVO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@ -34,6 +35,8 @@ public class DishServiceImpl implements DishService {
|
||||||
private DishFlavorMapper dishFlavorMapper;
|
private DishFlavorMapper dishFlavorMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private SetMealDishMapper setMealDishMapper;
|
private SetMealDishMapper setMealDishMapper;
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增菜品和口味
|
* 新增菜品和口味
|
||||||
|
@ -195,6 +198,10 @@ public class DishServiceImpl implements DishService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 停售时将Redis缓存清除
|
||||||
|
dish = dishMapper.getById(id);
|
||||||
|
String key = "dish_" + dish.getCategoryId();
|
||||||
|
redisTemplate.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -21,7 +21,6 @@ spring:
|
||||||
port: ${sky.redis.port}
|
port: ${sky.redis.port}
|
||||||
password: ${sky.redis.password}
|
password: ${sky.redis.password}
|
||||||
database: ${sky.redis.database}
|
database: ${sky.redis.database}
|
||||||
timeout: 5000
|
|
||||||
|
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
|
|
Loading…
Reference in New Issue