Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to cache data in WeChat Mini Programs

Shulou Source: shulou.com Published: 2022-05-31 13:33:24 09月28日 Update

This article mainly introduces the relevant knowledge of "how to cache data in WeChat Mini Programs". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to cache data in WeChat Mini Programs" can help you solve the problem.

WeChat Mini Programs can set, get and clean the local cache through wx.setStorage (wx.setStorageSync), wx.getStorage (wx.getStorageSync) and wx.clearStorage (wx.clearStorageSync). The maximum local cache is 10MB.

Wx.setStorage ()-set cache asynchronously

Official attributes given by Wechat

OBJECT parameter description:

Parameter type required indicates that keyString is the specified keydataObject/String in the local cache. SuccessFunction No API calls successful callback function failFunction No API calls failed callback function completeFunction No API call ends callback function (both successful and failed calls will be executed)

Call method:

Wx.setStorage ({key: "key", data: "value"})

Wx.setStorageSync ()-synchronizes the settings cache

Official attributes given by Wechat

Parameter description:

The parameter type is required to indicate that keyString is the specified keydataObject/String in the local cache and the content to be stored

Call method:

Try {wx.setStorageSync ('key',' value')} catch (e) {}

Wx.getStorage ()-get cache asynchronously

Official attributes given by Wechat

OBJECT parameter description:

The parameter type is required to indicate that keyString is the specified keysuccessFunction in the local cache. Res = {data: key corresponding to content} failFunction No API call failed callback function completeFunction No API call end callback function (both successful and failed calls will be executed)

Success return parameter description:

Parameter type describes the content corresponding to dataStringkey

Call method:

Wx.getStorage ({key: 'key', success: function (res) {console.log (res.data)}})

Wx.getStorageSync ()-get cached data synchronously

The official attribute description given by Wechat

Parameter description:

Parameter type is required indicating that keyString is the specified key in the local cache

Call method:

Try {var value = wx.getStorageSync ('key') if (value) {/ / Do something with return value}} catch (e) {/ / Do something when catch error}

Wx.getStorageInfo ()-get the currently cached data asynchronously

The official attribute description given by Wechat

OBJECT parameter description:

The parameter type is required to indicate that successFunction is the callback function called by the API. For more information, please see the returned parameters indicating whether the API failed callback function failFunction. CompleteFunction No callback function at the end of API call (both successful and failed calls will be executed)

Success return parameter description:

The parameter type indicates the amount of space currently occupied by all keycurrentSizeNumber in keysString Array's current storage, the space limited by unit kblimitSizeNumber, and the unit kb.

Call method:

Wx.getStorageInfo ({success: function (res) {console.log (res.keys) console.log (res.currentSize) console.log (res.limitSize)})

Wx.getStorageInfoSync ()-get the current cache data synchronously

Emmmmm--- Wechat did not specify the parameters.

Call method:

Try {var res = wx.getStorageInfoSync () console.log (res.keys) console.log (res.currentSize) console.log (res.limitSize)} catch (e) {/ / Do something when catch error}

Wx.removeStorage ()-asynchronously removes cached data from the specified key

Description of official parameters of Wechat

OBJECT parameter description:

Parameter type required indicates that keyString is specified in the local cache. KeysuccessFunction is the callback function called by the API failFunction No callback function failed by API call completeFunction No callback function at the end of API call (both successful and failed calls will be executed)

Call method:

Wx.removeStorage ({key: 'key', success: function (res) {console.log (res.data)}})

Wx.removeStorageSync ()-removes cached data from the specified key synchronously

Description of official parameters of Wechat

Parameter description:

Parameter type is required indicating that keyString is the specified key in the local cache

Call method:

Try {wx.removeStorageSync ('key')} catch (e) {/ / Do something when catch error}

Wx.clearStorage ()-asynchronously cleans up the local cache

Call method:

Wx.clearStorage ()

Wx.clearStorageSync ()-synchronously cleans the local cache

Call method:

Try {wx.clearStorageSync ()} catch (e) {/ / Do something when catch error} about "how to cache data in WeChat Mini Programs", thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Parameters caches functions interfaces methods types data official success content properties synchronization programs knowledge units size space industry storage different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB MySQL Apple macOS