Android data Cache

Source: Internet
Author: User

The use of cache, can further greatly alleviate the pressure of data interaction, but also to provide a certain offline browsing. Below is a brief list of the applicable environments for cache management:

1. Application of network Services

2. Data updates do not need to be updated in real time, and even a 3-5-minute delay can be implemented with a caching mechanism.

3. Cache expiration Time is acceptable (similar to NetEase's news reading, support offline offline reading)

The benefits of this are:

1. Reduce the pressure on the server

2. Improve client response speed (local data extraction)

3. To a certain extent, support offline browsing (can refer to NetEase's news application, the personal feel that offline reading is very good. )

First, the method of cache management

The principle of cache management is simple: the time to determine whether to read the cache or re-download; there is nothing to say in the network, go directly to the cache.

Second, the database (SQLite) cache mode

This method is after downloading the data file, the relevant information of the file such as URL, route, download time, expiration time and so on to the database, of course, I personally recommend the URL as a unique identifier. The next time you download the query from the database according to the URL, if the query to the current time has not expired, according to the path to read local files, so as to achieve the effect of caching.

From the implementation we can see that this method can flexibly store the properties of the file, which provides a lot of extensibility, can provide some support for other functions.

From the operation needs to create a database, each query database, if the expiration also need to update the database, clean the cache also need to delete the database data, a little trouble, and the database operation is not easy to appear a series of performance, ANR problem, pointer error problem, the implementation of the time to be cautious, concrete, But it's just a matter of adding a tool class or method.

There is also a problem, the cached database is stored in the/data/data/<package>/databases/directory, is occupied memory space, if the cache accumulated, easy to waste memory, need to clean up the cache in a timely manner.

Third, the file cache mode

This method uses the File.lastmodified () method to get the last modification time of the file, and the current time to determine whether it expires, thereby achieving the cache effect.

Only this property can be used on the implementation, and there is no possibility of providing technical support for other features. The operation is simple, compare time can, and take the data is the JSON data in the file. itself is not easy to deal with other problems, the cost is low.

Iv. Two-point description of the method of file caching

1. The cache time for different types of files is not the same.

Generally speaking, the cache time of the unchanging file is permanent, and the cache time of the change file is the maximum tolerable constant time. White point, the picture file content is unchanging, usually exists on the SD card until it is cleaned, we can always read the cache. The contents of the configuration file are likely to be updated, and an acceptable cache time needs to be set.

2. The cache time standard in different environments is not the same.

No network environment, we can only read the cache file, in order to apply something to show, there is no expiration of the said.

WiFi network environment, the cache time can be set short, one is the speed faster, but not the flow of money.

3G traffic environment, cache time can be set a bit longer, save traffic, is to save money, but also better user experience.

GPS doesn't say updates or anything, it's slow enough. How long the cache time can be.

Of course, as a good application, will not die a situation, for different networks to transform different forms of caching function is necessary. And this time according to their own actual situation to set: Data update frequency, the importance of data and so on.

Five, when to refresh

Developers want to try to read the cache as far as possible, the user on the one hand want to refresh in real time, but the faster the better, the less traffic consumption the better (about this piece, it is true that I did not think of the development, after all, the interface is so much, now the company's products almost click on the visit, but also some of the Slowly modify haha), is a contradiction.

In fact when the refresh I do not know, here I provide two points of advice:

1. The longest and longest duration of the data is unchanged and has no significant impact on the application.

For example, your data update time is 4 hours, then the cache time is set to 1-2 hours more appropriate. That is, update time/cache time = 2, but the user's personal changes, site editors and other people to update the others. One day users will always see the update, even if there is a delay, depending on the purpose of your product; If you think you are an information application, then reduce, 2-4 hours, if you think the data is more important or more popular, users often play, then reduce, 1-2 hours, and so on.

2. Provide a refresh button.

The necessary or safest way to provide a refresh button in the relevant interface, or the current popular drop-down list refresh mode. As a cache, providing a chance for the load to fail once again.

We have fully considered several details from the implementation, the note has been explained, no longer repeat.
We then invoke the following method:
void GetConfig () {        //First attempts to read the cache          string cacheconfigstring = Configcache.geturlcache (config_url);         //based on the result to determine whether to read the cache or reread         if (cacheconfigstring! = null) {             showconfig (cacheConfigString);         } else {             //if the cached result is empty, it means that a reload is required             // The cache is empty because it could be 1. no cache; 2. Cache expires; 3. Error reading cache             asynchttpclient client = new Asynchttpclient ();             client.get (CONFIG_ URL, New Asynchttpresponsehandler () {                  @Override                  public void onsuccess (String result) {                     //successfully downloaded, save to local cache file as back                      configcache.seturlcache (result,  config_url);                     //behind can be UI update, for reference only                       Showconfig (Result);                 }                 @ Override                public void OnFailure (Throwable arg0) {                     &NBSP;//based on failure reasons, consider whether to show load failure or read cache again                  }             });         }    }


Android data Cache

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.