商城项目分布式缓存使用Spring Cache原理实现以及应用的场景与不足-----商城项目

#你是如何克服编程学习中的挫折感的?#
package com.alatus.mall.product.service.impl;

import com.alatus.mall.product.service.CategoryBrandRelationService;
import com.alatus.mall.product.vo.SubCatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import com.alatus.mall.product.dao.CategoryDao;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
//        查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);
//        组装父子的树形结构
//        找到一级分类
        List<CategoryEntity> levelMenuOne = entities.stream().filter(categoryEntity ->
            categoryEntity.getParentCid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildren(menu,entities));
            return menu;
        }).sorted((menu1,menu2) -> {
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return levelMenuOne;
    }

    @Override
    public void removeMenuByIds(List<Long> list) {
//        检查当前删除的菜单,是否被别的地方引用
        baseMapper.deleteBatchIds(list);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParent(catelogId, paths);
        Collections.reverse(parentPath);
        return (Long[])parentPath.toArray(new Long[parentPath.size()]);
    }

//    SpringCache的不足,读模式,缓存穿透可以用配置允许空值解决
//    缓存击穿可以通过加锁解决,原理是大量并发进入一个过期的数据
//    缓存雪崩是多个缓存同时失效,加随机时间解决
//    写模式,缓存与数据库一致
//    使用读写加锁的方式,适合于读多写少的场景,引入canal,读多写多的更推荐直接去数据库
//    我们直接采用springCache的场景是读多写少,即时性一致性要求不高的数据,针对这个场景,缓存有过期时间即可
//    特殊数据(秒杀等)再采用特殊设计解决

//    级联更新所有数据
//    同时进行多个缓存操作
    @Override
    @Transactional
//    @Caching(evict = {
//            @CacheEvict(value = "category",key = "'getLevelCategories'"),
//            @CacheEvict(value = "category",key = "'getCatalogJson'")
//    })
//    只是删除可以使用另一种方式
//    存储同一类型的数据,我们都指定为同一个分区,且不要指定缓存前缀名
    @CacheEvict(value = "category",allEntries = true)
    public void updateCascade(CategoryEntity category) {

        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
//        双写:应该同时修改缓存中的数据
//        双写会导致,A线程写入新数据,还没写入完,B线程写入新数据,已经完成了双写,导致最后写入缓存的是A的老数据
//        删缓存:也可以考虑把缓存中的数据删掉的方式,等待下一次查询更新缓存中的数据
//        删缓存会导致A写入数据库,删除了缓存
//        B写入数据库,正在删缓存中,C读取缓存,导致A写入的数据库信息被读取到缓存了
//        B写入数据库后,又删除了A的数据,而C的读取又导致了A的数据被写进缓存
    }

//    用这个注解可以将当前方法的结果进行缓存,如果缓存中有,方法不用调用,如果缓存没有,就会执行方法,并存入缓存
//    每一个需要缓存的数据我们都要指定放到哪个名字的缓存,也可以叫做缓存的分区
//    分区应该叫分区类型,根据业务来分
//    缓存的key默认自动生成
//    默认值使用jdk序列化封装存到redis,默认ttl时间-1
//    我们在业务中想要实现以下的自定义,这个key我们需要指定,ttl时间我们需要指定,数据格式需要是json,便以其他语言系统用
//    key指定通过注解,ttl设置通过配置文件
    @Cacheable(value = {"category"},key = "#root.method.name")
    @Override
    public List<CategoryEntity> getLevelCategories(Integer catLevel) {
        return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("cat_level", catLevel));
    }

    // TODO 堆外内存溢出,原因是lettuce使用了netty做网络通信,lettuce的bug导致了我们的堆外内存溢出
//    缓存内的数据要考虑到和数据库的数据一致,需要考虑数据一致性问题
//        占分布式锁
//        锁的粒度,具体缓存的是某个数据
//        因为如果锁的名字撞了,实际上会导致多个不同的业务用了同一把锁
//        查个菜单列表信息结果要等商品信息数据查询完成
//    SpringCache实现避免缓存击穿的方式是本地获取数据查询时(其他时候都没有)会有一个本地锁syconz的,不是分布式锁
//    改数据存数据都没有这个本地锁,只有读取数据,以及没有数据被读取到,执行的存数据才有这个本地锁
    @Cacheable(value = {"category"},key = "#root.method.name",sync = true)//本地业务锁,避免缓存击穿
    @Override
    public Map<String, List<SubCatalogVo>> getCatalogJson() {
//        加入缓存机制
//        缓存中我们一律保存JSON字符串,因为JSON本身也是跨语言跨平台的
        //            缓存中没有
        List<CategoryEntity> categories = baseMapper.selectList(null);
        //        所有一级分类
        List<CategoryEntity> levelCategories = getParentCid(categories,0L);;
        //        封装数据
        return levelCategories.stream().collect(Collectors.toMap(k -> k.getCatId().toString(),v -> {
            List<CategoryEntity> entities = getParentCid(categories,v.getCatId());
            List<SubCatalogVo> subCatalogVos = null;
            if(entities!=null&&!entities.isEmpty()){
                subCatalogVos = entities.stream().map(item -> {
//                    找三级分类
                    List<CategoryEntity> inCategories = getParentCid(categories,item.getCatId());
                    SubCatalogVo subCatalogVo = new SubCatalogVo(v.getCatId().toString(),null, item.getCatId().toString(), item.getName());
                    if(inCategories!=null&&!inCategories.isEmpty()){
//                        封装成指定格式
                        List<SubCatalogVo.InCatalogVo> inCatalogVos = inCategories.stream().map(inCategory -> {
                            SubCatalogVo.InCatalogVo inCatalogVo = new SubCatalogVo.InCatalogVo(item.getCatId().toString(),inCategory.getCatId().toString(),inCategory.getName());
                            return inCatalogVo;
                        }).collect(Collectors.toList());
                        subCatalogVo.setCatalog3List(inCatalogVos);
                    }
                    return subCatalogVo;
                }).collect(Collectors.toList());
            }
            return subCatalogVos;
        }));
//        查到的数据保存到缓存中先
//        查到的数据放入缓存,将这个对象我们转为JSON
    }

    private List<CategoryEntity> getParentCid(List<CategoryEntity> categories,Long catId) {
        return categories.stream().filter(item -> item.getParentCid().equals(catId)).collect(Collectors.toList());
    }

    private List<Long> findParent(Long catelogId,List<Long> paths){
//        查出当前分类的ID
//        收集当前节点ID
        paths.add(catelogId);
        CategoryEntity id = this.getById(catelogId);
        if(id.getParentCid() != 0){
//            递归查找,每次都从父节点获取
            findParent(id.getParentCid(),paths);
        }
        return paths;
    }

    //    递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2) ->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}
package com.alatus.mall.product.service.impl;

import com.alatus.mall.product.service.CategoryBrandRelationService;
import com.alatus.mall.product.vo.SubCatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import com.alatus.mall.product.dao.CategoryDao;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
//        查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);
//        组装父子的树形结构
//        找到一级分类
        List<CategoryEntity> levelMenuOne = entities.stream().filter(categoryEntity ->
            categoryEntity.getParentCid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildren(menu,entities));
            return menu;
        }).sorted((menu1,menu2) -> {
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return levelMenuOne;
    }

    @Override
    public void removeMenuByIds(List<Long> list) {
//        检查当前删除的菜单,是否被别的地方引用
        baseMapper.deleteBatchIds(list);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParent(catelogId, paths);
        Collections.reverse(parentPath);
        return (Long[])parentPath.toArray(new Long[parentPath.size()]);
    }

//    SpringCache的不足,读模式,缓存穿透可以用配置允许空值解决
//    缓存击穿可以通过加锁解决,原理是大量并发进入一个过期的数据
//    缓存雪崩是多个缓存同时失效,加随机时间解决
//    写模式,缓存与数据库一致
//    使用读写加锁的方式,适合于读多写少的场景,引入canal,读多写多的更推荐直接去数据库
//    我们直接采用springCache的场景是读多写少,即时性一致性要求不高的数据,针对这个场景,缓存有过期时间即可
//    特殊数据(秒杀等)再采用特殊设计解决

//    级联更新所有数据
//    同时进行多个缓存操作
    @Override
    @Transactional
//    @Caching(evict = {
//            @CacheEvict(value = "category",key = "'getLevelCategories'"),
//            @CacheEvict(value = "category",key = "'getCatalogJson'")
//    })
//    只是删除可以使用另一种方式
//    存储同一类型的数据,我们都指定为同一个分区,且不要指定缓存前缀名
    @CacheEvict(value = "category",allEntries = true)
    public void updateCascade(CategoryEntity category) {

        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
//        双写:应该同时修改缓存中的数据
//        双写会导致,A线程写入新数据,还没写入完,B线程写入新数据,已经完成了双写,导致最后写入缓存的是A的老数据
//        删缓存:也可以考虑把缓存中的数据删掉的方式,等待下一次查询更新缓存中的数据
//        删缓存会导致A写入数据库,删除了缓存
//        B写入数据库,正在删缓存中,C读取缓存,导致A写入的数据库信息被读取到缓存了
//        B写入数据库后,又删除了A的数据,而C的读取又导致了A的数据被写进缓存
    }

//    用这个注解可以将当前方法的结果进行缓存,如果缓存中有,方法不用调用,如果缓存没有,就会执行方法,并存入缓存
//    每一个需要缓存的数据我们都要指定放到哪个名字的缓存,也可以叫做缓存的分区
//    分区应该叫分区类型,根据业务来分
//    缓存的key默认自动生成
//    默认值使用jdk序列化封装存到redis,默认ttl时间-1
//    我们在业务中想要实现以下的自定义,这个key我们需要指定,ttl时间我们需要指定,数据格式需要是json,便以其他语言系统用
//    key指定通过注解,ttl设置通过配置文件
    @Cacheable(value = {"category"},key = "#root.method.name")
    @Override
    public List<CategoryEntity> getLevelCategories(Integer catLevel) {
        return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("cat_level", catLevel));
    }

    // TODO 堆外内存溢出,原因是lettuce使用了netty做网络通信,lettuce的bug导致了我们的堆外内存溢出
//    缓存内的数据要考虑到和数据库的数据一致,需要考虑数据一致性问题
//        占分布式锁
//        锁的粒度,具体缓存的是某个数据
//        因为如果锁的名字撞了,实际上会导致多个不同的业务用了同一把锁
//        查个菜单列表信息结果要等商品信息数据查询完成
//    SpringCache实现避免缓存击穿的方式是本地获取数据查询时(其他时候都没有)会有一个本地锁syconz的,不是分布式锁
//    改数据存数据都没有这个本地锁,只有读取数据,以及没有数据被读取到,执行的存数据才有这个本地锁
    @Cacheable(value = {"category"},key = "#root.method.name",sync = true)//本地业务锁,避免缓存击穿
    @Override
    public Map<String, List<SubCatalogVo>> getCatalogJson() {
//        加入缓存机制
//        缓存中我们一律保存JSON字符串,因为JSON本身也是跨语言跨平台的
        //            缓存中没有
        List<CategoryEntity> categories = baseMapper.selectList(null);
        //        所有一级分类
        List<CategoryEntity> levelCategories = getParentCid(categories,0L);;
        //        封装数据
        return levelCategories.stream().collect(Collectors.toMap(k -> k.getCatId().toString(),v -> {
            List<CategoryEntity> entities = getParentCid(categories,v.getCatId());
            List<SubCatalogVo> subCatalogVos = null;
            if(entities!=null&&!entities.isEmpty()){
                subCatalogVos = entities.stream().map(item -> {
//                    找三级分类
                    List<CategoryEntity> inCategories = getParentCid(categories,item.getCatId());
                    SubCatalogVo subCatalogVo = new SubCatalogVo(v.getCatId().toString(),null, item.getCatId().toString(), item.getName());
                    if(inCategories!=null&&!inCategories.isEmpty()){
//                        封装成指定格式
                        List<SubCatalogVo.InCatalogVo> inCatalogVos = inCategories.stream().map(inCategory -> {
                            SubCatalogVo.InCatalogVo inCatalogVo = new SubCatalogVo.InCatalogVo(item.getCatId().toString(),inCategory.getCatId().toString(),inCategory.getName());
                            return inCatalogVo;
                        }).collect(Collectors.toList());
                        subCatalogVo.setCatalog3List(inCatalogVos);
                    }
                    return subCatalogVo;
                }).collect(Collectors.toList());
            }
            return subCatalogVos;
        }));
//        查到的数据保存到缓存中先
//        查到的数据放入缓存,将这个对象我们转为JSON
    }

    private List<CategoryEntity> getParentCid(List<CategoryEntity> categories,Long catId) {
        return categories.stream().filter(item -> item.getParentCid().equals(catId)).collect(Collectors.toList());
    }

    private List<Long> findParent(Long catelogId,List<Long> paths){
//        查出当前分类的ID
//        收集当前节点ID
        paths.add(catelogId);
        CategoryEntity id = this.getById(catelogId);
        if(id.getParentCid() != 0){
//            递归查找,每次都从父节点获取
            findParent(id.getParentCid(),paths);
        }
        return paths;
    }

    //    递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2) ->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}
  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值