Home  >  Article  >  WeChat Applet  >  Introduction to WeChat Development (9) Local Cache

Introduction to WeChat Development (9) Local Cache

零下一度
零下一度Original
2017-05-24 09:55:261860browse

Currently, WeChat provides 10M of local cache space for each mini program (oh my god, it’s so big)

  1. 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:

  • ##OperationAsynchronous methodSynchronous methodInsertwx.setStoragewx.setStorageSyncReadwx.getStoragewx.getStorageSyncRemovewx.removeStoragewx.removeStorageSyncClearwx.clearStoragewx.clearStorageSync##Get cache informationAll methods ending with Sync are synchronization methods. The difference between synchronous methods and asynchronous methods is:
    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:


    Introduction to WeChat Development (9) Local CacheIt 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:


    Introduction to WeChat Development (9) Local CacheIt 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

    API

    Provides 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.
    1. 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.
    2. 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.
    3. 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.
    4. Currently it can be inferred that under real machine
    debugging

    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

    2.

    小 Pigcms (PigCms) micro-e-commerce System operation version (independent Weidian mall + three-level distribution system)

    3.

    WeChat People Network v3.4.5 Advanced Commercial Edition WeChat Rubik’s Cube Source Code

    The 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!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn