当前位置:网站首页> 微信小程序缓存过期时间的相关设置(推荐)
微信小程序缓存过期时间的相关设置(推荐)
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; },到此这篇关于微信小程序缓存过期时间的相关设置的文章就介绍到这了,更多相关微信小程序缓存过期时间内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- JPA handwritten SQL, received with user-defined entity classes
- [leetcode] length of the last word [58]
- Kubernetes resource object introduction and common commands (III)
- Concurrentskiplistmap -- principle of table skipping
- Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
- [LeetCode] 最后一个单词的长度【58】
- TS initial use, TS type
- 写给当前及未来博士研究生一些建议整理分享
- SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
- 华为HMS Core携手超图为三维GIS注入新动能
猜你喜欢
![[embedded system course design] a single key controls the LED light](/img/c9/076618208bbab0b95faa5a7e644a07.png)
[embedded system course design] a single key controls the LED light

Use pair to do unordered_ Key value of map

BlocProvider为什么感觉和Provider很相似?

第六章 数据流建模

【QT】Qt 使用MSVC2017找不到编译器的解决办法

Pytorch learning record

S32Kxxx bootloader之UDS bootloader

Key points of security agreement
![[es practice] safe operation mode on ES](/img/3f/fa28783770098ff10bffeccd64fe51.png)
[es practice] safe operation mode on ES

2021 robocom world robot developer competition - preliminary competition of higher vocational group
随机推荐
Shell custom function
【QT】测试Qt是否能连接上数据库
【QT】QtCreator卸载与安装(非正常状态)
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
ADO. Net SqlCommand object
ARP message header format and request flow
TS初次使用、ts类型
Why does blocprovider feel similar to provider?
2021 robocom world robot developer competition - preliminary competition of undergraduate group
安全协议重点
algolia 搜索需求,做的快自闭了...
第六章 数据流建模
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
Applet form verification encapsulation
2021 RoboCom 世界机器人开发者大赛-本科组初赛
PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis
【QT】对于Qt MSVC 2017无法编译的问题解决
Iota in golang
关联性——组内相关系数
[QT] test whether QT can connect to the database