redis的应用(三)与solr的联合查询

文章目录

      • 需求分析
      • 缓存实现
      • 前端接口查询缓存的实现
      • 测试

需求分析

根据搜索关键字,页面显示相关的商品分类,根据相关的商品分类借助分类模板从后台查询出品牌、规格以及规格选项进行显示
为了应对高并发,将根据相关的商品分类借助分类模板从后台查询出品牌、规格以及规格选项保存到redis数据库中,查询的时候不再从关系数据库中查询,直接从更快的redis数据库中查询
涉及到的数据库表有三个:
redis的应用(三)与solr的联合查询_第1张图片
在这里插入图片描述
redis的应用(三)与solr的联合查询_第2张图片

缓存实现

缓存这些数据要在运营商后台管理中进行(因为运营商后台管理对这些数据进行增删改查的操作),查询这些数据是在搜索服务中调用,然后提供给搜索页面

在原来的分类查询业务的基础上加入商品分类缓存

  • 加入缓存
  • 在商品搜索模块,需要根据 分类名 得到模板Id
  • 因此,在操作分类的模块中,我们提前将分类数据缓存起来
  • 缓存规则:分类名作为 小键, 以模板id作为值
@Autowired
private RedisTemplate redisTemplate;

public List<TbItemCat> findByParentId(Long pId) {
	TbItemCatExample example = new TbItemCatExample();
	Criteria criteria = example.createCriteria();
	criteria.andParentIdEqualTo(pId);
	List<TbItemCat> itemCats = itemCatMapper.selectByExample(example);
	
	//加入缓存
	Set keys = redisTemplate.boundHashOps("categoryList").keys();
	if (keys!=null && keys.size()>0){

	}else {//没有任何的键,开始缓存
		List<TbItemCat> allCats = findAll();
		for (TbItemCat cat : allCats) {
			redisTemplate.boundHashOps("categoryList").put(cat.getName(),cat.getTypeId());
		}
		System.out.println(">>>>>添加分类信息至缓存");
	}
	return itemCats;
}

在原来的分类模板查询业务的基础上加入商品分类模板缓存

  • 商品搜索模块,需要根据模板id,得到品牌、规格列表
  • 所以,在此处,我们将模板的信息加入缓存
  • 缓存的规则:
  • 针对品牌:取一个品牌的大键brandList,然后以模板id作为小键,品牌列表作为值
  • 针对规格:取一个规格的大键specList,然后以模板id作为小键,规格 + 规格选项 的列表作为值
@Autowired
private RedisTemplate redisTemplate;

public PageResult findPage(TbTypeTemplate typeTemplate, int pageNum, int pageSize) {
	PageHelper.startPage(pageNum, pageSize);
	
	TbTypeTemplateExample example=new TbTypeTemplateExample();
	Criteria criteria = example.createCriteria();
	
	if(typeTemplate != null){			
					if(typeTemplate.getName() != null && typeTemplate.getName().length() > 0){
			criteria.andNameLike("%" + typeTemplate.getName() + "%");
		}			if(typeTemplate.getSpecIds() != null && typeTemplate.getSpecIds().length() > 0){
			criteria.andSpecIdsLike("%" + typeTemplate.getSpecIds() + "%");
		}			if(typeTemplate.getBrandIds() != null && typeTemplate.getBrandIds().length() > 0){
			criteria.andBrandIdsLike("%" + typeTemplate.getBrandIds() + "%");
		}			if(typeTemplate.getCustomAttributeItems() != null && typeTemplate.getCustomAttributeItems().length() > 0){
			criteria.andCustomAttributeItemsLike("%" + typeTemplate.getCustomAttributeItems() + "%");
		}
	}

	Page<TbTypeTemplate> page= (Page<TbTypeTemplate>)typeTemplateMapper.selectByExample(example);
	List<TbTypeTemplate> list = null;
	Set brandKeys = redisTemplate.boundHashOps("brandList").keys();
	if (brandKeys!=null && brandKeys.size()>0){

	}else{//缓存信息中没有键,开始缓存
		list = findAll();
		for (TbTypeTemplate template : list) {
			List<Map> brands = JSON.parseArray(template.getBrandIds(), Map.class);
			redisTemplate.boundHashOps("brandList").put(template.getId(),brands);
		}
	}

	Set specKeys = redisTemplate.boundHashOps("specList").keys();
	if (specKeys!=null && specKeys.size()>0){

	}else{//缓存信息中没有键,开始缓存
		if (list==null){
			list = findAll();
		}
		for (TbTypeTemplate template : list) {
			List<Map> specMaps = findSpecAndOptionsByTypeTemplateId(template.getId());
			redisTemplate.boundHashOps("specList").put(template.getId(),specMaps);
		}
		System.out.println(">>>>>添加规格列表至缓存");
	}
	return new PageResult(page.getTotal(), page.getResult());
}
public List<Map> findSpecAndOptionsByTypeTemplateId(Long typeTemplateId) {
	TbTypeTemplate tbTypeTemplate = typeTemplateMapper.selectByPrimaryKey(typeTemplateId);
	// List的格式:[{"id":26,"text":"尺码"},{"id":29,"text":"颜色"}]
	List<Map> maps = JSON.parseArray(tbTypeTemplate.getSpecIds(), Map.class);
	// map的格式:{"id":26,"text":"尺码"}
	for (Map map : maps) {

		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(Long.parseLong(map.get("id")+""));
		List<TbSpecificationOption> specificationOptions = specificationOptionMapper.selectByExample(example);
		map.put("options",specificationOptions);
		// map的新格式变为:{"id":26,"text":"尺码", "options": [{id: 24, optionName: ""56寸}]}
	}
	return maps;
}

前端接口查询缓存的实现

@Autowired
private RedisTemplate redisTemplate;

public Map<String, Object> search(Map searchMap) {

    Map<String,Object> map = new HashMap<>();
    //关键字高亮
    highlight(searchMap,map);
    //根据关键字获取分类
    categoryList(searchMap,map);
    
    
    // 如果页面传递过来了分类名,就不使用默认的第一个分类查询品牌、规格了
    if (!"".equals(searchMap.get("category"))){
        brandAndSpecListSearch((String)searchMap.get("category"),map);
    }else {
        //查询品牌规格列表
        List<String> categoryList = (List<String>) map.get("categoryList");
        if (categoryList.size()>0){
            brandAndSpecListSearch(categoryList.get(0),map);
        }
    }
    return map;
}

测试

在这里插入图片描述
控制台打印
在这里插入图片描述
redis的应用(三)与solr的联合查询_第3张图片
在这里插入图片描述
redis的应用(三)与solr的联合查询_第4张图片
搜索之前
在这里插入图片描述
搜索之后
redis的应用(三)与solr的联合查询_第5张图片

{
  "categoryList": [
    "电子书",
    "牙膏/牙粉",
    "手机"
  ],
  "specList": [
    {
      "options": [
        {
          "id": 98,
          "optionName": "移动3G",
          "orders": 1,
          "specId": 27
        },
        {
          "id": 99,
          "optionName": "移动4G",
          "orders": 2,
          "specId": 27
        },
        {
          "id": 100,
          "optionName": "联通3G",
          "orders": 3,
          "specId": 27
        },
        {
          "id": 101,
          "optionName": "联通4G",
          "orders": 4,
          "specId": 27
        },
        {
          "id": 112,
          "optionName": "电信3G",
          "orders": 5,
          "specId": 27
        },
        {
          "id": 113,
          "optionName": "电信4G",
          "orders": 6,
          "specId": 27
        },
        {
          "id": 114,
          "optionName": "移动2G",
          "orders": 7,
          "specId": 27
        },
        {
          "id": 115,
          "optionName": "联通2G",
          "orders": 8,
          "specId": 27
        },
        {
          "id": 116,
          "optionName": "电信2G",
          "orders": 9,
          "specId": 27
        },
        {
          "id": 117,
          "optionName": "双卡",
          "orders": 10,
          "specId": 27
        }
      ],
      "id": 27,
      "text": "网络"
    },
    {
      "options": [
        {
          "id": 118,
          "optionName": "16G",
          "orders": 1,
          "specId": 32
        },
        {
          "id": 119,
          "optionName": "32G",
          "orders": 2,
          "specId": 32
        },
        {
          "id": 120,
          "optionName": "64G",
          "orders": 3,
          "specId": 32
        },
        {
          "id": 121,
          "optionName": "128G",
          "orders": 4,
          "specId": 32
        }
      ],
      "id": 32,
      "text": "机身内存"
    }
  ],
  "brandList": [
    {
      "id": 1,
      "text": "联想"
    },
    {
      "id": 3,
      "text": "三星"
    },
    {
      "id": 2,
      "text": "华为"
    },
    {
      "id": 5,
      "text": "OPPO"
    },
    {
      "id": 4,
      "text": "小米"
    },
    {
      "id": 9,
      "text": "苹果"
    },
    {
      "id": 8,
      "text": "魅族"
    },
    {
      "id": 6,
      "text": "360"
    },
    {
      "id": 10,
      "text": "VIVO"
    },
    {
      "id": 11,
      "text": "诺基亚"
    },
    {
      "id": 12,
      "text": "锤子"
    }
  ],
  "rows": [
    {
      "barcode": null,
      "brand": "联想",
      "cartThumbnail": null,
      "category": "电子书",
      "categoryid": null,
      "costPirce": null,
      "createTime": null,
      "goodsId": 149187842867963,
      "id": 1369288,
      "image": "http://192.168.2.123/group1/M00/00/00/wKgCe17TrT-AfRG1AADFbi0oR0A460.jpg",
      "isDefault": null,
      "itemSn": null,
      "marketPrice": null,
      "num": null,
      "price": 0,
      "sellPoint": null,
      "seller": "测试",
      "sellerId": null,
      "spec": null,
      "specMap": {
        "jishenneicun": "16G",
        "wangluo": "移动4G"
      },
      "status": null,
      "stockCount": null,
      "title": "测试电子书nullnull",
      "updateTime": "2020-06-01 10:56:47"
    },
    {
      "barcode": null,
      "brand": "联想",
      "cartThumbnail": null,
      "category": "电子书",
      "categoryid": null,
      "costPirce": null,
      "createTime": null,
      "goodsId": 149187842867963,
      "id": 1369289,
      "image": "http://192.168.2.123/group1/M00/00/00/wKgCe17TrT-AfRG1AADFbi0oR0A460.jpg",
      "isDefault": null,
      "itemSn": null,
      "marketPrice": null,
      "num": null,
      "price": 0,
      "sellPoint": null,
      "seller": "测试",
      "sellerId": null,
      "spec": null,
      "specMap": {
        "jishenneicun": "16G",
        "wangluo": "双卡"
      },
      "status": null,
      "stockCount": null,
      "title": "测试电子书nullnull",
      "updateTime": "2020-06-01 10:56:47"
    },
    {
      "barcode": null,
      "brand": "联想",
      "cartThumbnail": null,
      "category": "电子书",
      "categoryid": null,
      "costPirce": null,
      "createTime": null,
      "goodsId": 149187842867963,
      "id": 1369290,
      "image": "http://192.168.2.123/group1/M00/00/00/wKgCe17TrT-AfRG1AADFbi0oR0A460.jpg",
      "isDefault": null,
      "itemSn": null,
      "marketPrice": null,
      "num": null,
      "price": 0,
      "sellPoint": null,
      "seller": "测试",
      "sellerId": null,
      "spec": null,
      "specMap": {
        "jishenneicun": "16G",
        "wangluo": "移动3G"
      },
      "status": null,
      "stockCount": null,
      "title": "测试电子书nullnull",
      "updateTime": "2020-06-01 10:56:47"
    },
    {
      "barcode": null,
      "brand": "小米",
      "cartThumbnail": null,
      "category": "电子书",
      "categoryid": null,
      "costPirce": null,
      "createTime": null,
      "goodsId": 149187842867964,
      "id": 1369287,
      "image": "http://192.168.2.123/group1/M00/00/00/wKgCe17TsLWAAWwDAADFbi0oR0A073.jpg",
      "isDefault": null,
      "itemSn": null,
      "marketPrice": null,
      "num": null,
      "price": 30,
      "sellPoint": null,
      "seller": "测试",
      "sellerId": null,
      "spec": null,
      "specMap": null,
      "status": null,
      "stockCount": null,
      "title": "电子书2",
      "updateTime": "2020-06-01 10:56:17"
    },
    {
      "barcode": null,
      "brand": "魅族",
      "cartThumbnail": null,
      "category": "牙膏/牙粉",
      "categoryid": null,
      "costPirce": null,
      "createTime": null,
      "goodsId": 149187842867955,
      "id": 1369279,
      "image": "http://192.168.25.133/group1/M00/00/00/wKgZhVnGZfWAaX2hAAjlKdWCzvg173.jpg",
      "isDefault": null,
      "itemSn": null,
      "marketPrice": null,
      "num": null,
      "price": 100,
      "sellPoint": null,
      "seller": "千度商店",
      "sellerId": null,
      "spec": null,
      "specMap": null,
      "status": null,
      "stockCount": null,
      "title": "测试商品110",
      "updateTime": "2017-09-23 21:47:51"
    },
    {
      "barcode": null,
      "brand": "中国移动",
      "cartThumbnail": null,
      "category": "手机",
      "categoryid": null,
      "costPirce": null,
      "createTime": null,
      "goodsId": 1,
      "id": 1039296,
      "image": "http://img12.360buyimg.com/n1/s450x450_jfs/t3034/299/2060854617/119711/577e85cb/57d11b6cN1fd1194d.jpg",
      "isDefault": null,
      "itemSn": null,
      "marketPrice": null,
      "num": null,
      "price": 2699,
      "sellPoint": null,
      "seller": "中国移动",
      "sellerId": null,
      "spec": null,
      "specMap": {
        "jishenneicun": "16G",
        "wangluo": "双卡"
      },
      "status": null,
      "stockCount": null,
      "title": "合约惠机测试手机(请勿下单)",
      "updateTime": "2015-03-08 21:33:18"
    }
  ]
}

你可能感兴趣的:(分布式电商项目,redis,solr)