document
API test
删除缓存-缓存一致性

更新分类

POST
http://localhost:88/api/product/category/update

Request Parameters

parameter
type
description
required
catId
int
示例:225
required
icon
string
示例:http:www.baidu.com
required
name
string
示例:手机1
required
productUnit
string
示例:1
required
showStatus
int
示例:1
required

Response Parameters

parameter
type
description
required
msg
string
示例:success
required
code
int
示例:0
required

Description or Example

# 核心代码 ```java @RequestMapping("/update") // @RequiresPermissions("product:category:update") public R update(@Validated(UpdateGroup.class) @RequestBody CategoryEntity category){ categoryService.updateSync(category); return R.ok(); } ``` ```java CategoryServiceImpl @Override @Transactional @CacheEvict(cacheNames = "categories", allEntries = true) // 将这个缓存空间下的所有数据都清空, 相当于把分类清空 // 失效策略, 当分类被更新的时候, 自动将缓存中的分裂信息删除, 因为无法确定更新的是哪级分类, 因此将分类所有的缓存都删除 public void updateSync(CategoryEntity category) { // 更新本表 this.updateById(category); Long catId = category.getCatId(); String name = category.getName(); // 同步更新 if (StringUtils.isNotBlank(name)) { categoryBrandRelationService.updateCategory(catId, name); } // TODO: 其他同步更新 } ``` ```java CategoryBrandRelationServiceImpl @Override public void updateCategory(Long catId, String name) { CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity(); categoryBrandRelationEntity.setCatelogId(catId).setCatelogName(name); LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId); // 更新 this.baseMapper.update(categoryBrandRelationEntity, updateWrapper); } ``` # 拓展知识 为什么需要事务? 为什么同步更新? *详情请看更新品牌*