dish cache

This commit is contained in:
2024-12-16 18:41:04 +08:00
parent 7747190fd9
commit 0b77bc7d70
6 changed files with 56 additions and 16 deletions

View File

@@ -19,7 +19,7 @@ public class RedisConfiguration {
redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置序列化器 //设置序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer()); // redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate; return redisTemplate;
} }
} }

View File

@@ -14,11 +14,11 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@RestController @RestController("UserCategoeyController")
@RequestMapping("/user/category") @RequestMapping("/user/category")
@Api(tags = "Category") @Api(tags = "Category")
@Slf4j @Slf4j
public class UserCategoryController { public class CategoryController {
@Autowired @Autowired
private CategoryService categoryService; private CategoryService categoryService;

View File

@@ -12,11 +12,11 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@RestController @RestController("UserDishController")
@RequestMapping("/user/dish") @RequestMapping("/user/dish")
@Api(tags = "UserDish") @Api(tags = "UserDish")
@Slf4j @Slf4j
public class UserDishController { public class DishController {
@Autowired @Autowired
private DishService dishService; private DishService dishService;

View File

@@ -16,11 +16,11 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@RestController @RestController("UserSetmealController")
@RequestMapping("/user/setmeal") @RequestMapping("/user/setmeal")
@Api(tags = "UserSetmeal") @Api(tags = "UserSetmeal")
@Slf4j @Slf4j
public class UserSetmealController { public class SetmealController {
@Autowired @Autowired
private SetmealService setmealService; private SetmealService setmealService;

View File

@@ -68,7 +68,7 @@ public interface DishMapper {
* @param categoryId * @param categoryId
* @return * @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> selectByCategoryId(Long categoryId);
List<Dish> selectByDishIdsAndStatusDisable(List<Long> dishIds); List<Dish> selectByDishIdsAndStatusDisable(List<Long> dishIds);

View File

@@ -17,16 +17,21 @@ import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper; import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult; import com.sky.result.PageResult;
import com.sky.service.DishService; import com.sky.service.DishService;
import com.sky.service.SetmealService;
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.beans.factory.annotation.Autowired; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
@Service @Service
@Slf4j @Slf4j
@@ -40,6 +45,8 @@ public class DishServiceImpl implements DishService {
private SetmealDishMapper setmealDishMapper; private SetmealDishMapper setmealDishMapper;
@Autowired @Autowired
private SetmealMapper setmealMapper; private SetmealMapper setmealMapper;
@Resource
private RedisTemplate redisTemplate;
/** /**
* 新增菜品 * 新增菜品
@@ -66,6 +73,9 @@ public class DishServiceImpl implements DishService {
dishFlavorMapper.insertBatch(flavors); 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); dishMapper.deleteByIds(ids);
dishFlavorMapper.deleteByDishIds(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); dishFlavorMapper.insertBatch(flavors);
} }
//clear redis cache
cleanCache("dish_*");
} }
/** /**
@@ -197,18 +213,42 @@ public class DishServiceImpl implements DishService {
} }
} }
} }
cleanCache("dish_*");
} }
@Override @Override
public List<DishVO> getDishVoByCategoryId(Long categoryId) { public List<DishVO> getDishVoByCategoryId(Long categoryId) {
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); List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
List<DishVO> dishVOList = new ArrayList<>(); dishVOList = new ArrayList<>();
for (Dish dish : dishes) { for (Dish dish : dishes) {
DishVO dishVO = new DishVO(); DishVO dishVO = new DishVO();
BeanUtils.copyProperties(dish, dishVO); BeanUtils.copyProperties(dish, dishVO);
dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId())); dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId()));
dishVOList.add(dishVO); 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);
}
} }