带有redis缓存的修改,对redis的操作解析

来源:互联网 发布:linux chmod 777 目录 编辑:程序博客网 时间:2024/05/16 17:05
public EgoResult updContent(TbContent content) {EgoResult er = new EgoResult();//更新数据库content.setUpdated(new Date());int index = tbContentDubboService.updContent(content);if (index > 0) {// 先判断缓存中得到key是否存在// redis -- db 原则:保持一致!//更新成功,此时数据库和redis中缓存的数据如果有值肯定已经不一样了if (jedisPoolDaoImpl.exists(key)) {//缓存中有值,且此时值已经和数据库中的不一致,则要么删除缓存的数据要么修改缓存中的数据//取出缓存中的值赋给jsonString json = jedisPoolDaoImpl.get(key);if (json!=null && !"".equals(json)) {//上面已经判断存在缓存json不为空,这里是不是就不用再加if判断// 使用JsonUtils工具类。将json转化为List<TbContent>List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);// 声明一个变量记录相同数据的id,// 如果循环遍历找到相同的ID则只需要把该id的TbContent对象的内容修改位前台的传递的对象,// 再把修改后的缓存数据作为缓存,// 如果没有找到相同ID则 说明之前缓存中没有该对象的缓存int listIndex = -1;for (int i = 0; i < list.size(); i++) {if (list.get(i).getId()==content.getId()) {listIndex = i;break;}}// 如果redis 中没有要更新的数据===key已然与数据库不符合    违背原则-删掉if (listIndex==-1) {//没有找到相同ID的TbContent对象,说明之前的缓存中没有对该对象的缓存jedisPoolDaoImpl.del(key);//如果程序执行到这,此时对大广告位已经没有缓存}else{//删除redis中该对象的环迅list.remove(listIndex);//添加为新的缓存list.add(listIndex, content);//将最新的缓存数据作为缓存String newJson = JsonUtils.objectToJson(list);jedisPoolDaoImpl.set(key, newJson);}}}er.setStatus(200);}else{er.setStatus(400);}return er;}

原创粉丝点击