Android Client cache mechanism (text cache and multimedia file cache)

Source: Internet
Author: User

Android Client cache mechanism (text cache and multimedia file cache)

The client-side caching mechanism is an important part of Android application development, and using a caching mechanism not only saves 3G of traffic to users, but is also a great choice in terms of user experience.

The caching mechanism is divided into two parts, a text cache, and a multimedia file cache in part.

Cache File Save Policy:

1. Text cache:

Text caching should be divided into two categories, one of which is frequently updated, and one that is less frequently updated.

The expiration time is differentiated based on the update frequency of the two.

Update the area that is more frequent, and its cache expiration time should be within the application (that is, when the application is opened from the time it is closed). In this case, there will be a dedicated cache folder for this type of cache file, as well as a dedicated cache database table storage information. This type of data is emptied every time the application starts.

Another area that is rarely updated, its cache does not set the expiration time, but instead provides a button or menu that lets the user choose to manually update (such as my friends list, my subscriptions, my share.)

(1) Normally, we interact with the server in JSON format to get the data, the JSON data obtained is just a string, we can consider to write these strings using a file stream txt, save to

SD card, add a record of the data to the database. When you add a database record, you provide two key fields, and one is

The URL of the request, and the other is the file address that was saved locally. Each subsequent request to the server is retrieved in the database based on the URL.

This method benefits: Reduce the system Database data volume, does not appear the database full phenomenon.

Cons: Frequent IO operations, sometimes incorrect code writing can cause memory leaks and other conditions.

(2) After parsing the JSON data into the List<map> object, and then traversing the list, the data is written to the corresponding database table structure, which can be retrieved in the database before each request to the server, if there is a direct return.

This method benefits: if there is a local text cache, the read cache is faster.

Disadvantage: Increase the amount of database data.

2. multimedia file Caching

(1) Image cache

The cache of the image can be cached to the specified image cache directory on the SD card based on the current date and time.

At the same time the database to do the corresponding records, the record method can be controlled by two key fields, one field is the URL address of the picture, and the other field is the local address of the picture. When the image is retrieved according to the URL in the data, if not the connection server download, download and then make a corresponding record in the server.

(2) Video file cache

Considering that the video file is animated, each episode should be more than 20MB in size, if it is downloaded while playing

The user's 3G traffic burden is very big, and the user finishes watching a set of cartoons, basically won't go to see the second time again.

From this point of view, it is recommended not to download the video file cache. It can also reduce the programmer's coding burden.

Of course, we can provide some anime enthusiasts with a download cartoon button for them to download favorite cartoons, and even can set if you want to download animation will pay some price and so on.

Cache file Deletion policy:

1. Each module deletes the corresponding module's cache file each time the client automatically or the user manually updates, and re-downloads the new cache file.

2. In the settings interface to provide the ability to delete the cache, click to delete the native all cache.

The local client-server interaction flowchart is as follows:


Android Client cache mechanism (text cache and multimedia file 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.