文章目錄
- 使用redis的流程圖:
- 實作快取商品分類的思維圖:
- 案例實作
- 1. 撰寫測驗類
- 2. 撰寫商品分類的bean(Category)
- 3. 創建properties檔案(jedis.properties)存放鍵值對
- 4. 創建Jedis工具類,連接redis,讀取propties檔案鍵值對配置
- 5. 撰寫service層(CategoryService)
- 6. 撰寫dao層
- 7. 測驗TestCategoryService看邏輯是否正確
- 8. 撰寫頁面Servlet顯示(就是測驗類copy修改就寫)
- 9. 過濾器,防止亂碼
- 10. 寫jsp頁面
- 11. 需要的jar包
- 12. 運行結果
使用redis的流程圖:

實作快取商品分類的思維圖:

- 瀏覽器發送請求,服務器接收請求交給CategroySerivce類的queryAll()方法處理去查詢資料
- 首次查詢redis,如果沒有資料(json格式),就呼叫CategoryDao去資料庫(MySQL)查找,獲取集合List商品分類集合,然后先回傳給CategorySrvice,讓其將List資料轉成json后快取進redis,同時將json資料回傳給服務器,
- 第二次起查詢,直接從redis中獲取json資料格式的商品分類,然后將json轉成List,顯示到瀏覽器上,
案例實作
1. 撰寫測驗類
//第一步:測驗開發CategoryService
public class TestCategoryService {
@Test
public void test01() throws Exception {
//1. 創建業務類物件
CategoryService categoryService = new CategoryService();
//2. 查詢所有分類集合
List<Category> list = categoryService.queryAll();
//3. 回圈遍歷輸出
for(Category category:list){
System.out.println(category);
}
}
}
2. 撰寫商品分類的bean(Category)
public class Category {
private int cid; //編號
private String cname; //名稱
//省略setter和getter
}
3. 創建properties檔案(jedis.properties)存放鍵值對
maxTotal=50
maxIdle=15
url=localhost
port=6379
4. 創建Jedis工具類,連接redis,讀取propties檔案鍵值對配置
//使用ResourceBundle讀.propties檔案
public class JedisUtils {
//單例模式 靜態代碼塊只執行一次,因為創建會很消耗性能
private static JedisPool pool = null;
//1. 創建一個連接池 靜態代碼在專案中,如果被使用只會加載一次
static{
//1.1 通過ResourceBundle讀取properties檔案 jedis.properties
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
//獲取引數
String maxTotal = bundle.getString("maxTotal");
String maxIdle = bundle.getString("maxIdle");
String url = bundle.getString("url");
String port = bundle.getString("port");
//1.2創建連接池
//創建連接池的配置物件
JedisPoolConfig config = new JedisPoolConfig();
//設定最大連接數
config.setMaxTotal(Integer.parseInt(maxTotal));
//設定空閑連接數
config.setMaxIdle(Integer.parseInt(maxIdle));
//2:創建連接池
pool = new JedisPool(config, url, Integer.parseInt(port));
}
//2, 對外提供一個獲取連接的方法
public static Jedis getJedis(){
return pool.getResource();
}
//3, 提供釋放資源的方法
public static void close(Jedis jedis){
if(jedis != null) {
jedis.close();
}
}
}
5. 撰寫service層(CategoryService)
public class CategoryService {
//呼叫dao層查詢資料
CategoryDao dao = new CategoryDao();
//jackson 的 ObjectMapper類3 用來將資料與json格式相互轉換
ObjectMapper objectMapper = new ObjectMapper();
public List<Category> queryAll() throws IOException {
//1. 先訪問redis,有資料就回傳 JedisUtils 資料格式:Map<Srting,String>
// 獲取redis連接
Jedis redis = JedisUtils.getJedis();
// 查找redis快取中的資料
String json = redis.get("categoryList");
if(json == null){//2. 如果沒有資料就查詢資料庫(MySQL),快取到redis
System.out.println("(第一次查詢) redis中沒有資料,去查找資料庫的資料");
List<Category> categoryList = dao.findAll();
//快取 將查找到的list集合轉換成json資料格式,快取到redis上
//通過ObjectMapper物件,轉list資料格式轉化成json資料格式
String categoryListJson = objectMapper.writeValueAsString(categoryList);
System.out.println("查找到的categoryList的json資料是:");
System.out.println(categoryListJson);
redis.set("categoryList",categoryListJson);
return categoryList;
}else{//json ! =null 在redis中查詢到了資料
System.out.println("非第一次查詢,資料快取到redis中,redis有資料,直接從記憶體中回傳,速度快");
//將json轉成物件 參1 json資料 參2 TypeReference
List<Category> categoryList = objectMapper.readValue(json,new TypeReference<List<Category>>(){});
return categoryList;
}
}
}
6. 撰寫dao層
public class CategoryDao {
public List<Category> findAll() {
//1:創建集合
List<Category> categoryList = new ArrayList<>();
//使用回圈模擬資料,以后使用mybatis來查資料
for (int i = 0; i < 10; i++) {
categoryList.add(new Category(i,"選單名" + i));
}
return categoryList;
}
}
7. 測驗TestCategoryService看邏輯是否正確
(這里連接的redis是本地的localhost,需要開啟reids服務器redis-server.exe,否則會報連接錯誤)
第一次查詢測驗
(第一次查詢) redis中沒有資料,去查找資料庫的資料
查找到的categoryList的json資料是:
[{“cid”:0,“cname”:“選單名0”},{“cid”:1,“cname”:“選單名1”},{“cid”:2,“cname”:“選單名2”},{“cid”:3,“cname”:“選單名3”},{“cid”:4,“cname”:“選單名4”},{“cid”:5,“cname”:“選單名5”},{“cid”:6,“cname”:“選單名6”},{“cid”:7,“cname”:“選單名7”},{“cid”:8,“cname”:“選單名8”},{“cid”:9,“cname”:“選單名9”}]
Category{cid=0, cname=‘選單名0’}
Category{cid=1, cname=‘選單名1’}
Category{cid=2, cname=‘選單名2’}
Category{cid=3, cname=‘選單名3’}
Category{cid=4, cname=‘選單名4’}
Category{cid=5, cname=‘選單名5’}
Category{cid=6, cname=‘選單名6’}
Category{cid=7, cname=‘選單名7’}
Category{cid=8, cname=‘選單名8’}
Category{cid=9, cname=‘選單名9’}
第二次/三次查詢資料(非第一次的任意次數查找)
非第一次查詢,資料快取到redis中,redis有資料,直接從記憶體中回傳,速度快
Category{cid=0, cname=‘選單名0’}
Category{cid=1, cname=‘選單名1’}
Category{cid=2, cname=‘選單名2’}
Category{cid=3, cname=‘選單名3’}
Category{cid=4, cname=‘選單名4’}
Category{cid=5, cname=‘選單名5’}
Category{cid=6, cname=‘選單名6’}
Category{cid=7, cname=‘選單名7’}
Category{cid=8, cname=‘選單名8’}
Category{cid=9, cname=‘選單名9’}
從結果可以知道業務邏輯沒有錯誤,這時候就可以寫頁面了
8. 撰寫頁面Servlet顯示(就是測驗類copy修改就寫)
@WebServlet("/CategoryListServlet")
public class CategoryListServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 創建業務類物件
CategoryService categoryService = new CategoryService();
//2. 查詢分類集合
List<Category> categoryList = categoryService.queryAll();
//3. 請求轉發
request.setAttribute("categoryList",categoryList);
request.getRequestDispatcher("categoryList.jsp").forward(request,response);
}
}
9. 過濾器,防止亂碼
//攔截所有,進行編碼設定
@WebFilter("/*")
public class EncodingFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
chain.doFilter(req, resp);//放行
}
}
10. 寫jsp頁面
<%-- isELIgnored="false 開啟el運算式--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>categoryList商品型別串列</title>
</head>
<body>
<c:if test="${empty categoryList}">
商品型別是空的喲
</c:if>
<c:if test="${!empty categoryList}">
<table border="1" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>編號</th>
<th>商品型別名稱</th>
</tr>
</thead>
<tbody>
<c:forEach items="${categoryList}" var="category" varStatus="vs">
<tr>
<td>${vs.count}</td>
<td>${category.cname}</td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</body>
</html>
11. 需要的jar包

12. 運行結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/120464.html
標籤:其他
上一篇:請問在利用python對word進行編輯時,以下這兩句話有什么區別?
下一篇:python語法基礎進階練習
