商城项目引入Redis作为分布式缓存优化接口提升性能-----商城项目

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

import com.alatus.mall.product.service.CategoryBrandRelationService;
import com.alatus.mall.product.vo.SubCatalogVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
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;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @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()]);
    }

//    级联更新所有数据
    @Override
    @Transactional
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    @Override
    public List<CategoryEntity> getLevelCategories(Integer catLevel) {
        return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("cat_level", catLevel));
    }

    private Map<String, List<SubCatalogVo>> getCatalogJsonFromDb(){
        List<CategoryEntity> categories = baseMapper.selectList(null);
//        所有一级分类
        List<CategoryEntity> levelCategories = getParentCid(categories,0L);;
//        封装数据
        Map<String, List<SubCatalogVo>> subCatalogVosMap = 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;
        }));
        return subCatalogVosMap;
    }

    @Override
    public Map<String, List<SubCatalogVo>> getCatalogJson() {
//        加入缓存机制
//        缓存中我们一律保存JSON字符串,因为JSON本身也是跨语言跨平台的
        String catalogJSON = redisTemplate.opsForValue().get("catalogJSON");
        if(StringUtils.isEmpty(catalogJSON)){
//            缓存中没有
            Map<String, List<SubCatalogVo>> catalogJsonFromDb = this.getCatalogJsonFromDb();
//            查到的数据放入缓存,将这个对象我们转为JSON
            String jsonString = JSON.toJSONString(catalogJsonFromDb);
            redisTemplate.opsForValue().set("catalogJSON",jsonString);
            return catalogJsonFromDb;
        }
        return JSON.parseObject(catalogJSON,new TypeReference<Map<String, List<SubCatalogVo>>>(){});
    }

    private List<CategoryEntity> getParentCid(List<CategoryEntity> categories,Long catId) {
        return categories.stream().filter(item -> item.getParentCid() == 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() == 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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
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;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @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()]);
    }

//    级联更新所有数据
    @Override
    @Transactional
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    @Override
    public List<CategoryEntity> getLevelCategories(Integer catLevel) {
        return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("cat_level", catLevel));
    }

    private Map<String, List<SubCatalogVo>> getCatalogJsonFromDb(){
        List<CategoryEntity> categories = baseMapper.selectList(null);
//        所有一级分类
        List<CategoryEntity> levelCategories = getParentCid(categories,0L);;
//        封装数据
        Map<String, List<SubCatalogVo>> subCatalogVosMap = 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;
        }));
        return subCatalogVosMap;
    }

    @Override
    public Map<String, List<SubCatalogVo>> getCatalogJson() {
//        加入缓存机制
//        缓存中我们一律保存JSON字符串,因为JSON本身也是跨语言跨平台的
        String catalogJSON = redisTemplate.opsForValue().get("catalogJSON");
        if(StringUtils.isEmpty(catalogJSON)){
//            缓存中没有
            Map<String, List<SubCatalogVo>> catalogJsonFromDb = this.getCatalogJsonFromDb();
//            查到的数据放入缓存,将这个对象我们转为JSON
            String jsonString = JSON.toJSONString(catalogJsonFromDb);
            redisTemplate.opsForValue().set("catalogJSON",jsonString);
            return catalogJsonFromDb;
        }
        return JSON.parseObject(catalogJSON,new TypeReference<Map<String, List<SubCatalogVo>>>(){});
    }

    private List<CategoryEntity> getParentCid(List<CategoryEntity> categories,Long catId) {
        return categories.stream().filter(item -> item.getParentCid() == 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() == 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;
    }

}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.Alatus.Mall</groupId>
        <artifactId>Mall</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.alatus</groupId>
    <artifactId>Mall-common</artifactId>
    <url>http://maven.apache.org</url>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>
    <description>
        每一个微服务公共依赖,Bean,工具类
    </description>
    <dependencies>
<!--        mybatis-Plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-generator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
        <!--        模板引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<!--        httpComponent-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>
<!--        commons-lang-->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
<!--        jdbc-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
<!--        servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>
<!--        nacos配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
<!--        nacos发现中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
<!--        openfiegn远程调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
<!--        spring-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <version>1.0.0</version>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.Alatus.Mall</groupId>
        <artifactId>Mall</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.alatus</groupId>
    <artifactId>Mall-common</artifactId>
    <url>http://maven.apache.org</url>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>
    <description>
        每一个微服务公共依赖,Bean,工具类
    </description>
    <dependencies>
<!--        mybatis-Plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-generator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
        <!--        模板引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<!--        httpComponent-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>
<!--        commons-lang-->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
<!--        jdbc-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
<!--        servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>
<!--        nacos配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
<!--        nacos发现中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
<!--        openfiegn远程调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
<!--        spring-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <version>1.0.0</version>
</project>
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
  datasource:
    username: root
    password: root
    url: jdbc:mysql://192.168.56.10:3306/Mall-pms
    driver-class-name: com.mysql.cj.jdbc.Driver
  thymeleaf:
    cache: true
  mvc:
    static-path-pattern: /static/**
  redis:
    host: 192.168.56.10
    port: 6379
feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 1
      logic-not-delete-value: 0
server:
  port: 10000
logging:
  level:
    com.alatus.mall.product: error
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
  datasource:
    username: root
    password: root
    url: jdbc:mysql://192.168.56.10:3306/Mall-pms
    driver-class-name: com.mysql.cj.jdbc.Driver
  thymeleaf:
    cache: true
  mvc:
    static-path-pattern: /static/**
  redis:
    host: 192.168.56.10
    port: 6379
feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 1
      logic-not-delete-value: 0
server:
  port: 10000
logging:
  level:
    com.alatus.mall.product: error
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值