国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【業(yè)務(wù)功能100】補充代碼【業(yè)務(wù)功能88】微服務(wù)-springcloud-分布式鎖-redis-redisson-springcache

這篇具有很好參考價值的文章主要介紹了【業(yè)務(wù)功能100】補充代碼【業(yè)務(wù)功能88】微服務(wù)-springcloud-分布式鎖-redis-redisson-springcache。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

采用redisson做分布式鎖,完成數(shù)據(jù)的查詢接口功能getCatelog2JSONRedis

原先從mysql數(shù)據(jù)庫查詢的效率較低,現(xiàn)在將部分固定數(shù)據(jù)展示比如頁面的樹形欄目信息等,存儲到redis緩存,然后基于分布式集群,需要結(jié)合本地鎖(synchronized )與分布式鎖(redissonClient.getLock(“catelog2JSON-lock”)),避免多次重復(fù)訪問數(shù)據(jù)庫

 /**
     * 查詢出所有的二級和三級分類的數(shù)據(jù)
     * 并封裝為Map<String, Catalog2VO>對象
     * @return
     */
    //@Override
    public Map<String, List<Catalog2VO>> getCatelog2JSONRedis() {
        String key = "catalogJSON";
        // 從Redis中獲取分類的信息
        String catalogJSON = stringRedisTemplate.opsForValue().get(key);
        if(StringUtils.isEmpty(catalogJSON)){
            System.out.println("緩存沒有命中.....");
            // 緩存中沒有數(shù)據(jù),需要從數(shù)據(jù)庫中查詢
            Map<String, List<Catalog2VO>> catelog2JSONForDb = getCatelog2JSONDbWithRedisson();
            return catelog2JSONForDb;
        }
        System.out.println("緩存命中了....");
        // 表示緩存命中了數(shù)據(jù),那么從緩存中獲取信息,然后返回
        Map<String, List<Catalog2VO>> stringListMap = JSON.parseObject(catalogJSON, new TypeReference<Map<String, List<Catalog2VO>>>() {
        });
        return stringListMap;
    }

    public Map<String, List<Catalog2VO>> getCatelog2JSONDbWithRedisson() {
        String keys = "catalogJSON";
        // 獲取分布式鎖對象  加鎖的時候,這個鎖的名稱一定要注意
        // 商品信息 product-lock  product-1001-lock product-1002-lock
        RLock lock = redissonClient.getLock("catelog2JSON-lock");
        Map<String, List<Catalog2VO>> data = null;
        try {
            lock.lock();
            // 加鎖成功
            data = getDataForDB(keys);
        }finally {
            lock.unlock();
        }
        return data;
    }

/**
     * 從數(shù)據(jù)庫中查詢操作
     * @param keys
     * @return
     */
    private Map<String, List<Catalog2VO>> getDataForDB(String keys) {
        // 從Redis中獲取分類的信息
        String catalogJSON = stringRedisTemplate.opsForValue().get(keys);
        if(!StringUtils.isEmpty(catalogJSON)){
            // 說明緩存命中

            // 表示緩存命中了數(shù)據(jù),那么從緩存中獲取信息,然后返回
            Map<String, List<Catalog2VO>> stringListMap = JSON.parseObject(catalogJSON, new TypeReference<Map<String, List<Catalog2VO>>>() {
            });
            return stringListMap;
        }
        System.out.println("-----------》查詢數(shù)據(jù)庫操作");

        // 獲取所有的分類數(shù)據(jù)
        List<CategoryEntity> list = baseMapper.selectList(new QueryWrapper<CategoryEntity>());
        // 獲取所有的一級分類的數(shù)據(jù)
        List<CategoryEntity> leve1Category = this.queryByParenCid(list,0l);
        // 把一級分類的數(shù)據(jù)轉(zhuǎn)換為Map容器 key就是一級分類的編號, value就是一級分類對應(yīng)的二級分類的數(shù)據(jù)
        Map<String, List<Catalog2VO>> map = leve1Category.stream().collect(Collectors.toMap(
                key -> key.getCatId().toString()
                , value -> {
                    // 根據(jù)一級分類的編號,查詢出對應(yīng)的二級分類的數(shù)據(jù)
                    List<CategoryEntity> l2Catalogs = this.queryByParenCid(list,value.getCatId());
                    List<Catalog2VO> Catalog2VOs =null;
                    if(l2Catalogs != null){
                        Catalog2VOs = l2Catalogs.stream().map(l2 -> {
                            // 需要把查詢出來的二級分類的數(shù)據(jù)填充到對應(yīng)的Catelog2VO中
                            Catalog2VO catalog2VO = new Catalog2VO(l2.getParentCid().toString(), null, l2.getCatId().toString(), l2.getName());
                            // 根據(jù)二級分類的數(shù)據(jù)找到對應(yīng)的三級分類的信息
                            List<CategoryEntity> l3Catelogs = this.queryByParenCid(list,l2.getCatId());
                            if(l3Catelogs != null){
                                // 獲取到的二級分類對應(yīng)的三級分類的數(shù)據(jù)
                                List<Catalog2VO.Catalog3VO> catalog3VOS = l3Catelogs.stream().map(l3 -> {
                                    Catalog2VO.Catalog3VO catalog3VO = new Catalog2VO.Catalog3VO(l3.getParentCid().toString(), l3.getCatId().toString(), l3.getName());
                                    return catalog3VO;
                                }).collect(Collectors.toList());
                                // 三級分類關(guān)聯(lián)二級分類
                                catalog2VO.setCatalog3List(catalog3VOS);
                            }
                            return catalog2VO;
                        }).collect(Collectors.toList());
                    }

                    return Catalog2VOs;
                }
        ));
        // 從數(shù)據(jù)庫中獲取到了對應(yīng)的信息 然后在緩存中也存儲一份信息
        //cache.put("getCatelog2JSON",map);
        // 表示緩存命中了數(shù)據(jù),那么從緩存中獲取信息,然后返回
        if(map == null){
            // 那就說明數(shù)據(jù)庫中也不存在  防止緩存穿透
            stringRedisTemplate.opsForValue().set(keys,"1",5, TimeUnit.SECONDS);
        }else{
            // 從數(shù)據(jù)庫中查詢到的數(shù)據(jù),我們需要給緩存中也存儲一份
            // 防止緩存雪崩
            String json = JSON.toJSONString(map);
            stringRedisTemplate.opsForValue().set("catalogJSON",json,100,TimeUnit.MINUTES);
        }
        return map;
    }

    /**
     * 從數(shù)據(jù)庫查詢的結(jié)果
     * 查詢出所有的二級和三級分類的數(shù)據(jù)
     * 并封裝為Map<String, Catalog2VO>對象
     * 在SpringBoot中,默認(rèn)的情況下是單例
     * @return
     */
    public Map<String, List<Catalog2VO>> getCatelog2JSONForDb() {
        String keys = "catalogJSON";
        //synchronized 加本地鎖,即使分布式集群,本地服務(wù)也需要加上本地鎖synchronized 或者其他鎖,避免大量并發(fā)訪問直接走到分布式鎖造成壓力
        synchronized (this){
            // 從Redis中獲取分類的信息
            String catalogJSON = stringRedisTemplate.opsForValue().get(keys);
            if(!StringUtils.isEmpty(catalogJSON)){
                // 說明緩存命中
                // 表示緩存命中了數(shù)據(jù),那么從緩存中獲取信息,然后返回
                Map<String, List<Catalog2VO>> stringListMap = JSON.parseObject(catalogJSON, new TypeReference<Map<String, List<Catalog2VO>>>() {
                });
                return stringListMap;
            }
            System.out.println("-----------》查詢數(shù)據(jù)庫操作");

            // 獲取所有的分類數(shù)據(jù)
            List<CategoryEntity> list = baseMapper.selectList(new QueryWrapper<CategoryEntity>());
            // 獲取所有的一級分類的數(shù)據(jù)
            List<CategoryEntity> leve1Category = this.queryByParenCid(list,0l);
            // 把一級分類的數(shù)據(jù)轉(zhuǎn)換為Map容器 key就是一級分類的編號, value就是一級分類對應(yīng)的二級分類的數(shù)據(jù)
            Map<String, List<Catalog2VO>> map = leve1Category.stream().collect(Collectors.toMap(
                    key -> key.getCatId().toString()
                    , value -> {
                        // 根據(jù)一級分類的編號,查詢出對應(yīng)的二級分類的數(shù)據(jù)
                        List<CategoryEntity> l2Catalogs = this.queryByParenCid(list,value.getCatId());
                        List<Catalog2VO> Catalog2VOs =null;
                        if(l2Catalogs != null){
                            Catalog2VOs = l2Catalogs.stream().map(l2 -> {
                                // 需要把查詢出來的二級分類的數(shù)據(jù)填充到對應(yīng)的Catelog2VO中
                                Catalog2VO catalog2VO = new Catalog2VO(l2.getParentCid().toString(), null, l2.getCatId().toString(), l2.getName());
                                // 根據(jù)二級分類的數(shù)據(jù)找到對應(yīng)的三級分類的信息
                                List<CategoryEntity> l3Catelogs = this.queryByParenCid(list,l2.getCatId());
                                if(l3Catelogs != null){
                                    // 獲取到的二級分類對應(yīng)的三級分類的數(shù)據(jù)
                                    List<Catalog2VO.Catalog3VO> catalog3VOS = l3Catelogs.stream().map(l3 -> {
                                        Catalog2VO.Catalog3VO catalog3VO = new Catalog2VO.Catalog3VO(l3.getParentCid().toString(), l3.getCatId().toString(), l3.getName());
                                        return catalog3VO;
                                    }).collect(Collectors.toList());
                                    // 三級分類關(guān)聯(lián)二級分類
                                    catalog2VO.setCatalog3List(catalog3VOS);
                                }
                                return catalog2VO;
                            }).collect(Collectors.toList());
                        }

                        return Catalog2VOs;
                    }
            ));
            // 從數(shù)據(jù)庫中獲取到了對應(yīng)的信息 然后在緩存中也存儲一份信息
            //cache.put("getCatelog2JSON",map);
            // 表示緩存命中了數(shù)據(jù),那么從緩存中獲取信息,然后返回
            if(map == null){
                // 那就說明數(shù)據(jù)庫中也不存在  防止緩存穿透
                stringRedisTemplate.opsForValue().set(keys,"1",5, TimeUnit.SECONDS);
            }else{
                // 從數(shù)據(jù)庫中查詢到的數(shù)據(jù),我們需要給緩存中也存儲一份
                // 防止緩存雪崩
                String json = JSON.toJSONString(map);
                stringRedisTemplate.opsForValue().set("catalogJSON",json,100,TimeUnit.MINUTES);
            }
            return map;
        } }

采用springcache 注解方式進(jìn)行緩存處理

與業(yè)務(wù)邏輯解耦,使用更方便

springcache底層沒有采用分布式鎖去避免了大量訪問進(jìn)數(shù)據(jù)庫,造成緩存擊穿,但是使用了 synchronized
本地同步鎖,這樣如果是分布式集群有三臺,那么每個節(jié)點都是只會有一個請求去訪問,即使都去訪問了數(shù)據(jù)庫,這樣壓力也不會很大文章來源地址http://www.zghlxwxcb.cn/news/detail-700747.html

 @Cacheable(value = "catagory",key = "#root.methodName")
    @Override
    public Map<String, List<Catalog2VO>> getCatelog2JSON() {
        // 獲取所有的分類數(shù)據(jù)
        List<CategoryEntity> list = baseMapper.selectList(new QueryWrapper<CategoryEntity>());
        // 獲取所有的一級分類的數(shù)據(jù)
        List<CategoryEntity> leve1Category = this.queryByParenCid(list,0l);
        // 把一級分類的數(shù)據(jù)轉(zhuǎn)換為Map容器 key就是一級分類的編號, value就是一級分類對應(yīng)的二級分類的數(shù)據(jù)
        Map<String, List<Catalog2VO>> map = leve1Category.stream().collect(Collectors.toMap(
                key -> key.getCatId().toString()
                , value -> {
                    // 根據(jù)一級分類的編號,查詢出對應(yīng)的二級分類的數(shù)據(jù)
                    List<CategoryEntity> l2Catalogs = this.queryByParenCid(list,value.getCatId());
                    List<Catalog2VO> Catalog2VOs =null;
                    if(l2Catalogs != null){
                        Catalog2VOs = l2Catalogs.stream().map(l2 -> {
                            // 需要把查詢出來的二級分類的數(shù)據(jù)填充到對應(yīng)的Catelog2VO中
                            Catalog2VO catalog2VO = new Catalog2VO(l2.getParentCid().toString(), null, l2.getCatId().toString(), l2.getName());
                            // 根據(jù)二級分類的數(shù)據(jù)找到對應(yīng)的三級分類的信息
                            List<CategoryEntity> l3Catelogs = this.queryByParenCid(list,l2.getCatId());
                            if(l3Catelogs != null){
                                // 獲取到的二級分類對應(yīng)的三級分類的數(shù)據(jù)
                                List<Catalog2VO.Catalog3VO> catalog3VOS = l3Catelogs.stream().map(l3 -> {
                                    Catalog2VO.Catalog3VO catalog3VO = new Catalog2VO.Catalog3VO(l3.getParentCid().toString(), l3.getCatId().toString(), l3.getName());
                                    return catalog3VO;
                                }).collect(Collectors.toList());
                                // 三級分類關(guān)聯(lián)二級分類
                                catalog2VO.setCatalog3List(catalog3VOS);
                            }
                            return catalog2VO;
                        }).collect(Collectors.toList());
                    }

                    return Catalog2VOs;
                }
        ));
        return map;
    }

到了這里,關(guān)于【業(yè)務(wù)功能100】補充代碼【業(yè)務(wù)功能88】微服務(wù)-springcloud-分布式鎖-redis-redisson-springcache的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包