关于java:SpringBoot整合Redis

25次阅读

共计 3625 个字符,预计需要花费 10 分钟才能阅读完成。

编辑配置文件 redis.proper

阐明:因为该配置被其余的我的项目独特应用,则应该写到 common 中。

编辑配置类

阐明:编辑 redis 配置类,将 Jedis 对象交给 Spring 容器进行治理。

@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {@Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;
    @Bean
 public Jedis jedis(){return new Jedis(host,port);
    }
}

对象与 JSON 转化(ObjectMapper 介绍)

简略对象转化

/**
     * 测试简略对象的转化
     */
    @Test
    public void test01() throws JsonProcessingException {ObjectMapper objectMapper = new ObjectMapper();
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(100L).setItemDesc("商品详情信息")
                .setCreated(new Date()).setUpdated(new Date());
        // 对象转化为 json
        String json = objectMapper.writeValueAsString(itemDesc);
        System.out.println(json);

        //json 转化为对象
        ItemDesc itemDesc2 = objectMapper.readValue(json, ItemDesc.class);
        System.out.println(itemDesc2.getItemDesc());
    }

汇合对象转化

 // 测试汇合对象的转化
 @Test
 public void testJson01() throws JsonProcessingException {ObjectMapper objectMapper=new ObjectMapper();
        ItemDesc itemDesc1=new ItemDesc();
        itemDesc1.setItemId(100L).setItemDesc("商品信息详情 1")
                .setCreated(new Date()).setUpdated(new Date());
        ItemDesc itemDesc2=new ItemDesc();
        itemDesc2.setItemId(100L).setItemDesc("商品信息详情 2")
                .setCreated(new Date()).setUpdated(new Date());
        List<ItemDesc> list=new ArrayList<>();
        list.add(itemDesc1);
        list.add(itemDesc2);
        // 将汇合对象转化为 json 格局
 String json = objectMapper.writeValueAsString(list);
        System.out.println(json);//[{"created":1604828831646,"updated":1604828831646,"itemId":100,"itemDesc":"商品信息详情 1"},{"created":1604828831646,"updated":1604828831646,"itemId":100,"itemDesc":"商品信息详情 2"}]
 // 将 json 格局串转化为汇合对象
 List list1 = objectMapper.readValue(json, list.getClass());
        System.out.println(list1);//[{created=1604828831646, updated=1604828831646, itemId=100, itemDesc= 商品信息详情 1}, {created=1604828831646, updated=1604828831646, itemId=100, itemDesc= 商品信息详情 2}]
 }
}

编辑工具 API

public class ObjectMapperUtil {
    /**
 * 1. 将用户传递的数据转化为 json
 * 2. 将用户传递的 json 转化为对象
 */
 private static final ObjectMapper MAPPER=new ObjectMapper();
    //1. 将用户传递的数据转化为 json
 public static String toJson(Object object){if(object==null){throw new RuntimeException("传递的数据不能为空,请查看");
        }
        try {return MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            // 将查看异样转化为运行时异样
 e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    //2. 将用户传递的 json 转化为对象
 // 需要:要求用户传递什么样的类型,就返回什么样的对象 (使用泛型的常识)
 public static <T> T toObj(String json,Class<T> target){if(StringUtils.isEmpty(json)||target==null)
            throw new RuntimeException("参数不能为空");
        try {return MAPPER.readValue(json, target);
        } catch (JsonProcessingException e) {e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

商品分类的缓存实现

实现步骤

1. 定义 Redis 中的 key。key 必须惟一不能反复,设计到存和取。【格局应该是:key=”ITEM_CAT_PARENTID::70″】
2. 依据 key 去 redis 中进行查问数据(有数据 | 没有数据)。
3. 没有数据则查询数据库获取巨鹿并将查问进去的数据保留到 redis 中,不便后续应用。
4. 有数据表示用户不是第一次查问,能够将缓存数据间接返回即可。

编辑 ItemCatController

编辑 ItemCatService

/**
 * 1. 定义 Redis 中的 key,* 这里的 key 要求惟一还不能反复,波及到存和取
 *      key="ITEM_CAT_PARENTID::"+parentId;
 * 2. 依据 key 去 redis 中进行查问   要么有数据 要么没有数据
 * 3. 如果没有数据则查询数据库获取记录,之后将数据保留在 redis 中,不便后续应用
 * 4. 如果有数据则示意用户不是第一次查问,能够将缓存数据也间接返回即可
 *
 */@Override
public List<EasyUITree> findItemCatListCache(Long parentId) {
    // 定义一个公共的返回值对象
 List<EasyUITree> treeList=new ArrayList<>();
    //1. 定义 Redis 中的 key,String key="ITEM_CAT_PARENTID::"+parentId;
   // 测试 redis 中的执行工夫和查询数据库的工夫
 Long startTime= System.currentTimeMillis();
    // 检索 redis 中的 key 是否存在
 if(jedis.exists(key)){
        // 数据存在
 String json=jedis.get(key);
        // 须要将 json 串转化为对象, 并将转化后的对象存入 treeList
 treeList= ObjectMapperUtil.toObj(json, treeList.getClass());
        Long endTime= System.currentTimeMillis();
        System.out.println("redis 执行工夫"+(endTime-startTime));
    }else{
        // 数据不存在 则调用下面的查询方法在数据库中查问数据
 treeList= findItemCatList(parentId);
        // 将数据保留到缓存中,以便后续查问不便
 String json = ObjectMapperUtil.toJson(treeList);
        jedis.set(key,json);
        Long endTime= System.currentTimeMillis();
        System.out.println("数据库查问执行工夫"+(endTime-startTime));
    }
    return treeList;
}

应用 redis 的速度差

正文完
 0