Android data cache

Source: Internet
Author: User

Android data cache

Caching can further relieve the Data Interaction pressure and provide offline browsing. The following briefly describes the application environment of Cache Management:

1. applications that provide Network Services

2. Data Update does not need to be updated in real time. A cache mechanism can be used even if the delay is 3-5 minutes.

3. the cache expiration time is acceptable (similar to Netease's news reading and supports offline reading)

Benefits:

1. reduce the pressure on the server

2. Improve the client response speed (local data extraction)

3. support offline browsing to a certain extent (you can refer to Netease's news application. I personally feel that offline reading is very good .)

 

I. Cache Management Methods

The principle of Cache Management is very simple: you can set the time to determine whether to read the cache or re-download it. There is nothing to say when the network is disconnected, just go to the cache directly.

Ii. Database (SQLite) caching Method

After downloading the data file, store the file-related information such as url, path, download time, and expiration time in the database, of course, I personally suggest using a url as a unique identifier. During the next download, the local file is first queried from the database based on the url. If the current time does not expire, the local file is read Based on the path to achieve the cache effect.

From the implementation, we can see that this method can flexibly store the attributes of files, which provides great scalability and support for other functions.

You need to create a database from the operation, and query the database each time. If the database needs to be updated after expiration, you also need to delete the database data when clearing the cache, which is a little troublesome, improper database operations are prone to a series of performance, ANR problems, pointer errors, and implementation with caution. For specific operations, it is only a matter of adding a tool class or method.

Another problem is that the cached database is stored in/data/ The/databases/directory occupies memory space. If the cache is accumulated, it is easy to waste memory and the cache needs to be cleared in time.

 

Iii. File Cache

In this way, use the File. lastModified () method to get the last modification time of the File, and determine whether the File expires with the current time to achieve the cache effect.

This attribute can only be used for implementation, without providing technical support for other functions. The operation is simple, and the time can be compared, and the data obtained is the JSON data in the file. It is not easy to solve other problems, and the cost is low.

 

Iv. Two instructions on File Cache Methods

1. Different types of files have different cache times.

In general, the cache time for unchanged files is permanent, and the cache time for changing files is the maximum tolerable duration. To put it bluntly, the content of the image file remains unchanged. Normally, it is stored on the SD card until it is cleared. We can always read the cache. The configuration file content may be updated and an acceptable cache time needs to be set.

2. the cache time standards are different in different environments.

In the absence of a network environment, we can only read cached files. For the sake of application display, there is nothing to say about expiration.

In a Wi-Fi network, the cache time can be set to a little shorter. First, the network speed is fast, but the traffic is not money.

In a 3G traffic environment, you can set a longer cache time to save traffic, which means saving money and better user experience.

GPS is slow enough. The cache duration can be long.

Of course, as a good application, it will not be fixed. It is necessary to change different forms of cache functions for different networks. In addition, this time is set based on your actual situation: data update frequency, data importance, and so on.

 

5. When to refresh

On the one hand, developers want to read the cache as much as possible. On the one hand, users want to refresh in real time, but the faster the response speed, the better the traffic consumption. (I did not think about it during development, after all, there are so many interfaces. Now the company's products can be accessed almost at once, and there are also some redundant features. It's a conflict.

I don't know when to refresh it. Here I provide two suggestions:

1. the maximum length of data remains unchanged, which has no significant impact on applications.

For example, if your data is updated for 4 hours, the cache time is set to 1 ~ 2 hours is suitable. That is, the update time/cache time is 2, but some Manual updates, such as personal modifications and website editors, are also described. One day users will always see updates, even if there is latency, depending on the purpose of your product; if you think you are an information application, then decrease, 2 ~ If you think the data is important or popular for 4 hours, users will often play and reduce it ~ 2 hours, and so on.

2. the refresh button is provided.

If necessary, or in the safest way, provide a refresh button on the relevant interface, or use the pull-down list refresh method that is currently popular. It is a cache and provides a re-access opportunity for loading failure.

 

 

In terms of implementation, we have fully considered several details. The comments have been described and will not be repeated.
The call method is as follows:
Void getConfig () {// first try to read the cache String cacheConfigString = ConfigCache. getUrlCache (CONFIG_URL); // determine whether to read the cache or re-read if (cacheConfigString! = Null) {showConfig (cacheConfigString);} else {// If the cache result is empty, it indicates that the reason for re-loading // The cache to be empty may be 1. no cache; 2. cache expiration; 3. an error occurred while reading the cache. AsyncHttpClient client = new AsyncHttpClient (); client. get (CONFIG_URL, new AsyncHttpResponseHandler () {@ Override public void onSuccess (String result) {// after successful download, save it to the local directory as the cache file ConfigCache. setUrlCache (result, CONFIG_URL); // UI updates can be followed by showConfig (result);} @ Override public void onFailure (Throwable arg0) {// Based on the cause of failure, whether to display loading failure or read cache again }});}}

 

 

 

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.