JavaWeb-redis [comprehensive case] Use redis to cache product classification

JavaWeb-redis [comprehensive case] Use redis to cache product classification

Thinking analysis:

img

  1. Query redis, if there is no json data, call CategoryDao to get the collection, List<Category>
    first return to CategorySrvice, and then convert to json and store in redis
  2. From the second time, directly get the json in redis and convert the json intoList<Category>

I use test-driven development here, so I first write a test class, the purpose is to clarify the requirements:

CategoryServiceTest class

package com.lbl.service;

import com.lbl.bean.Category;
import org.junit.Test;

import java.io.IOException;
import java.util.List;


public class CategoryServiceTest {
    
    
    @Test
    public void test01() throws IOException {
    
    
        //1.创建服务类对象
        CategoryService categoryService = new CategoryService();

        //2.查询分类集合
        List<Category> list=categoryService.findAll();

        //打印出结果
        for (Category c:list){
    
    
            System.out.println(c);
        }
    }
}

CategoryService

package com.lbl.service;

import com.lbl.bean.Category;
import com.lbl.dao.CategoryDao;

import java.io.IOException;
import java.util.List;

public class CategoryService {
    
    
    public List<Category> findAll() throws IOException {
    
    
        CategoryDao categoryDao = new CategoryDao();
        List<Category> result = categoryDao.findAll();
        return result;
    }
}

Category

package com.lbl.bean;

public class Category {
    
    
    private int cid;
    private String cname;

    public Category() {
    
    
    }

    public Category(int cid, String cname) {
    
    
        this.cid = cid;
        this.cname = cname;
    }

    public int getCid() {
    
    
        return cid;
    }

    public void setCid(int cid) {
    
    
        this.cid = cid;
    }

    public String getCname() {
    
    
        return cname;
    }

    public void setCname(String cname) {
    
    
        this.cname = cname;
    }

    @Override
    public String toString() {
    
    
        return "Category{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }
}

CategoryDao

package com.lbl.dao;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lbl.bean.Category;
import com.lbl.utils.JedisUtils;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDao {
    
    


    List<Category> result=null;

    static List<Category> list;

    static {
    
    
        //1:创建集合
        list = new ArrayList<>();
        //使用循环模拟数据,以后使用mybatis来查数据
        for (int i = 0; i < 10; i++) {
    
    
            list.add(new Category(i, "分类名" + i));
        }
    }

    public List<Category> findAll() throws IOException {
    
    
        //先访问redis,如果有数据,则直接返回
        //如果redis为空,则访问数据库
        Jedis redis = JedisUtils.getRedis();
        String json = redis.get("list");

        if (json == null) {
    
    
            //说明redis中没有,所以要去数据库查
            System.out.println("redis中没有数据,要从数据库中查询");

            //因为没有查到,所以说明redis没有该数据,所以为了下次能快速查找,我们将数据存入redis
            //这里不能直接存,而要将集合list转为json类型,再存
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonValue = objectMapper.writeValueAsString(list);
            redis.set("list", jsonValue);
            return list;
        } else {
    
    
            //redis中有数据,所以可以直接返回
            //但这里有问题,就是redis中存的是string类型的数据
            //所以我们要将string转为list
            System.out.println("redis中有数据,直接获取redis缓存中的数据!");
            ObjectMapper objectMapper = new ObjectMapper();
            result = objectMapper.readValue(json, new TypeReference<List<Category>>() {
    
    });//将json转成对象   参1 json数据  参2
            return result;
        }
    }
}

JedisUtils

package com.lbl.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

public class JedisUtils {
    
    
    static JedisPool pool;
    //1.创建一个连接池
    static {
    
    
        //1.1解析properties文件
        ResourceBundle bundle = ResourceBundle.getBundle("redis");
        //获取参数
        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));

        //1.3创建连接池
        pool = new JedisPool(config,url,Integer.parseInt(port));
    }

    //2.对外开放一个获取jedis的方法
    public static Jedis getRedis(){
    
    
        return pool.getResource();
    }

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

redis.properties

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

The operation effect is as follows:

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108714923