@Autowired
private RedisTemplate redisTemplate;
/**
* 根据上级 ID 查询列表
*/
@Override
public List<TbItemCat> findByParentId(Long parentId) {
TbItemCatExample example1=new TbItemCatExample();
Criteria criteria1 = example1.createCriteria();
criteria1.andParentIdEqualTo(parentId);
//每次执行查询的时候,一次性读取缓存进行存储 (因为每次增删改都要执行此方法)
List<TbItemCat> list = findAll();
for(TbItemCat itemCat:list){
redisTemplate.boundHashOps("itemCat").put(itemCat.getName(),
itemCat.getTypeId());
}
System.out.println("更新缓存:商品分类表");
return itemCatMapper.selectByExample(example1);
}
@Autowired
private RedisTemplate redisTemplate;
/**
* 将数据存入缓存
*/
private void saveToRedis(){
//获取模板数据
List<TbTypeTemplate> typeTemplateList = findAll();
//循环模板
for(TbTypeTemplate typeTemplate :typeTemplateList){
//存储品牌列表
List<Map> brandList = JSON.parseArray(typeTemplate.getBrandIds(),
Map.class);
redisTemplate.boundHashOps("brandList").put(typeTemplate.getId(),
brandList);
//存储规格列表
List<Map> specList = findSpecList(typeTemplate.getId());//根据模板 ID 查询
规格列表
redisTemplate.boundHashOps("specList").put(typeTemplate.getId(),
specList);
}
}
public PageResult findPage(TbTypeTemplate typeTemplate, int pageNum, int pageSize) {
......
saveToRedis();//存入数据到缓存
return new PageResult(page.getTotal(), page.getResult());
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |