[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>需求
最近有一个有趣的需求,用户家庭账号体系的建立,前提(小程序开发,前端无法检测小程序彻底退出),家庭中的成员账号A切换登录之前还需要判断另一个账B号的最后使用时间,如果账号A在
两个小时内登录过,会对账号A有一个提示,是否挤掉账号B,同时账号B再调用请求的时候,也会收到被账号A挤掉通知,退出到授权登录界面。
<>解决办法
由于上面的需求想到的解决办法是使用Redis,Redis中为每一个账号设置一个有效时间(2小时)的token值,通过判断每一个用户token值是否存在,来判断挤掉账号时候各种提醒内容。但是这里会有一个问题,需求要求token失效时间是2小时,上一个账号两个小时内
登录过才会进行提醒(需要判断上一个账号最后更新token时间),所以前端在每个请求调用的时候,后端都需要更新一下redis的过期时间。
* Node.js中引用Redis var redis = require('redis'), PORT = 6389,//端口号 HOST =
'127.0.0.1', //服务器IP PWD = '88888888888888', //密码 OPTS = {},//设置项 rclient =
redis.createClient(RDS_PORT,RDS_HOST,RDS_OPTS);
rclient.auth(RDS_PWD,function(){ console.log('redis通过认证'); }); //redis数据库
rclient.set('token','kaola');//赋值
* Redis设置key的过期时间 rclient.expire('token',60);//设置60秒自动过期
*
Redis更新key的过期时间
对一个已经带有生存时间的 key 执行 EXPIRE 命令,新指定的生存时间会取代旧的生存时间
rclient.expire('token',60);//更新token过期时间60秒
附:上面提到的小需求是目前想到的办法,如果有小伙伴有更好的实现方式可以评论我们一起讨论哦。