小程式的緩存資料是以應用為次元。不同ID的小程式間緩存資料是互相隔離、不受影響的。預設情況下,開發者無需關心緩存次元的問題。
當開發者在開發第三方企業應用且希望使用本地緩存臨時儲存使用者企業相關的一些資訊時,則需要注意緩存的這個特性。(使用者可以存在于多個組織,在不同的組織間可以随意切換,多個組織可能會開通同一個第三方企業應用。這種情況下,從應用的次元,同一個使用者在不同的時間可能具備不同的企業身份,即dd.corpId不同)。
dd.setStorage
将資料存儲在本地緩存中指定的 key 中,會覆寫掉原來該 key 對應的資料。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 緩存資料的key |
data | Object/String | 是 | 要緩存的資料 |
success | Function | 否 | 調用成功的回調函數 |
fail | Function | 否 | 調用失敗的回調函數 |
complete | Function | 否 | 調用結束的回調函數(調用成功、失敗都會執行 |
dd.setStorage({
key: 'currentCity',
data: {
cityName: '杭州',
adCode: '330100',
spell: ' hangzhou',
},
success: function() {
dd.alert({content: '寫入成功'});
}
});
dd.setStorageSync
同步将資料存儲在本地緩存中指定的 key 中。
同步資料IO操作可能會影響小程式流暢度,建議使用異步接口,或謹慎處理調用異常。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 緩存資料的key |
data | Object/String | 是 | 要緩存的資料 |
dd.setStorageSync({
key: 'currentCity',
data: {
cityName: '杭州',
adCode: '330100',
spell: ' hangzhou',
}
});
dd.getStorage
擷取緩存資料。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 緩存資料的key |
success | Function | 否 | 調用成功的回調函數 |
fail | Function | 否 | 調用失敗的回調函數 |
complete | Function | 否 | 調用結束的回調函數(調用成功、失敗都會執行 |
success傳回值
名稱 | 類型 | 說明 |
---|---|---|
data | Object/String | key對應的内容(不存在時傳回 null) |
dd.getStorage({
key: 'currentCity',
success: function(res) {
dd.alert({content: '擷取成功:' + res.data.cityName});
},
fail: function(res){
dd.alert({content: res.errorMessage});
}
});
dd.getStorageSync
同步擷取緩存資料。
同步資料IO操作可能會影響小程式流暢度,建議使用異步接口,或謹慎處理調用異常。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
key | String | 是 | 緩存資料的key |
傳回值
名稱 | 類型 | 說明 |
---|---|---|
data | Object/String | key對應的内容(不存在時傳回 null) |
let res = dd.getStorageSync({ key: currentCity });
dd.alert({
content: JSON.stringify(res.data),
});
更多(删除緩存)。。。
轉載至https://ding-doc.dingtalk.com/doc#/dev/storage