dish cache
This commit is contained in:
@@ -19,7 +19,7 @@ public class RedisConfiguration {
|
||||
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||
//设置序列化器
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new StringRedisSerializer());
|
||||
// redisTemplate.setValueSerializer(new StringRedisSerializer());
|
||||
return redisTemplate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RestController("UserCategoeyController")
|
||||
@RequestMapping("/user/category")
|
||||
@Api(tags = "Category")
|
||||
@Slf4j
|
||||
public class UserCategoryController {
|
||||
public class CategoryController {
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@@ -12,11 +12,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RestController("UserDishController")
|
||||
@RequestMapping("/user/dish")
|
||||
@Api(tags = "UserDish")
|
||||
@Slf4j
|
||||
public class UserDishController {
|
||||
public class DishController {
|
||||
@Autowired
|
||||
private DishService dishService;
|
||||
|
||||
@@ -16,11 +16,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RestController("UserSetmealController")
|
||||
@RequestMapping("/user/setmeal")
|
||||
@Api(tags = "UserSetmeal")
|
||||
@Slf4j
|
||||
public class UserSetmealController {
|
||||
public class SetmealController {
|
||||
@Autowired
|
||||
private SetmealService setmealService;
|
||||
|
||||
@@ -68,7 +68,7 @@ public interface DishMapper {
|
||||
* @param categoryId
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from dish where category_id = #{categoryId}")
|
||||
@Select("select * from dish where category_id = #{categoryId} and status = 1")
|
||||
List<Dish> selectByCategoryId(Long categoryId);
|
||||
|
||||
List<Dish> selectByDishIdsAndStatusDisable(List<Long> dishIds);
|
||||
|
||||
@@ -17,16 +17,21 @@ import com.sky.mapper.SetmealDishMapper;
|
||||
import com.sky.mapper.SetmealMapper;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.DishService;
|
||||
import com.sky.service.SetmealService;
|
||||
import com.sky.vo.DishVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -40,6 +45,8 @@ public class DishServiceImpl implements DishService {
|
||||
private SetmealDishMapper setmealDishMapper;
|
||||
@Autowired
|
||||
private SetmealMapper setmealMapper;
|
||||
@Resource
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 新增菜品
|
||||
@@ -66,6 +73,9 @@ public class DishServiceImpl implements DishService {
|
||||
dishFlavorMapper.insertBatch(flavors);
|
||||
}
|
||||
|
||||
//clear redis cache
|
||||
String key = "dish_"+dishDTO.getCategoryId();
|
||||
cleanCache(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,6 +121,9 @@ public class DishServiceImpl implements DishService {
|
||||
}*/
|
||||
dishMapper.deleteByIds(ids);
|
||||
dishFlavorMapper.deleteByDishIds(ids);
|
||||
|
||||
//clear all the dish cache in redis
|
||||
cleanCache("dish_*");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,6 +171,9 @@ public class DishServiceImpl implements DishService {
|
||||
//插入新的口味数据
|
||||
dishFlavorMapper.insertBatch(flavors);
|
||||
}
|
||||
|
||||
//clear redis cache
|
||||
cleanCache("dish_*");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,18 +213,42 @@ public class DishServiceImpl implements DishService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cleanCache("dish_*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DishVO> getDishVoByCategoryId(Long categoryId) {
|
||||
List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
|
||||
List<DishVO> dishVOList = new ArrayList<>();
|
||||
for (Dish dish : dishes) {
|
||||
DishVO dishVO = new DishVO();
|
||||
BeanUtils.copyProperties(dish, dishVO);
|
||||
dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId()));
|
||||
dishVOList.add(dishVO);
|
||||
List<DishVO> dishVOList;
|
||||
|
||||
//construct key in redis: dish_{categoryId}
|
||||
String key = "dish_"+categoryId;
|
||||
|
||||
//check if dish data exists in redis
|
||||
ValueOperations valueOperations = redisTemplate.opsForValue();
|
||||
dishVOList = (List<DishVO>) valueOperations.get(key);
|
||||
|
||||
if (dishVOList != null && !dishVOList.isEmpty()) {
|
||||
//if exists,return dish data
|
||||
return dishVOList;
|
||||
}else {
|
||||
//if not exists
|
||||
List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
|
||||
dishVOList = new ArrayList<>();
|
||||
for (Dish dish : dishes) {
|
||||
DishVO dishVO = new DishVO();
|
||||
BeanUtils.copyProperties(dish, dishVO);
|
||||
dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId()));
|
||||
dishVOList.add(dishVO);
|
||||
}
|
||||
valueOperations.set(key, dishVOList);
|
||||
return dishVOList;
|
||||
}
|
||||
return dishVOList;
|
||||
}
|
||||
|
||||
private void cleanCache(String pattern) {
|
||||
//clear redis cache
|
||||
Set keys = redisTemplate.keys(pattern);
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user