Implementation of Android Network cache

Source: Internet
Author: User
Tags internet cache

In the development group, a number of students asked about the Internet cache problem in Android. In fact, regardless of whether it is Android or iOS, the general idea of caching is the same, the following is a general introduction to the cache in several cases. By the way, some open-source network request frameworks have already provided support for caching. such as today's more popular okhttp. But sometimes we want to take advantage of Android's own network request API and implement our own network caching strategy. Your own implementation will be more flexible and more in line with your business logic needs.

I have roughly divided the network cache into two categories:

1, the network data refresh infrequently

In this case, the frequency of network data is not very high, and the real-time display of data requirements are not very high, such as some e-commerce platform commodity display list data. In such a case. The data is refreshed every 1 hours, so that we are not required to request data from the network every time, set a timeout value for the data, and of course the timeout value should be less than the time of the data refresh. For example, suppose the data refresh is 1 hours at a time. Then we can set the cache timeout value to 30 minutes. 10 minutes, or 5 minutes. This ensures that the data is displayed at high speed without compromising the user's access to the latest data. In such cases the business logic is as follows:

if (network available) {if (local cache && cache not expired && non-flush operation) {return to local,}else{for network request, store network data locally,//(able to use file or Sqllite) to return network data. }}else{if (Refresh operation) {indicates that the refresh operation is not available in a network state,}else{if (local cache exists) {Returns a local cache}else{returns NULL. }}}

2, the network data refresh more frequently

Assume that the network data refresh more frequently, for example, stick, say what. Can not use the above-mentioned caching mechanism, because of the real-time requirements of the data is relatively high, so the need for the caching mechanism also slightly improve the user experience, so that users have just come in when the content can be seen. The caching logic for this scenario is as follows:

if (network available) {if (local cache exists) {Local data is set first in the interface;} makes a network request. The network request data is stored locally. Wait for the network request to end with a new data refresh interface. }else{if (local cache exists) {Local data is set in the interface. No data is indicated in the}else{interface. Or the network is not available. }}

The above is the more commonly used network cache two implementation ideas. The other thing to note is the detailed technology that stores the network data locally. Can be stored using sqllite, and can also be stored using files. Suppose to use file storage proposed to establish a directory, and the corresponding hashcode code for each URL is the file name, so that the storage is relatively clear, and calculate the time-out value can be directly with the current time minus the changes in the file time can be, assuming that the Sqllite database storage, Storage should store the corresponding URL, timestamp, and content.

If there is inappropriate welcome to shoot bricks.


Implementation of the Android network cache

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.