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

uniapp数据缓存

利用uniapp做开发时,缓存数据是及其重要的,下面是同步缓存和异步缓存的使用

同步缓存

在执行同步缓存时会阻塞其他代码的执行

uni.setStorageSync(key, data)

设置缓存,如:

uni.setStorageSync('name', '张三')

uni.getStorageSync(key)

获取缓存,如:

uni.getStorageSync('name')

uni.removeStorageSync(key)

移除缓存,如:

uni.removeStorageSync('name')

uni.clearStorageSync()

清空所有缓存,如:

uni.clearStorageSync()

uni.getStorageInfoSync()

获取缓存更详细的信息,正如缓存中所有的key,如:

let res = uni.getStorageInfoSync()
//  取出缓存中所有的key,数组形式,如['name','age', ...]
let allStorageKeys = res.keys

异步缓存

异步缓存不会阻塞代码的执行,但是需要利用回调的特点,即执行成功之后要执行的代码放success中,失败的代码放fail中,一定要执行的代码放complete中

uni.setStorage(OBJECT)

设置缓存,如:

uni.setStorage({key: 'name',data: '张三'
})

uni.getStorage(OBJECT)

获取缓存,如:

uni.getStorage({key: 'name',success: (storage) => {//  获取key对应的valueconsole.log('value: ', storage.data)}
})

uni.removeStorage(OBJECT)

移除缓存,如:

uni.removeStorage({key: removeAsyncKey.value
})

uni.clearStorage()

清空所有缓存,如:

uni.clearStorage()

uni.getStorageInfo(OBJECT)

获取缓存更详细的信息,正如缓存中所有的key,如:

uni.getStorageInfo({success: (res) => {//  取出缓存中所有的key,数组形式,如['name','age', ...]let allStorageKeys = res.keysconsole.log(allStorageKeys)}
})

uniapp案例

页面如下:

以下是用Vue3语法写的uniapp测试缓存的代码

<template><view class="root"><view class="asyncStorageBox"><view class="title"><text>异步缓存</text></view><view class="set"><text>key: </text><input type="text" v-model="setAsyncKey" /><text>value: </text><input type="text" v-model="setAsyncValue"/><button @click="setAsyncStorage">设置缓存</button></view><view class="remove"><text>key: </text><input type="text" v-model="removeAsyncKey"/><text style="visibility: hidden;">value: </text><input type="text" style="visibility: hidden;"/><button @click="removeAsyncStorage">清除缓存</button></view><view class="get"><text>key: </text><input type="text" v-model="getAsyncKey"/><text>value: </text><input type="text" disabled="false" style="border-style: none;" v-model="getAsyncValue"/><button @click="getAsyncStorage">获取缓存</button></view><view class="getAll"><view class=""><button @click="getAsyncAllStorage">所有缓存</button><button type="warn" @click="clearAsyncAllStorage">清空缓存</button></view><textarea name="" id="" cols="30" rows="6" disabled="false" v-model="computeAllAsyncKeyValue"></textarea></view></view><view class="syncStorageBox"><view class="title"><text>同步缓存</text></view><view class="set"><text>key: </text><input type="text" v-model="setSyncKey"/><text>value: </text><input type="text" v-model="setSyncValue"/><button @click="setSyncStorage">设置缓存</button></view><view class="remove"><text>key: </text><input type="text" v-model="removeSyncKey"/><text style="visibility: hidden;">value: </text><input type="text" style="visibility: hidden;"/><button @click="removeSyncStorage">清除缓存</button></view><view class="get"><text>key: </text><input type="text" v-model="getSyncKey" /><text>value: </text><input type="text" disabled="false" style="border-style: none;" v-model="getSyncValue"/><button @click="getSyncStorage">获取缓存</button></view><view class="getAll"><view class=""><button @click="getSyncAllStorage">所有缓存</button><button @click="clearSyncAllStorage" type="warn">清空缓存</button></view><textarea name="" id="" cols="30" rows="6" disabled="false" v-model="computeAllSyncKeyValue"></textarea></view></view></view>
</template>
​
<script setup>import {} from '@dcloudio/uni-app'import { computed, ref } from 'vue';//  异步缓存数据const setAsyncKey = ref('')const setAsyncValue = ref('')const removeAsyncKey = ref('')const getAsyncKey = ref('')const getAsyncValue = ref('')const allAsyncKeyValue = ref({})const computeAllAsyncKeyValue = computed(() => JSON.stringify(allAsyncKeyValue.value))/*** 异步缓存key、value*/function setAsyncStorage() {uni.setStorage({key: setAsyncKey.value,data: setAsyncValue.value})}/*** 异步获取数据*/function getAsyncStorage() {uni.getStorage({key: getAsyncKey.value,success: (storage) => {getAsyncValue.value = storage.data}})}/*** 异步清除缓存*/function removeAsyncStorage() {uni.removeStorage({key: removeAsyncKey.value})}/*** 异步清空所有缓存*/function clearAsyncAllStorage() {uni.clearStorage()}/*** 异步查询出所有缓存*/function getAsyncAllStorage() {uni.getStorageInfo({success: (res) => {let allStorageKeys = res.keysallAsyncKeyValue.value = {}for (let k of allStorageKeys) {uni.getStorage({key: k,success: (storage) => {allAsyncKeyValue.value[k] = storage.data}})}}})}//  同步缓存数据const setSyncKey = ref('')const setSyncValue = ref('')const removeSyncKey = ref('')const getSyncKey = ref('')const getSyncValue = ref('')const allSyncKeyValue = ref({})const computeAllSyncKeyValue = computed(() => JSON.stringify(allSyncKeyValue.value))/*** 同步缓存key、value*/function setSyncStorage() {uni.setStorageSync(setSyncKey.value, setSyncValue.value)}/*** 同步获取数据*/function getSyncStorage() {getSyncValue.value = uni.getStorageSync(getSyncKey.value)}/*** 同步清除缓存*/function removeSyncStorage() {uni.removeStorageSync(removeSyncKey.value)}/*** 同步清空所有缓存*/function clearSyncAllStorage() {uni.clearStorageSync()}/*** 同步查询出所有缓存*/function getSyncAllStorage() {let res = uni.getStorageInfoSync()console.log(res)let allStorageKeys = res.keysallSyncKeyValue.value = {}for (let k of allStorageKeys) {allSyncKeyValue.value[k] = uni.getStorageSync(k)}}</script>
​
<style lang="scss">.root {display: flex;flex-direction: column;.asyncStorageBox{display: flex;flex-direction: column;border: 1px solid black;margin-bottom: 20rpx;}.syncStorageBox{display: flex;flex-direction: column;border: 1px solid black;}.title {text-align: center;font-weight: bold;}.set {display: flex;flex-direction: row;input {margin-left: 20rpx;width: 150rpx;padding-left: 10rpx;border: 1px dotted #aaa;}button {height: 70rpx;line-height: 70rpx;margin-top: -10rpx;}margin: 30rpx 0;}.getAll{display: flex;margin-bottom: 20rpx;textarea {border: 1px solid black;width: 60%;margin-left: 50rpx;}button {height: 100rpx;margin-bottom: 50rpx;}}.get {display: flex;flex-direction: row;input {margin-left: 20rpx;width: 150rpx;padding-left: 10rpx;border: 1px dotted #aaa;}button {height: 70rpx;line-height: 70rpx;margin-top: -10rpx;}margin: 30rpx 0;}.remove {display: flex;flex-direction: row;input {margin-left: 20rpx;width: 150rpx;padding-left: 10rpx;border: 1px dotted #aaa;}button {height: 70rpx;line-height: 70rpx;margin-top: -10rpx;}margin: 30rpx 0;}}
</style>

相关文章:

  • C#源码安装ZedGraph组件,并且立即演示使用
  • GIS在构建虚拟世界中的新机遇
  • 滚雪球学MySQL[1.1讲]:MySQL简介与环境配置
  • el-upload自定上传列表删除,上传列表已删除,提交数据仍存在问题
  • 什么情况?上交所服务器被你们给买崩了?
  • 将Mixamo的模型和动画导入UE5
  • Android OpenGLES2.0开发(三):绘制一个三角形
  • 全方位助力“生活家”丨约克VRF中央空调UDIII舒享系列引领美好生活新潮流
  • Leetcode面试经典150题-39.组合总数进阶:40.组合总和II
  • 【OpenCV】 Python 图像处理 入门
  • vscode 顶部 Command Center,minimap
  • php中根据指定日期获取所在天,周,月,年的开始日期与结束日期
  • C# ReoGrid使用记录
  • 阿里云服务器操作系统 Alibaba Cloud Linux 全新升级,核心场景性能提升超 20%
  • 学习react小记
  • 【刷算法】从上往下打印二叉树
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • AHK 中 = 和 == 等比较运算符的用法
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • CAP理论的例子讲解
  • FastReport在线报表设计器工作原理
  • iOS | NSProxy
  • JavaScript 基本功--面试宝典
  • java中具有继承关系的类及其对象初始化顺序
  • oldjun 检测网站的经验
  • Spring Cloud Feign的两种使用姿势
  • Swoft 源码剖析 - 代码自动更新机制
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • Transformer-XL: Unleashing the Potential of Attention Models
  • ⭐ Unity + OpenCV 实现实时图像识别与叠加效果
  • Yii源码解读-服务定位器(Service Locator)
  • 闭包,sync使用细节
  • 从setTimeout-setInterval看JS线程
  • 分布式熔断降级平台aegis
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 问题之ssh中Host key verification failed的解决
  • 详解移动APP与web APP的区别
  • 新版博客前端前瞻
  • 优秀架构师必须掌握的架构思维
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 如何正确理解,内页权重高于首页?
  • ​HTTP与HTTPS:网络通信的安全卫士
  • # 详解 JS 中的事件循环、宏/微任务、Primise对象、定时器函数,以及其在工作中的应用和注意事项
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #pragam once 和 #ifndef 预编译头
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (9)目标检测_SSD的原理
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (二)JAVA使用POI操作excel
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (四)图像的%2线性拉伸