Redis----緩存穿透問題

  • pom.xml
server.port=8088
server.servlet.context-path=/test
#配置jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

#配置mybatis
# 掃描xml文件
mybatis.mapper-locations=classpath:/mapper/*Mapper.xml

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://183.170.26.225:3306/society?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
#我的redis
spring.redis.host=192.168.136.223
spring.redis.port=6379
#哨兵模式  
#spring.redis.password=    如果設置密碼加上
#spring.redis.sentinel.master=mymaster
#spring.redis.cluster.nodes=192.168.136.**:6379,192.168.136.**:6379,192.168.136.**:6379
  • serviceImpl
 /**
     * 查詢所有插件
     * @return
     */
    @Override
    public List<Plugin> getAll() {
        List<Plugin> plugins=new ArrayList<>();
        try{
            plugins=(List<Plugin>)redisTemplate.opsForValue().get("allPlugin");
            if (plugins == null){
                //加鎖預防緩存穿透
                synchronized (this){
                    plugins=(List<Plugin>)redisTemplate.opsForValue().get("allPlugin");
                    if (plugins==null){
                        System.out.println("緩存中沒有");
                        plugins= pluginMapper.selectAll();
                        redisTemplate.opsForValue().set("allPlugin",plugins);
                    }else {
                        System.out.println("緩存中有數據");
                    }
                }

            }else {
                System.out.println("緩存中有數據");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return plugins;
    }
  • Controller
package com.tmxk.sring_boot_all.controller.redis;

import com.tmxk.sring_boot_all.models.Plugin;
import com.tmxk.sring_boot_all.service.impl.PluginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author zhaoxin
 * @Email [email protected]
 * @Description //TODO
 * @Date 2019/4/10
 **/
@Controller
public class RedisController {
    @Autowired
    private PluginServiceImpl pluginService;

    @RequestMapping("/getAllByRedis")
    @ResponseBody
    public List<Plugin> getAll(){
        //線程   該線程調用底層查詢方法
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                pluginService.getAll();
            }
        };
        //模擬多線程
        ExecutorService executorService = Executors.newFixedThreadPool(25);
        for (int i=0;i<10000;i++){
            executorService.submit(runnable);
        }
        return  pluginService.getAll();
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章