@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public Result queryAll() {
String key=CACHE_SHOP_TYPE;
List<String> shopTypeList= stringRedisTemplate.opsForList().range(key,0,-1);
List<ShopType> res =shopTypeList.stream().map(s->{
ShopType shopType=JSONUtil.toBean(s, ShopType.class);
return shopType;
}).collect(Collectors.toList());
if(shopTypeList!=null &&!shopTypeList.isEmpty()){
return Result.ok(res);
}
List<ShopType> shopTypes = query().orderByAsc("sort").list();
if(shopTypes==null||shopTypes.isEmpty()){
return Result.fail("首页数据异常");
}
stringRedisTemplate.opsForList().rightPushAll(key, shopTypes.stream().map(JSONUtil::toJsonStr).collect(Collectors.toList()));
return Result.ok(shopTypes);
}
}
黑马点评Redis的p37作业(list实现)
于 2024-03-02 21:00:02 首次发布
本文介绍了一个使用SpringData和Redis的Java服务,通过`queryAll`方法查询ShopType,优先从Redis缓存中获取,提高性能。
摘要由CSDN通过智能技术生成