public static final String LOCK_SHOP_TYPE_KEY = "cache:shop:type:"
@RestController
@RequestMapping("/shop-type")
public class ShopTypeController {
@Resource
private IShopTypeService typeService;
/**
* 给店铺类型添加缓存
* @return
*/
@GetMapping("list")
public Result queryTypeList() {
return typeService.queryList();
}
}
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* 给店铺类型添加缓存
* @return
*/
@Override
public Result queryList() {
List<String> shopTypes = stringRedisTemplate.opsForList().range(RedisConstants.LOCK_SHOP_TYPE_KEY, 0, -1);
if (!shopTypes.isEmpty()) {
List<ShopType> tmp = new ArrayList<>();
for (String types : shopTypes) {
ShopType shopType = JSONUtil.toBean(types, ShopType.class);
tmp.add(shopType);
}
return Result.ok(tmp);
}
List<ShopType> tmp = query().orderByAsc("sort").list();
if (tmp == null){
return Result.fail("店铺类型不存在!");
}
for (ShopType shopType : tmp) {
String jsonStr = JSONUtil.toJsonStr(shopType);
shopTypes.add(jsonStr);
}
stringRedisTemplate.opsForList().leftPushAll(RedisConstants.LOCK_SHOP_TYPE_KEY,shopTypes);
return Result.ok(tmp);
}
}
public interface IShopTypeService extends IService<ShopType> {
/**
* 给店铺类型添加缓存
* @return
*/
Result queryList();
}