Home  >  Article  >  Web Front-end  >  How to set cache time in uniapp

How to set cache time in uniapp

PHPz
PHPzOriginal
2023-04-20 13:54:222043browse

In the modern Internet era, application caching is ubiquitous. In order to improve user experience and reduce server load, caching technology is necessary. As a cross-platform framework, UniApp is compatible with multiple mobile platforms and naturally supports application caching. In UniApp, setting the relevant configuration of cache time has become necessary knowledge. This article will introduce in detail how to set the UniApp cache time.

  1. The concept and significance of cache time

In browser caching technology, cache time generally refers to local cache and proxy server cache. Local caching stores the resources responded by the server in the browser, and obtains these resources directly from the cache the next time you visit the same page, avoiding network requests and server load. Proxy server caching caches a copy of the request issued by the client in the proxy server. The proxy server directly responds to the same request next time, which also reduces the pressure on the server. In UniApp, the application cache works similarly. When the application starts, the cache resources will be read from the local or server cache. On the one hand, it ensures that the application is updated in near real-time, and on the other hand, it reduces the burden on the server. .

  1. How to set the cache time in UniApp

Setting the cache time in UniApp requires two steps:

(1) Add it to the manifest.json file Cache related configuration.

"networkTimeout": {
  "request": 30000,
  "downloadFile": 60000,
  "connectSocket": 60000,
  "uploadFile": 60000
},
"applets": {
  "network": {
    "cache": {
      "networkTimeout": 300000,
      "maxAge": 86400000
    }
  }
},

Among them, the networkTimeout configuration item specifies the HTTP request timeout, in milliseconds. The applets and network cache configuration items are provided by UniApp and are used to cache the network request returns of the application.

(2) Set the page-level cache time in the vue file.

The Vue component has life cycle functions created() and mounted(). In black technology, we can also use onPullDownRefresh() or onReachBottom(). Taking created() as an example, the following is a simple code example:

created () {
  uni.setStorageSync('mymodule', _this.module)//设置缓存,缓存名为mymodule,值为_module
},

In this way, when the page is opened, the cache will be read to ensure that the page does not expire within the specified time.

  1. Application Scenarios

The UniApp cache time setting does not apply to all application scenarios. Usually suitable for applications where data updates are infrequent, such as menus, personal information, etc., which will not cause major data changes after being cached. Only in these scenarios, cache time will bring a good experience and reduce the load on the server.

  1. Summary

Through the introduction of UniApp cache time, I believe you have a deeper understanding of it. UniApp's cache time settings need to take into account various factors such as application scenarios, network environments, cache strategies, etc. Only when there are clear requirements can you set the cache time to avoid unnecessary problems. Finally, using the configuration method described in this article to set the cache time can effectively improve the user experience and reduce the load on the server.

The above is the detailed content of How to set cache time in uniapp. 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