java web redis_JavaWeb——redis【综合案例】使用redis缓存商品分类

JavaWeb——redis【综合案例】使用redis缓存商品分类

思路分析:

查询redis,没有json数据,就调用CategoryDao,去获取集合List

先返回给CategorySrvice,再转成json存进redis

第二次起,直接获取redis中的json,将json转成List

我这边使用测试驱动开发,所以,我先写一个测试类,目的是明确需求:

CategoryServiceTest类

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 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 findAll() throws IOException {

CategoryDao categoryDao = new CategoryDao();

List 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 result=null;

static List list;

static {

//1:创建集合

list = new ArrayList<>();

//使用循环模拟数据,以后使用mybatis来查数据

for (int i = 0; i < 10; i++) {

list.add(new Category(i, "分类名" + i));

}

}

public List 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>() {});//将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

运行效果如下:

原文作者:水巷石子

原文出处:CSDN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值