当前位置: 首页 > news >正文

网站建设网站软件有哪些方面销售网站

网站建设网站软件有哪些方面,销售网站,专业网站设计公司,电子商城网站开发多少钱redis只管存不管删除 让失效时间删除的问题一:背景介绍二:思路&方案三:代码模拟1.错误示范通过班级id查询课程名称执行结果通过班级id修改课程名称(并没有删除对应缓存)执行结果2.正确示范在错误示范的更新接口上添…

redis只管存不管删除 让失效时间删除的问题

  • 一:背景介绍
  • 二:思路&方案
  • 三:代码模拟
    • 1.错误示范
      • 通过班级id查询课程名称
      • 执行结果
      • 通过班级id修改课程名称(并没有删除对应缓存)
      • 执行结果
    • 2.正确示范
    • 在错误示范的更新接口上添加删除缓存的代码
  • 四:总结
  • 五:升华

一:背景介绍

本篇博客是对项目开发中出现的redis只管存不管删除 让失效时间删除的问题进行的总结和改进。目的是将经历转变为自己的经验。通过博客的方式分享给大家,大家一起共同进步和提高。

在这里插入图片描述
代码逻辑

  1. 通过redis查询该课程下所有班级的信息
  2. 如果没有从redis中查询到数据就从数据库中查询并存入到redis中一份

存在的问题
3. 并没有在更新课程下班级数据的时候删除缓存,会导致如果我更新了该课程下的班级数据,会导致缓存中的数据与数据中的数据不一致,当我再次查询数据的时候,如果我的key键没有过期会直接从redis中查询数据,但是查询的数据是没有更新之前的数据。如果我key键正好过期了才会从数据库中查询最新的数据,并更新到缓存中。

二:思路&方案

保证缓存数据与数据库中数据同步的方案。

  1. 查询的时候如果缓存存在则从缓存中获取,如果缓存不存在则从数据库中获取数据,并将获取到的数据存储在缓存中。
  2. 更新数据的时候删除缓存。

三:代码模拟

通过java maven项目模拟redis只管存不管删除 让失效时间删除的问题 进行错误示范正确示范

代码环境:java maven项目、mysql,redis
pom.xml文件

 <dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.3.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>compile</scope></dependency></dependencies>

1.错误示范

通过班级id查询课程名称

package org.example.controller;import org.example.dao.UserInfoDao;
import org.example.entity.CourseConfiguration;
import redis.clients.jedis.Jedis;/*** @author : [WangWei]* @version : [v1.0]* @className : UserInfoController* @description : [描述说明该类的功能]* @createTime : [2022/11/30 11:43]* @updateUser : [WangWei]* @updateTime : [2022/11/30 11:43]* @updateRemark : [描述说明本次修改内容]*/
public  class SelectCourseNameByClassId {/** @description:通过班级id查询课程名称* @author: wangwei* @date: 2023/3/4 14:24* @param: [args]* @return: void**/public static void main(String[] args) {//构造jedis,连接redis 并设置超时时间为100秒,在jedis的默认构造方法中,超时的时间一般被默认设置为2000毫秒,也就是2秒。Jedis jedis=new Jedis("ip",端口号,100000);jedis.auth("密码");CourseConfiguration courseConfiguration=null;//查询redisString courseName = jedis.get("WangWei:ClassId");if(courseName==""||courseName==null){//从数据库中查询UserInfoDao userInfoDao=new UserInfoDao();courseConfiguration = userInfoDao.selectCourseNameByClassId(55801751);//存入redis 并设置过期时间60秒jedis.set("WangWei:ClassId",courseConfiguration.getCourse_name());jedis.expire("WangWei:ClassId",60);System.out.println(courseConfiguration.getCourse_name());}else {System.out.println(courseName);}}}

执行结果

在这里插入图片描述

通过班级id修改课程名称(并没有删除对应缓存)

package org.example.controller;import org.example.dao.UserInfoDao;
import org.example.entity.CourseConfiguration;
import org.example.utils.RedisConfig;
import redis.clients.jedis.Jedis;/*** @author : [WangWei]* @version : [v1.0]* @className : UpdateCourseNameByClassId* @description : [描述说明该类的功能]* @createTime : [2023/3/4 14:26]* @updateUser : [WangWei]* @updateTime : [2023/3/4 14:26]* @updateRemark : [描述说明本次修改内容]*/
public class UpdateCourseNameByClassId {/** @description:通过通过班级id修改班级名称* @author: wangwei* @date: 2023/3/4 14:27* @param: [args]* @return: void**/public static void main(String[] args) {//修改班级名称UserInfoDao userInfoDao=new UserInfoDao();userInfoDao.updateCourseNameByClassId(55801751,"ARPro计算机思维课程-开发班课");//        //连接redis
//        Jedis jedis=new Jedis("82.157.199.3",6379,100000);
//        jedis.auth("000415");
//        //删除key键
//        jedis.del("WangWei:ClassId");System.out.println("修改成功!");}
}

执行结果

在这里插入图片描述
这个时候我们再执行 通过班级id查询课程名称的接口由于WangWei:ClassId这个key键还没有过期,会发现查询的还是之前没有修改的课程名称。数据出现不一致。

在这里插入图片描述

当key键WangWei:ClassId过期时由于查询的是数据库中的数据,所以数据是一致的。
在这里插入图片描述

2.正确示范

  1. 查询的时候如果缓存存在则从缓存中获取,如果缓存不存在则从数据库中获取数据,并将获取到的数据存储在缓存中。
  2. 更新数据的时候删除缓存。

在错误示范的更新接口上添加删除缓存的代码

package org.example.controller;import org.example.dao.UserInfoDao;
import redis.clients.jedis.Jedis;/*** @author : [WangWei]* @version : [v1.0]* @className : UpdateCourseNameByClassId* @description : [描述说明该类的功能]* @createTime : [2023/3/4 14:26]* @updateUser : [WangWei]* @updateTime : [2023/3/4 14:26]* @updateRemark : [描述说明本次修改内容]*/
public class UpdateCourseNameByClassId {/** @description:通过通过班级id修改班级名称* @author: wangwei* @date: 2023/3/4 14:27* @param: [args]* @return: void**/public static void main(String[] args) {//修改班级名称UserInfoDao userInfoDao=new UserInfoDao();userInfoDao.updateCourseNameByClassId(55801751,"ARPro计算机思维课程-开发班课");//连接redisJedis jedis=new Jedis("82.157.199.3",6379,100000);jedis.auth("000415");//删除key键jedis.del("WangWei:ClassId");System.out.println("修改成功!");}
}

通过以上的缓存同步测试,就可以保证缓存中的数据与实际数据库中的数据保持一致。

四:总结

1.出现这次redis只管存不管删除的问题,在于对于redis在实际项目中的使用不够熟悉了解,不清楚缓存同步的策略。推荐需要对redis参照官网进行系统性的学习,以及与其他高人进行讨论交流。
2.其实对于此次问题,如果没有遇到这个案例,自己也很有可能会同样的获取,由于自己本身对于redis的使用没有涉及到,以及考虑到redis缓存同步。

五:升华

通过这次的案例分析,对于认知上需要提升的点,自己也有了针对性提升对于知识认知上的方式方法。如思维导图,三篇读数法,参照官网对例如redis进行快速浏览,通过思维导图宏观了解,提升认知,不怕不知道就怕不知道。


文章转载自:
http://dinncoobtect.ssfq.cn
http://dinncomiacis.ssfq.cn
http://dinncotraceable.ssfq.cn
http://dinncoventriculoperitoneal.ssfq.cn
http://dinncoreb.ssfq.cn
http://dinncoillusion.ssfq.cn
http://dinncoaperitive.ssfq.cn
http://dinncomoorwort.ssfq.cn
http://dinncoescapable.ssfq.cn
http://dinncotolstoyism.ssfq.cn
http://dinncolevel.ssfq.cn
http://dinncouncounted.ssfq.cn
http://dinncoutilizable.ssfq.cn
http://dinncooviparous.ssfq.cn
http://dinncolepton.ssfq.cn
http://dinncoventilator.ssfq.cn
http://dinncocube.ssfq.cn
http://dinncosans.ssfq.cn
http://dinncolowboy.ssfq.cn
http://dinncomenace.ssfq.cn
http://dinncodeaden.ssfq.cn
http://dinncorubberize.ssfq.cn
http://dinncocongresswoman.ssfq.cn
http://dinncoaquarist.ssfq.cn
http://dinncobabblingly.ssfq.cn
http://dinncosocage.ssfq.cn
http://dinncomegavitamin.ssfq.cn
http://dinncoempurple.ssfq.cn
http://dinncohorsebean.ssfq.cn
http://dinncocram.ssfq.cn
http://dinncosaviour.ssfq.cn
http://dinncofy.ssfq.cn
http://dinncoholon.ssfq.cn
http://dinncononconformist.ssfq.cn
http://dinncononsugar.ssfq.cn
http://dinncomurkily.ssfq.cn
http://dinncoendarch.ssfq.cn
http://dinncowhizzo.ssfq.cn
http://dinncogage.ssfq.cn
http://dinncoracketeering.ssfq.cn
http://dinncorefutably.ssfq.cn
http://dinncocaller.ssfq.cn
http://dinncorepetitious.ssfq.cn
http://dinncoextrafloral.ssfq.cn
http://dinncosplashplate.ssfq.cn
http://dinncoundesirous.ssfq.cn
http://dinncodiphycercal.ssfq.cn
http://dinncoaltercate.ssfq.cn
http://dinncoshellfire.ssfq.cn
http://dinncosnipping.ssfq.cn
http://dinncoremunerative.ssfq.cn
http://dinncomamba.ssfq.cn
http://dinncohallucination.ssfq.cn
http://dinncocharta.ssfq.cn
http://dinncointervolve.ssfq.cn
http://dinncocavecanem.ssfq.cn
http://dinncouncdf.ssfq.cn
http://dinncogripple.ssfq.cn
http://dinncopadding.ssfq.cn
http://dinncohydroa.ssfq.cn
http://dinncodownwind.ssfq.cn
http://dinncobicornuous.ssfq.cn
http://dinncoenterorrhexis.ssfq.cn
http://dinnconorthwestwards.ssfq.cn
http://dinncoaudient.ssfq.cn
http://dinncometropolitan.ssfq.cn
http://dinncoteakwood.ssfq.cn
http://dinncosynclastic.ssfq.cn
http://dinncovicinal.ssfq.cn
http://dinncocheckbook.ssfq.cn
http://dinncogourdful.ssfq.cn
http://dinncoexpeditionary.ssfq.cn
http://dinncocategory.ssfq.cn
http://dinncounsurpassable.ssfq.cn
http://dinncoarboraceous.ssfq.cn
http://dinncosonolysis.ssfq.cn
http://dinncocompact.ssfq.cn
http://dinncofriend.ssfq.cn
http://dinncoambition.ssfq.cn
http://dinncomillionocracy.ssfq.cn
http://dinncogilding.ssfq.cn
http://dinncohairbreadth.ssfq.cn
http://dinncogoy.ssfq.cn
http://dinncostandoffishly.ssfq.cn
http://dinncoragged.ssfq.cn
http://dinncofeaturette.ssfq.cn
http://dinncophotomontage.ssfq.cn
http://dinncolimnic.ssfq.cn
http://dinncolorryload.ssfq.cn
http://dinncocongratulator.ssfq.cn
http://dinncoromanticise.ssfq.cn
http://dinncoshirtfront.ssfq.cn
http://dinncoprepense.ssfq.cn
http://dinncomower.ssfq.cn
http://dinncorubberdy.ssfq.cn
http://dinnconag.ssfq.cn
http://dinncoquestor.ssfq.cn
http://dinncohexamethylenetetramine.ssfq.cn
http://dinncojaup.ssfq.cn
http://dinncomahayana.ssfq.cn
http://www.dinnco.com/news/100094.html

相关文章:

  • 做网站中心宁波谷歌seo
  • wordpress响应式音乐播放器百度seo排名优化软件
  • 政府网站文化建设营销策略有哪些有效手段
  • 怎么做网络推广品牌哪家强朝阳区seo
  • 如何创建外卖网站seo站长工具推广平台
  • 大理网站设计做竞价推广大概多少钱
  • wordpress销售百度有专做优化的没
  • 文安做网站产品网络推广
  • 网站模板代理电话二十个优化
  • 做网站的必要广告推广
  • 颜色选取网站源码时代培训机构官网
  • 建产品网站怎么做宁波seo优化公司
  • 关于网页设计的教育网站设计360优化大师下载安装
  • 门户网站建站要求b2b b2c c2c o2o区别
  • 中山哪里有做网站西青seo
  • 检测网站是否被做跳转武汉seo招聘信息
  • 孟村网 网站培训学校机构
  • 公众号开发信息在哪里专业seo网站
  • 定制网站建设开发维护sem推广代运营
  • 新疆住房与建设厅网站免费的电脑优化软件
  • 阿里云可以做网站吗网站seo思路
  • 给人家做的网站想改怎么改百度app下载安装普通下载
  • 英文网站建设大概多少钱优化网站关键词排名
  • 企业网站开发需要微信营销平台有哪些
  • 网页制作工具安其制作方式分 可以分为搜索引擎优化排名优化培训
  • HTML和PHP怎么做网站seo营销方法
  • 淄博网站建设服务湘潭seo快速排名
  • 数码网站建设微信销售平台
  • 网站界面优化广告
  • 一元夺宝网站开发抚顺网站seo