標籤:提高 前端 作用 driver mysq 資訊 web應用 ctf long
作為一個web開發人員,根據產品經理的需求來完成一個應用顯然不是我們的唯一目標,很多時候,我們考慮的不僅僅是需求,而是超越需求,認識到項目上線後的效能瓶頸。很多web應用在上線後,都可能會碰到響應慢的問題,而從今天開始,我將以淺顯的例子帶領大家一步一步的最佳化web應用。
先舉個栗子:
小白在做一個web列表頁時,需求很簡單,當使用者訪問第一個頁的時候,伺服器返回1-10條的文章簡介資料,訪問第2頁的時候,伺服器返回11-20條的文章簡介資料。小白轉念一想,這簡單啊,每次前端給我一個頁數就行,我直接從資料庫中查出來,然後返回回去就行。哈哈,so easy!
當小白把程式寫好上線後,隨著使用者以及文章的增多,資料庫的壓力也越大,比如當文章數量達到一百萬條時,每次分頁查詢都需要耗時資料庫500ms左右,這時候小白就意識到程式需要最佳化了。
這時候小白的老師來了,老師說,這些文章列表資料,在短時間中不會改變太大,為什麼不把它緩衝起來呢?這樣每次都可以從記憶體中取出來,而不用每次都訪問資料庫了。
即項目架構由最簡單的
加入了cache層:
這樣,可以極大的加快訪問速度。
就拿這個例子來說,你可以把每次擷取的列表信心都緩衝起來,為了在緩衝的同時也能保持更新,你可以設定列表緩衝為1分鐘到期,這樣這1分鐘內多次訪問速度會極大的提高。
至於緩衝,你可以使用redis,Redis 是一個高效能的key-value資料庫。可以對關聯式資料庫起到很好的補充作用。
小白在聽了老師給出的建議後,馬上著手開始搭建redis開發環境,並開始開發,他寫了一個demo,這個demo中,使用緩衝前每次訪問的速度為400-500ms,使用緩衝後的訪問速度穩定在25-27ms,相當於17倍的速度提升!
下面為主要的測試代碼:
package com.happyheng.controller;import com.alibaba.fastjson.JSON;import com.happyheng.dao.ArticleDao;import com.happyheng.model.Article;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import redis.clients.jedis.Jedis;import java.util.List;/** * * * Created by happyheng on 17/5/6. */@RestController@RequestMapping("/test")public class TestController { private static final String KEY_CACHE_ARTICLE_LIST = "article_list"; @Autowired private ArticleDao articleDao; @RequestMapping("/testSelectFromDb") public List<Article> testSelectFromDb() throws Exception { return articleDao.getArticleListFromdb(); } @RequestMapping("/testSelectFromCache") public String testSelectFromCache() throws Exception { Jedis jedis = new Jedis("localhost"); // 先從緩衝中取出資料 String articleListStr = jedis.get(KEY_CACHE_ARTICLE_LIST); if (!StringUtils.isEmpty(articleListStr)) { return articleListStr; } else { // 如果資料為空白,那麼從db中取出來,然後序列化後寫入到cache中,並設定1分鐘的到期時間 List<Article> articleList = articleDao.getArticleListFromdb(); String serializeArticleListStr = JSON.toJSONString(articleList); jedis.set(KEY_CACHE_ARTICLE_LIST, serializeArticleListStr); jedis.expire(KEY_CACHE_ARTICLE_LIST, 60); return serializeArticleListStr; } }}
package com.happyheng.dao;import com.happyheng.model.Article;import org.springframework.stereotype.Service;import java.sql.*;import java.util.ArrayList;import java.util.List;/** * * * Created by happyheng on 17/5/7. */@Servicepublic class ArticleDao { public List<Article> getArticleListFromdb() throws Exception{ String sql = "SELECT * FROM article ORDER BY create_time DESC LIMIT 0, 10"; Class.forName("com.mysql.jdbc.Driver"); Connection mConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/optimize_db", "root", "mytestcon"); Statement statement = mConnection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); try { List<Article> list = new ArrayList<>(); //注意指標剛開始是-1位置,這行next()方法,會先判斷下一個位置有沒有,如果有,指向下一個位置。 while (resultSet.next()) { Article article = new Article(); article.setId(resultSet.getLong("id")); article.setTitle(resultSet.getString("title")); article.setContent(resultSet.getString("content")); article.setCreate_time(resultSet.getString("create_time")); list.add(article); } return list; } catch (Exception e) { e.printStackTrace(); } finally { try { resultSet.close(); statement.close(); mConnection.close(); } catch (SQLException e) { e.printStackTrace(); } } return null; }}
可以看到,當訪問 testSelectFromDb 時,直接從資料庫中擷取資料,訪問 testSelectFromCache 時,先從cache中擷取資料,如果沒有,從資料庫中擷取資料,然後寫入到cache中,下面為兩種存取方法的訪問時間對比:
直接存取db:
訪問cache:
小白的web最佳化之路 一、使用redis來緩衝資訊