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 |
- The synchronous method will block the current task until the synchronous method returns.
- Asynchronous methods will not block the current task.
- 6. The following are two methods of inserting cache to illustrate the difference between synchronization and asynchronousness:
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:
- The same mobile phone, different mini-program applications, at least in terms of logical storage, are definitely not shared caches, which meet application-level isolation.
- The same mobile phone, the same applet, can be used by different WeChat users by scanning the code. After testing, the cache is not shared. Under current test conditions, it is user-level isolation.
- The same mobile phone, the same mini program, the same WeChat user scanned the code twice and used it. After testing, it is a shared cache.
- The same mobile phone, the same applet, can be switched by different WeChat users (scan the code twice respectively). After testing, the same user shares the cache, but different users do not share the cache.
- Currently it can be inferred that under real machine
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
