Android uses the cache mechanism to download files

Source: Internet
Author: User

When downloading an object or browsing an object online, or to ensure the correctness of the object download, a cache mechanism is required. SoftReference is often used.

SoftReference is characterized by a soft reference of a Java object saved by an instance. The existence of this soft reference does not prevent the garbage collection thread from reclaiming the Java object. That is to say, once SoftReference saves a soft reference to a Java object, the get () provided by the SoftReference class before the garbage thread recycles this Java object () method returns a strong reference to a Java object. In addition, once the garbage thread recycles the Java object, the get () method returns null. Soft references can be used together with a ReferenceQueue. If the referenced objects are recycled by the garbage collector, the Java virtual machine adds this soft reference to the reference queue associated with it.


The general cache policy is:

Level-1 memory cache, level-2 File Cache (the database is also counted as a File Cache), and level-3 network data

I. Cache Policy for Online downloads

Basic Strategies for downloading files (images, audios, and videos) over the Internet:


1. Do not directly download the file to the target file. Use the temp file for transit to ensure the correctness and integrity of the file. The process is as follows:

A) Generate A unique local target file name B with the network target file name.

B) generate a unique local temporary file name with the local target file name B. T

C) download the file to T

D. Check the correctness and integrity of file T after the download is completed.

E) if it is incorrect or incomplete, delete the file T and return false.

F) after the verification is completed, rename or copy the file T to file B.

G) The final cleaning site, deleting the temporary file T, returns true after successful


2. Verify file correctness and integrity as much as possible:

A) Correctness: for example, MD5/Hash Code comparison and file format comparison.

B) Integrity: for example, whether the file size is consistent and the image data is correct (related information is provided in the image file header)
3. Consider whether secondary processing is required for downloading a local file. Consider the following:

A) For example, if the size of the image starting from the network source is 800*600, and the size of the thumbnail is 160*145, You need to crop the downloaded file and save it, delete the source file directly.

Ii. File Cache Policy:
1. The corresponding I/O key of the unique cache file is required. Generally, hashcode can be used.
2. If the same file takes different time, consider clearing the local cache first and then downloading the new cache to the local.
3. You can add a timestamp to a file to generate a unique hashcode.
4. During file generation, you may need to take the following comprehensive considerations:

A) is there no space for sdcard (this requirement exists, but almost no one will consider it. Once it happens, it will be crash ).

B) cache cleaning policy. Daily and weekly cleaning? After a threshold value is reached, will it be automatically cleared? (If there is no cleanup policy, it is very sb to keep junk data as a treasure ).

C) cache the actually needed data. Do not think that external storage is infinite, so you can store everything. You need to know that many things are complex and complex. A colleague once stored several hundred MB of user data (gender, age, and contact information of all users) every day, while PM only needed an active data report for several users every day, therefore, the daily user analysis report data can be cached (only 10 KB ).

D) encrypt the cache file. The simplest is to remove the file extension, which is also encrypted. Of course, you can encrypt the Server File and then decrypt it in the memory. This depends on the needs of the project, and my experience is insufficient. It is generally used to change the extension.

Note:For more information about softreference, seeHttp://blog.csdn.net/kavendb/article/details/5935577

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.