Day11【Redis】综合案例 使用redis缓存商品分类***

思路分析

在这里插入图片描述

  • 第一次
    1:查询redis,
    2:没有json数据,就调用CategoryDao,去获取集合List< Category>
    3、4、5:返回给CategorySrvice,将集合转成json存进redis,再将集合返回给客户端。
  • 第二次
    直接获取redis中的json,将json转成List< Category>

环境搭建

在这里插入图片描述
src\jedis.properties

maxTotal=30
maxIdle=10
minIdle=5
url=localhost
port=6379

src\cn\cyl\util\JedisUtils.java

public class JedisUtils {
    //定义为成员变量
    private static JedisPool pool;
    //静态代码在项目中,如果被使用只会加载一次
    static {
        //读取配置文件文件
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");
        //获取参数
        String maxTotal = bundle.getString("maxTotal");
        String maxIdle = bundle.getString("maxIdle");
        String minIdle = bundle.getString("minIdle");
        String url = bundle.getString("url");
        String port = bundle.getString("port");

        //1.创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //设置最大连接数
        config.setMaxTotal(Integer.parseInt(maxTotal));
        //设置最大空闲连接数
        config.setMaxIdle(Integer.parseInt(maxIdle));
        //设置最小空闲连接数
        config.setMinIdle(Integer.parseInt(minIdle));
        //2.创建连接池
        pool = new JedisPool(config,url,Integer.parseInt(port));
    }

    //3.从连接池获取一个连接
    public static Jedis getRedis() {
        Jedis jedis = pool.getResource();
        return jedis;
    }

    //4:提供释放资源的方法
    public static void close(Jedis jedis){
        if(jedis != null) {
            jedis.close();
        }
    }
}

(1) 编写测试类

test\java\cn\cyl\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);
        }
    }
}

(2)编写javaBean与CategoryDao

src\cn\cyl\bean\Category.java

public class Category {
    private int cid;
    private String cname;
    //省略getset 构造
}

src\cn\cyl\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;
    }
}

(3)编写CategoryService

public class CategoryService {
    CategoryDao dao = new CategoryDao();
    ObjectMapper objectMapper = new ObjectMapper();

    public List<Category> queryAll() throws IOException {
        //queryAll的逻辑
        //1.先访问redis,有数据就返回
        //获取连接
        Jedis jedis = JedisUtils.getRedis();
        //查redis内存是否有数据
        String json = jedis.get("list");
        if (json==null){
            System.out.println("第一次查询 redis没有数据,查的数据库的数据,慢");
            //2.没有数据就查询数据,进行缓存,方便下次使用
            List<Category> list = dao.findAll();
            //缓存,将集合转换成json,缓存到redis
            //创建ObjectMapper对象,转list成json
            String jsonValue = objectMapper.writeValueAsString(list);
            System.out.println(jsonValue);
            jedis.set("list",jsonValue);
            return list;
        }else {
            System.out.println("第二次 redis有数据,直接返回,快");
            //将json转成对象   参1 json数据  参2 new TypeReference<返回值类型>(){}
            List<Category> list = objectMapper.readValue(json,new TypeReference<List<Category>>(){});
            return list;
        }
    }
}

(4)测试 将json数据转成对象

test\java\cn\cyl\pack01\TestObjectMapper.java

public class TestObjectMapper {
    @Test
    public void test01() throws IOException {
        String json = "[{\"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\"},{\"cid\":10,\"cname\":\"分类名10\"}]";

        ObjectMapper objectMapper = new ObjectMapper();
        //将json转成对象   参1 json数据  参2 new TypeReference<返回值类型>(){}
        List<Category> list = objectMapper.readValue(json,new TypeReference<List<Category>>(){});

        System.out.println(list);
    }
}

(5)过滤器解决全站中文编码乱码

src\cn\cyl\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 {

    }
}

(6)Servlet显示分类列表

@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 {
        //1.创建业务类对象
        CategoryService categoryService = new CategoryService();
        //2.查询分类集合
        List<Category> list = categoryService.queryAll();

        //请求转发
        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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值