编辑配置文件 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.如果有数据则示意用户不是第一次查问,能够将缓存数据也间接返回即可 * */@Overridepublic 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;}