》》1:查詢redis,沒有json資料,就呼叫CategoryDao,去獲取集合List<Category>
先返回給CategorySrvice,再轉成json存進redis
》》2:第二次起,直接獲取redis中的json,將json轉成List<Category>
test\java\com\wzx\pack01\TestCategoryService.java
//第一步:測試開發CategoryService
public class TestCategoryService {
@Test
public void test01() throws Exception {
//0: 建立業務類物件
CategoryService categoryService = new CategoryService();
//1: 查詢分類集合
List<Category> list = categoryService.queryAll();
for(Category category:list){
System.out.println(category);
}
}
}
src\com\wzx\bean\Category.java
//第二步驟 :
public class Category {
private int cid;
private String cname;
src\com\wzx\dao\CategoryDao.java
public class CategoryDao {
public List<Category> findAll() {
//1:建立集合
List<Category> list = new ArrayList<>();
//使用迴圈模擬資料,以後使用mybatis來查資料
for (int i = 0; i < 10; i++) {
list.add(new Category(i,"分類名"+i));
}
return list;
}
}
src\com\wzx\service\CategoryService.java
//第三步驟:實現queryAll方法
//3.1 複製jedis jar過來
//3.2 繫結
//3.3 複製properties檔案,與工具類
public class CategoryService {
CategoryDao dao = new CategoryDao();
ObjectMapper objectMapper = new ObjectMapper();
public List<Category> queryAll() throws IOException {
//queryAll的邏輯
//1:先存取redis,有資料就返回 JedisUtils Map<Srting,String>
//3.4獲取連線
Jedis redis = JedisUtils.getRedis();
//快取
String json = redis.get("list");
if(json == null){
System.out.println("第一次查詢 redis沒有資料,查的資料庫的資料,慢");
//2: 沒有資料就查詢資料,進行快取,方便下次使用 CategoryDao
List<Category> list = dao.findAll();
//快取 將集合轉換成json,快取到redis
//3.5 新增json jar到lib目錄並且繫結
//3.6 建立ObjectMapper物件,轉list成json
String jsonValue = objectMapper.writeValueAsString(list);
System.out.println(jsonValue);
redis.set("list",jsonValue);
return list;
}else{//json ! =null
System.out.println("第二次 redis有資料,直接返回,快");
List<Category> list = objectMapper.readValue(json,new TypeReference<List<Category>>(){});//將json轉成物件 參1 json資料 參2
return list;
}
}
}
test\java\com\wzx\pack01\TestObjectMapper.java
public class TestObjectMapper {
@Test
public void test01() throws IOException {
String 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\"}]";
//
ObjectMapper objectMapper = new ObjectMapper();
List<Category> list = objectMapper.readValue(json,new TypeReference<List<Category>>(){});//將json轉成物件 參1 json資料 參2 new TypeReference<>(){}
System.out.println(list);
}
}
src\com\wzx\filter\CharSetFilter.java
@WebFilter("/*")
public class CharSetFilter implements Filter {
public void destroy() {
}
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);
}
public void init(FilterConfig config) throws ServletException {
}
}
src\com\wzx\servlet\CategoryListServlet.java
@WebServlet("/list")
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 {
//0: 建立業務類物件
CategoryService categoryService = new CategoryService();
//1: 查詢分類集合
List<Category> list = categoryService.queryAll();
//2: 請求轉發
request.setAttribute("list",list);
request.getRequestDispatcher("list.jsp").forward(request,response);
}
}
web\list.jsp
<%@ 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>Title</title>
</head>
<body>
<c:forEach items="${list}" var="item">
<div > ${item.cname} </div>
</c:forEach>
</body>
</html>
maxTotal=30
maxIdle=10
url=localhost
port=6379
src\com\wzx\util\JedisUtils.java
public class JedisUtils {
private static JedisPool pool = null;
//1:建立一個連線池
static{
//1.1 解析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建立連線池
//1:建立連線池的設定物件
JedisPoolConfig config = new JedisPoolConfig();
//1.1 設定最大連線數
config.setMaxTotal(Integer.parseInt(maxTotal));
//1.2 設定空閒連線數
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();
}
}
}