Home >WeChat Applet >WeChat Development >Introduction to WeChat Development (9) Local Cache
Currently, WeChat provides 10M of local cache space for each mini program (oh my god, it’s so big)
With local cache, your mini program can do:
Offline application (tested to operate cached data without network)
Smooth user experience
Reduce network requests and save server resources
Which data is suitable for side cache:
Hot data
Static data (user data, server authorization ID, etc.)
Network address (network address of pictures, files, etc.)
Paging list data and detailed content
General cache systems use key-value pairs to complete data insertion and reading. By performing a Hash algorithm on key, a unique value is obtained and bound to the value; when queries, the algorithm space is queried based on the hashed key Complexity O(1);
The implementation of the local cache of the mini program is based on the above method. However, whether the data is stored in ROM or stored in RAM for persistence remains to be studied.
There are two types of local cache data operations: synchronous and asynchronous. The synchronization method has a success callback function , which indicates the operation after successful data processing. The following is the local cache operation interface provided by the mini program:
Asynchronous method | Synchronous method | |
---|---|---|
wx.setStorage | wx.setStorageSync | |
wx.getStorage | wx.getStorageSync | |
wx.removeStorage | wx.removeStorageSync | |
wx.clearStorage | wx.clearStorageSync | |
wx.getStorageInfo | wx.getStorageInfoSync |
1. Call the asynchronous method first, and then call the synchronous method
Page({save: function(e){console.log('开始保存')wx.setStorage({ key: 'key1', data: 'data1', success: function(res){ console.log('异步保存成功') }})wx.setStorageSync('key2', 'data2')console.log('同步保存成功') }})
Execution results:
It can be seen that the synchronous method is successfully saved before the asynchronous method, indicating that the asynchronous method does not block the current task.
1. First call the synchronous method, then call the asynchronous method
Page({save: function(e){console.log('开始保存')wx.setStorageSync('key2', 'data2')console.log('同步保存成功')wx.setStorage({ key: 'key1', data: 'data1', success: function(res){ console.log('异步保存成功') }}) }})
Execution result:
It can be seen that the asynchronous method can only wait until the synchronous method is executed It will be executed only if it succeeds.
Cache
APIProvides an interface wx.getStorageInfo for obtaining local cache information. With it, developers can re-encapsulate existing APIs, such as adding cache time and inserting without overwriting. , batch deletion, determining the current cache size, etc. The last one is the issue of cache isolation level:
conditions, each mini program QR code scanning user will be allocated a 10M local cache. The above are the results obtained by personal testing on real machines. The results may not be completely accurate and are for reference only.
【Related recommendations】
1.
WeChat public account platform source code download小 Pigcms (PigCms) micro-e-commerce System operation version (independent Weidian mall + three-level distribution system) WeChat People Network v3.4.5 Advanced Commercial Edition WeChat Rubik’s Cube Source CodeThe above is the detailed content of Introduction to WeChat Development (9) Local Cache. For more information, please follow other related articles on the PHP Chinese website!