电商项目(四)

一、缓存

我们知道使用缓存,可以提高查询效率,那什么情况下需要使用缓存呢?通常而言,使用缓存需满足以下两个条件:

(1)查询频率较高的数据。

(2)修改频率较低的数据。


对于第一点,在我们开发过程中,如果查询业务比较多,需要频繁的连接数据库,这会对数据库的性能带来极大的损耗;这个时候可以考虑对这部分数据添加缓存。


对于第二点,如果我们业务中,需要频繁的修改某个数据,这个时候是不适合给它添加缓存的,因为每次修改了数据,都需要去更新缓存。


综合上面两点,我们需要给导航菜单、广告位投放的内容添加缓存。


[if !supportLists]1.2         [endif]缓存逻辑实现

[if !supportLists]1.2.1       [endif]第一部分:添加缓存

需求:在REST接口中,给导航菜单、首页大广告位内容添加缓存。缓存逻辑在ego-rest工程中实现。

[if !vml]

[endif]

[if !supportLists]1.2.1.1    [endif]第一步:添加redis的jar依赖


<dependency>

    <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

</dependency>


[if !supportLists]1.2.1.2     [endif]第二步:Spring整合Redis集群

在src路径下,添加spring-jedis.xml配置文件,整合redis。

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="

       

  http://www.springframework.org/schema/beans

       

  http://www.springframework.org/schema/beans/spring-beans.xsd

       

  http://www.springframework.org/schema/mvc

       

  http://www.springframework.org/schema/mvc/spring-mvc.xsd

      

    http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/tx

     

     http://www.springframework.org/schema/tx/spring-tx.xsd

       

  http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd">


      <!-- 连接池配置 -->

    <beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">

        <!-- 最大连接数 -->

        <propertyname="maxTotal"value="30"/>

        <!-- 最大空闲连接数 -->

        <propertyname="maxIdle"value="10"/>

        <!-- 每次释放连接的最大数目 -->

        <propertyname="numTestsPerEvictionRun"value="1024"/>

        <!-- 释放连接的扫描间隔(毫秒) -->

        <propertyname="timeBetweenEvictionRunsMillis"value="30000"/>

        <!-- 连接最小空闲时间 -->

        <propertyname="minEvictableIdleTimeMillis"value="1800000"/>

        <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->

        <propertyname="softMinEvictableIdleTimeMillis"value="10000"/>

        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->

        <propertyname="maxWaitMillis"value="1500"/>

        <!-- 在获取连接的时候检查有效性, 默认false

  -->

        <propertyname="testOnBorrow"value="true"/>

        <!-- 在空闲时检查有效性, 默认false

  -->

        <propertyname="testWhileIdle"value="true"/>

        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->

        <propertyname="blockWhenExhausted"value="false"/>

    </bean> 


    <beanid="jedisCluster"class="redis.clients.jedis.JedisCluster">

        <constructor-argindex="0">

             <set>

                 <beanclass="redis.clients.jedis.HostAndPort">

                     <constructor-argindex="0"value="192.168.23.12"></constructor-arg>

                     <constructor-argindex="1"value="7001"></constructor-arg>

                 </bean>

                 <beanclass="redis.clients.jedis.HostAndPort">

                     <constructor-argindex="0"value="192.168.23.12"></constructor-arg>

                     <constructor-argindex="1"value="7002"></constructor-arg>

                 </bean>

                 <beanclass="redis.clients.jedis.HostAndPort">

                     <constructor-argindex="0"value="192.168.23.12"></constructor-arg>

                     <constructor-argindex="1"value="7003"></constructor-arg>

                 </bean>

                 <beanclass="redis.clients.jedis.HostAndPort">

                     <constructor-argindex="0"value="192.168.23.12"></constructor-arg>

                     <constructor-argindex="1"value="7004"></constructor-arg>

                 </bean>

                 <beanclass="redis.clients.jedis.HostAndPort">

                     <constructor-argindex="0"value="192.168.23.12"></constructor-arg>

                     <constructor-argindex="1"value="7005"></constructor-arg>

                 </bean>

                 <beanclass="redis.clients.jedis.HostAndPort">

                     <constructor-argindex="0"value="192.168.23.12"></constructor-arg>

                     <constructor-argindex="1"value="7006"></constructor-arg>

                 </bean>

             </set>

        </constructor-arg>

        <constructor-argindex="1"ref="jedisPoolConfig"></constructor-arg>

    </bean>

</beans>


[if !supportLists]1.2.1.3     [endif]第三步:修改ContentServiceImpl

@Service

public class ContentServiceImpl implements ContentService{


    private String EGO_CONTENT = "EGO_CONTENT";


    @Autowired

    private JedisCluster jedisCluster;


    @Autowired

    private ContentMapper mapper;


    /*

     *缓存逻辑

     *   

  (1)首先查找缓存。

     *   

  (2)如果缓存有数据,则直接返回数据,不需要查询数据库

     *   

  (3)如果缓存中没有数据,则查询数据库。并且将数据放入缓存中。

     *   

     *   

  (4)缓存不能影响正常的业务执行,即当前缓存无法使用,则直接查询数据库

     *

     *确定选用哪一个数据结构类型(string,list,set,hash)

     *选用数据结构类型的时候,有一个原则:能用hash尽量使用hash。原因:减少key的数量,在寻址的时候速度更快。

     *

     * {key:{field:value}}  

     *我们这里使用hash数据结构类型,key定义成一个常量EGO_CONTENT   field就使用类型分类的id,value使用内容列表的json格式。

     *

     *注意:hash结构只能存储string格式的数据

     *

     */

    @Override

    public EgoResult getContentByCatId(Long catId) {


       Listlist = null;

       try {


           //1、查找缓存

           StringjsonData = jedisCluster.hget(EGO_CONTENT, catId+"");


           if(null!=jsonData && !"".equals(jsonData)){

              //缓存里面有数据,则直接返回数据即可

              list= JsonUtils.jsonToList(jsonData, Content.class);

           }else{

              //如果缓存中查不到数据,则查询数据库

              MapcolumnMap = new HashMap<>();

              columnMap.put("category_id", catId);

              list = mapper.selectByMap(columnMap);

              //再将数据放入缓存中

              jedisCluster.hset(EGO_CONTENT, catId+"", JsonUtils.objectToJson(list));

           }


       }catch (Exception e) {

           e.printStackTrace();

           //如果缓存中查不到数据,则查询数据库

           MapcolumnMap = new HashMap<>();

           columnMap.put("category_id", catId);


           list = mapper.selectByMap(columnMap);

       }


       returnEgoResult.ok(list);

    }

}


[if !supportLists]1.2.1.4     [endif]第四步:测试

(1)重启rest工程

(2)访问portal工程首页。将缓存添加到redis中。

(3)再次访问portal工程首页。查看缓存是否生效。


[if !supportLists]1.2.2       [endif]第二部分:缓存同步

修改、更新导航菜单、网站内容后,要同步修改缓存,或者清空对应的缓存。

[if !vml]

[endif]


[if !supportLists]2            [endif]搜索系统实现

[if !supportLists]2.1         [endif]系统架构

在本项目中,我们将搜索业务独立出来,创建搜索子系统。这样做,既能提高系统的拓展能力,也能灵活的对系统进行分布式部署。

[if !vml]

[endif]

[if !supportLists]2.2         [endif]实现思路

(1)搭建搜索服务器。

(2)创建搜索系统。

(3)发布搜索服务的公共接口。

(4)在门户系统调用该接口,实现搜索。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,440评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,814评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,427评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,710评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,625评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,014评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,511评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,162评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,311评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,262评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,278评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,989评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,583评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,664评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,904评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,274评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,856评论 2 339

推荐阅读更多精彩内容