商品类目和商品大广告的Redis缓存

(dubbo)主要的实现类:

 

商品类目的Redis缓存

com.bjsxt.ego.portal.service.impl.PortalItemCatServiceImpl

package com.bjsxt.ego.portal.service.impl;

import com.bjsxt.ego.beans.CatNode;
import com.bjsxt.ego.beans.CatResult;
import com.bjsxt.ego.beans.JsonUtils;
import com.bjsxt.ego.portal.service.PortalItemCatService;
import com.bjsxt.ego.rpc.pojo.TbItemCat;
import com.bjsxt.ego.rpc.service.ItemCatService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import java.util.ArrayList;
import java.util.List;

@Service
public class PortalItemCatServiceImpl implements PortalItemCatService {
    @Autowired
    private ItemCatService itemCatServiceProxy;

    @Value("${ITEM_CAT}")
    private String itemCatkey;

    @Autowired
    private JedisCluster cluster;

    @Override
    public String loadItemCatService() {

        String jsonstr = cluster.get(itemCatkey);
        if (!StringUtils.isEmpty(jsonstr)){
            return jsonstr;
        }


        List<TbItemCat> list = itemCatServiceProxy.loadItemCatListService();
        //创建CatResult对象
        CatResult result=new CatResult();

        //将List转化为符合前端规范数据格式,递归遍历list
        List<?> date=getChildren(0L,list);

        result.setData(date);

        //将result对象序列化json字符串
        String str = JsonUtils.objectToJson(result);

        //将str缓存到Redis集群中
        cluster.set(itemCatkey,str);
        return str;
    }

    private List<?> getChildren(long parentId, List<TbItemCat> itemCats) {
        // 盛放指定分类下的所有子分类信息
        List resultList = new ArrayList<>();
        for (TbItemCat itemCat:itemCats){
            if (itemCat.getParentId().equals(parentId)){
                if (itemCat.getIsParent()){
                //如果itemCat代表一级分类或者二级分类
                    CatNode catNode=new CatNode();
                    if (itemCat.getParentId().longValue()==0){
                        // 如果是一级分类 "<a href='/products/1.html'>图书、音像、电子书刊</a>",
                        catNode.setName(
                                "<a href='/products/" + itemCat.getId() + ".html'>" + itemCat.getName() + "</a>");
                    }else {
                        // 如果是二级分类 "电子书刊",
                        catNode.setName(itemCat.getName());
                    }
                    // "/products/2.html",
                    catNode.setUrl("/products/" + itemCat.getId() + ".html");
                    catNode.setList(getChildren(itemCat.getId(),itemCats));
                    // 将节点添加到list集合中
                    resultList.add(catNode);
                }else {
                    // 如果itemCat表示三级分类 "/products/3.html|电子书",
                    resultList.add("/products/" + itemCat.getId() + ".html|" + itemCat.getName());
                }
            }
        }
        return resultList;
    }
}

 

 

大广告的Redis缓存:

com.bjsxt.ego.portal.service.impl.PortalContentServiceImpl

package com.bjsxt.ego.portal.service.impl;

import com.bjsxt.ego.beans.BigPicture;
import com.bjsxt.ego.beans.JsonUtils;
import com.bjsxt.ego.portal.service.PortalContentService;
import com.bjsxt.ego.rpc.pojo.TbContent;
import com.bjsxt.ego.rpc.service.TbContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import java.util.ArrayList;
import java.util.List;

@Service
public class PortalContentServiceImpl implements PortalContentService {
    @Autowired
    private TbContentService tbContentServiceProxy;

    @Value("${CONTENT_PICTURE}")
    private String contentPicturekey;

    //注入JedisCluster
    @Autowired
    private JedisCluster cluster;


    @Override
    public String loadContentListByCidService(Long cid) {
        //查询Redis数据库
        String jesonStr = cluster.get(contentPicturekey);
        if (!StringUtils.isEmpty(jesonStr)){
            return jesonStr;
        }

        List<TbContent> list = tbContentServiceProxy.loadTbContentListByCidService(cid);
        //封装前台数据格式的广告数据
        List<BigPicture> bigList=new ArrayList<>();
        for (TbContent content:list){
                BigPicture picture=new BigPicture();
                picture.setSrcb(content.getPic());
                picture.setHeight(240);
                picture.setAlt(content.getTitle());
                picture.setWidth(670);
                picture.setSrc(content.getPic2());
                picture.setWidthb(550);
                picture.setHref(content.getUrl());
                picture.setHeightb(240);
                bigList.add(picture);
        }
        String s = JsonUtils.objectToJson(bigList);

        //将str保存到redis缓存
        cluster.set(contentPicturekey,s);
        cluster.expire(contentPicturekey,86400);
        return s;
    }
}

 

接口:

com.bjsxt.ego.portal.service.PortalContentService

package com.bjsxt.ego.portal.service;

public interface PortalContentService {
    /**
     * 返回某个内容类目,对应的内容数据
     * @param cid
     * @return
     */
    public String loadContentListByCidService(Long cid);
}

com.bjsxt.ego.rpc.service.TbContentService

/**
     * 加载某个类目对应的内容列表
     * @param cid
     * @return
     */
    public List<TbContent> loadTbContentListByCidService(Long cid);

 (实现类)com.bjsxt.ego.rpc.service.impl.TbContentServiceImpl

@Override
    public List<TbContent> loadTbContentListByCidService(Long cid) {
        try {
            TbContentExample example=new TbContentExample();
            TbContentExample.Criteria c = example.createCriteria();
            c.andCategoryIdEqualTo(cid);
            List<TbContent> tbContents = tbContentMapper.selectByExampleWithBLOBs(example);
            return tbContents;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

 

 

com.bjsxt.ego.portal.service.PortalItemCatService

package com.bjsxt.ego.portal.service;

public interface PortalItemCatService {

    /**
     * 加载前台首页的商品类目
     * @return
     */
    public String loadItemCatService();
}

com.bjsxt.ego.rpc.service.ItemCatService

/**
     * 加载门户首页的商品类目
     * @return
     */
    public List<TbItemCat> loadItemCatListService();

(实现类)com.bjsxt.ego.rpc.service.impl.ItemCatServiceImpl 

 @Override
    public List<TbItemCat> loadItemCatListService() {
        TbItemCatExample example=new TbItemCatExample();
        return itemCatMapper.selectByExample(example);
    }

 

配置文件(Redis集群)

spring/applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载cache.properties-->
	<context:property-placeholder location="classpath:cache.properties"/>
<!--实例化JedisCluster-->
	<bean id="cluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="nodes">
			<set>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.181.130"/>
					<constructor-arg name="port" value="6380"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.181.130"/>
					<constructor-arg name="port" value="6381"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.181.130"/>
					<constructor-arg name="port" value="6382"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.181.130"/>
					<constructor-arg name="port" value="6383"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.181.130"/>
					<constructor-arg name="port" value="6384"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.181.130"/>
					<constructor-arg name="port" value="6385"/>
				</bean>
			</set>
		</constructor-arg>
	</bean>
</beans>

 

 

 

控制台没有SQL语句输出,说明是从集群中取出的数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值