当前位置:网站首页> 微信小程序缓存过期时间的相关设置(推荐)
微信小程序缓存过期时间的相关设置(推荐)
2022-07-01 23:50:00 【1024问】
微信小程序缓存机制介绍
哪些是一定需要过期的缓存
写法
微信小程序缓存机制介绍每个微信小程序都可以有自己的本地缓存,可以通过 wx.setStorage(wx.setStorageSync)
、wx.getStorage(wx.getStorageSync)
、wx.clearStorage(wx.clearStorageSync)
可以对本地缓存进行设置、获取和清理。
但是微信默认设置了缓存是无限长的过期时限,这对于我们的小程序开发,是非常不好的。
哪些是一定需要过期的缓存我正在开发的项目中,用户通过第三方登录拿到一个token
,每次请求都必须带上token
,但是token
的时限是30分钟。
所以token
就属于有必要当成缓存,但是又必须设置缓存时限的数据。
在用户登录成功后,把过期时间、token一起存入缓存:
// 设置token缓存wx.setStorageSync('token', res.data.token);// 当前时间var timestamp = Date.parse(new Date());// 加上过期期限var expiration = timestamp + 1200000; //缓存20分钟// 存入缓存wx.setStorageSync('data_expiration', expiration);
在app.js
入口文件里,监测是否超时:
// 缓存是否过期 _isExpiration() { // 当前时间 var timestamp = Date.parse(new Date()); // 缓存中的过期时间 var data_expiration = wx.getStorageSync("data_expiration"); // 如果缓存中没有data_expiration,说明也没有token,还未登录 if (data_expiration) { // 如果超时了,清除缓存,重新登录 if (timestamp > data_expiration) { wx.clearStorageSync(); return true; }else{ return false; } } return true; },
到此这篇关于微信小程序缓存过期时间的相关设置的文章就介绍到这了,更多相关微信小程序缓存过期时间内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- ADO.NET 之sqlConnection 对象使用摘要
- notBlank 和 notEmpty
- Is it safe to buy funds on Great Wall Securities?
- 写给当前及未来博士研究生一些建议整理分享
- Redis RDB snapshot
- S32Kxxx bootloader之UDS bootloader
- Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
- Iota in golang
- Redis AOF日志
- 北京炒股开户选择手机办理安全吗?
猜你喜欢
使用 pair 做 unordered_map 的键值
【ES实战】ES上的安全性运行方式
【QT】对于Qt MSVC 2017无法编译的问题解决
PyTorch学习记录
【CMake】Qt creator 里面的 cmake 配置
Redis AOF日志
Digital transformation has a long way to go, so how to take the key first step
S32Kxxx bootloader之UDS bootloader
Why does blocprovider feel similar to provider?
notBlank 和 notEmpty
随机推荐
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
Relatively easy to understand PID understanding
Kubernetes resource object introduction and common commands (III)
常见的积分商城游戏类型有哪些?
Overview of edge calculation
[C #] dependency injection and Autofac
[LeetCode] 最后一个单词的长度【58】
[must] bm41 output the right view of the binary tree [medium +]
学成在线案例实战
2021 robocom world robot developer competition - preliminary competition of undergraduate group
【QT】对于Qt MSVC 2017无法编译的问题解决
边缘计算概述
Practical application and extension of plain framework
MySQL Replication中并行复制怎么实现
openwrt 开启KV漫游
mysql:insert ignore、insert和replace区别
Windows installation WSL (II)
E-commerce RPA robot helps brand e-commerce to achieve high traffic
. env. XXX file, with constant, but undefined
有没有一段代码,让你为人类的智慧所折服