1. 程式人生 > >springboot專案練習五 整合redis-頁面靜態化

springboot專案練習五 整合redis-頁面靜態化

1 靜態化:根據請求連結生成html檔案完成訪問頁面的靜態化

2 靜態化借用nosql資料庫redis完成檔案儲存判斷

  1. 使用者發起請求,接收使用者請求,判斷是否已經生成html靜態檔案,有則直接將生成的HTML檔案地址return返回。
  2. 未生成html檔案,通過Http發起網路請求,生成html檔案,將檔名稱以key-value的方式儲存進redis快取中。
  •  第一步專案中新增redis的依賴
  • 第二步專案中配置redis的埠號訪問密碼等配置檔案
  • 編寫測試類
<!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
spring.redis.database=0 
spring.redis.host=192.168.34.3 //redis服務地址
spring.redis.port=6379 // 埠號
spring.redis.password=123456 //密碼 沒有配置的可以省略
spring.redis.timeout=3000 // 連線超時
spring.redis.pool.max-idle=200
spring.redis.pool.min-idle=200
spring.redis.pool.max-active=2000
spring.redis.pool.max-wait=1000

修改newController.java類增加如下方法 

    @Autowired
	private StringRedisTemplate  stringRedisTemplate; 

    @RequestMapping("redis")//測試
	@ResponseBody
	public String testRedis(){
		stringRedisTemplate.opsForValue().set("test", "zhangsan");//儲存key為test value為張三的值
		String string = stringRedisTemplate.opsForValue().get("test"); // 獲取該值
		return string;
		
	}

springboot 官網整合redis的連結地址,很多配置資訊可以從官網上找到 

https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-connecting-to-redis

 

3 檔案靜態化

  • 編寫前臺jq指令碼發起請求
  • 接收使用者請求生成html檔案
  • 以新聞的id為key將資料儲存在快取資料庫redis中。
function opterate(rowData){
	var str ="";
	str +="<a href='javascript:void(0)' onclick='del("+rowData.id+")'>刪除</a>";
	str += "&nbsp;&nbsp;";
	str += "<a href='javascript:void(0)' onclick=showNews('"+rowData.id+"','"+rowData.url_3w+"')>檢視</a>";
	return  str;
}
function showNews(id,url){
	var newUrl ="/news/getNews/"+id; 
	$.post(newUrl,{url:url},function(data){
		if(data.code==200){ // 接收服務端返回的html檔名稱
			window.location.href=data.data+".html";// 請求對應的地址
		}
	});
}

配置HTML生成目錄在application.properties中新增檔案路徑

html.rootDir = F:/springboot/springboot_solr/src/main/webapp/

在newController.java中獲取html生成的根路徑新增生成html檔案的方法和請求

    @Value("${html.rootDir}")
	private String htmlDir; //從配置檔案中獲取配置的html檔案根路徑

    @RequestMapping("getNews/{id}")
	@ResponseBody
	public ResultData getNews(@PathVariable("id") String  id,@RequestParam("url") String url){
		/**
		 * 1 判斷是否已經存在html檔案
		 */
		String htmlName = stringRedisTemplate.opsForValue().get("news"+id);
		if(!StringUtils.isEmpty(htmlName)){ //已經存在直接return
			return new ResultData("200",htmlName,""); //將檔名稱作為data資料返回 前臺js接收到路徑地址後,通過window.location.href="xxxx"+".html"完成檔案的訪問
		}else{ // 不存在生成在專案目錄下生成html檔案
			gcreateHtmlFile(id,url);
			stringRedisTemplate.opsForValue().set("news"+id, id);
		}
		
		return new ResultData("200",id,"");
		
	}

    /*
    生成html檔案
    通過openConnection 方法發起請求獲取inputStream流 將流指向輸出檔案
    這裡使用org.apache.commons.io.FileUtils 工具類完成輸入流向輸出流檔案的拷貝生成html檔案

    */
	public void gcreateHtmlFile(String id, String url) {
		// 通過配置檔案獲取存放靜態檔案的資料夾
		try {
			String path = htmlDir;
			File rootDir = new File(path);
			if (!rootDir.isDirectory()) {
				rootDir.mkdirs();
			}
			URL newUrl = new URL(url);
			URLConnection openConnection = newUrl.openConnection();
			InputStream in = openConnection.getInputStream();
			FileUtils.copyInputStreamToFile(in, new File(path + "/" + id + ".html"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

在專案目錄下生成的html檔案如下

 

簡單版的靜態化頁面就完成了。其他的部分慢慢增加

增加採集功能,採取網易新聞,增加資料量