本系列教程內容提要
Java工程師之Redis實戰系列教程教程是一個學習教程,是關於Java工程師的Redis知識的實戰系列教程,本系列教程均以解決特定問題為目標,使用Redis快速解決在實際生產中的相關問題,為了更方便的與大家一起探討與學習,每個章節均提供盡可能詳細的示例源碼及注釋,所有示例源碼均可在javacourse-redis-in-action找到相關幫助!
什么是大型網站?
從技術上的角度來看,大型網站的實現是能夠應對各種突發事件,能夠處理海量數據等因素.... 這里我們抓住“處理海量數據”這一點來進行探討學習。
何提高網站處理海量數據?
- 減少用戶請求體大小
- 響應數據進行緩存
我們應該怎么做?
第一步:拒絕cookie,使用token令牌登錄 github示例源碼下載
Cookie是常見的記錄用戶會話的解決方案,但在某些場景下其並不適用,如前后端分離,如Cookie體積大時影響請求速率。
- 用戶發起操作請求
- Redis校驗是否擁有TOKEN令牌
- 沒有令牌跳轉登錄
- 登錄成功生成TOKEN保存至Redis
- ......
Redis數據結構
核心源碼
@RequestMapping("/register")
public String register(User user, Model model) {
userFactory.put(user.getUsername(), user);
model.addAttribute("result", "注冊成功!");
return "RegAndLog";
}
@RequestMapping("/login")
public String login(User user, Model model, @RequestParam(required = false) String token, HttpServletRequest request) {
Boolean exists = jedis.exists("login:" + token);
String clientIp = getClientIp(request);
String url = getURL(request);
if (!exists) {
User result = userFactory.get(user.getUsername());
if (result != null && user.getUsername().equals(result.getUsername()) && user.getPassword().equals(result.getPassword())) {
/*將用戶登錄緩存到Redis*/
String tokenUUID = UUID.randomUUID().toString();
updateToken(jedis, tokenUUID, result, clientIp, url);
/*獲取用戶的登錄記錄*/
Set<String> IPList = jedis.zrange("recent:" + user.getUsername(), 0, -1);
/*獲取用戶最新訪問的頁面*/
Set<String> URLList = jedis.zrange("viewed:" + user.getUsername(), 0, -1);
model.addAttribute("record", IPList);
model.addAttribute("URLList", URLList);
model.addAttribute("result", "登錄成功!TOKEN為" + tokenUUID + ",30秒后過期.....30秒內可使用Token登錄");
return "RegAndLog";
}
model.addAttribute("result", "登錄失敗!");
return "RegAndLog";
}
model.addAttribute("result", "使用Token登錄成功!");
return "RegAndLog";
}
各位友友運行本小結源碼:訪問 http://localhost:8080/cookie/LogOrReg
第二步:拒絕cookie,使用Redis構造購物車 github示例源碼下載
對於一個購物網站來說,購物車就是一個必不可少的功能,從長遠來看對用戶購物車的數據的統計與分析有利於進行大數據的分析,提高網站的營業額。但在服務端每次解析,校驗,設置Cookie,會增加程序的響應時間。同樣;隨着Cookie體積的增大,也會增加用戶請求時間,所以我們在Redis上進行包存購物車。
Redis數據接結構
核心源碼
@GetMapping("/addCart")
public String addCart(Item item, Model model) {
User user = getUser();
addTOCart(jedis, user, item);
Map<String, String> cart = getCart(jedis, user);
model.addAttribute("result", "添加購物車成功!");
model.addAttribute("cartList", cart);
return "ProductList";
}
各位友友運行本小結源碼:訪問 http://localhost:8080/cart/productlist.html
第三步:緩存網頁數據,提高網頁響應 github示例源碼下載
對於我們網站的大多數網頁,一般都很少改動,例如商品頁面,商品的標題和商品的介紹基本上不會改動,但是商品的剩余數量你又不得不去數據庫實時查詢,這將會導致“用戶每打開或刷新一次網頁,你不得不去數據庫查詢一次數據”,對於一般的關系數據庫數據庫每秒處理200~2000上限,就成為了你網站的瓶頸所在。
Reids數據結構圖
核心源碼
@RequestMapping("/testCacheForItem/{itemname}")
public String testCacheForItem(Model model, @PathVariable(required = true, name = "itemname") String itemname) {
/*模擬數據*/
Item item = new Item(itemname, itemname + "這是商品的介紹" + itemname, new Random().nextInt(10));
/*判斷是否被緩存*/
Boolean hexists = jedis.exists("cache:" + itemname);
if (!hexists) {
Gson gson = new Gson();
String s = gson.toJson(item);
jedis.set("cache:" + itemname, s);
model.addAttribute("s", s);
model.addAttribute("result", "第一次訪問,已經加入Redis緩存");
return "CacheItem";
}
String s = jedis.get("cache:" + itemname);
model.addAttribute("s", s);
model.addAttribute("result", "重復訪問,從Redis中讀取數據");
return "CacheItem";
}
各位友友運行本小結源碼:訪問 http://localhost:8080/cache/testCacheForItem/吃雞神槍
本章小結
對於真正實現一個能處理海量數據的購物網站來說,我們做的實在是太簡單了...是使用各種語言和工具的相互配置,程序邏輯的優化,才能構建出一個真正的能處理海量數據的網站。當然我們做的也不差....hhhh