Android Client Cache Mechanism (Text cache and multimedia File Cache)

Source: Internet
Author: User

Overview:

The client cache mechanism is a very important task in Android Application Development. Using the cache mechanism can not only save 3G traffic for users, but also provide a good choice for user experience.

The cache mechanism is divided into two parts: Text cache and multimedia file cache.

Cache file storage policy:

1. Text cache:

The text cache can be divided into two types: frequent updates and infrequent updates.

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

When a region is updated frequently, its cache expiration time should be within the application (that is, the period from when the application is opened to when it is closed ). in this case, there will be a special cache folder to store this type of cache files, and a special cache database table to store information. this type of data is first cleared when an application is started.

In another region with few updates, the cache does not set the expiration time, but provides a button or menu for users to choose to manually update (such as my friend list, my subscription, my shares. wait ....)

For details about the text cache, see Appendix 1.

(1) normally, we interact with the server to obtain data in JSON format. The obtained JSON data is only a string. We can consider writing these strings into a TXT file stream, save

In the SD card, when the database adds the record of the data, two key fields are provided, one is

The requested URL, and the other is the locally saved file address. In the future, each request to the server will be retrieved Based on the URL in the database.

Advantages of this method: reduce the data volume of the system database, and the database will not be full.

Disadvantage: frequent I/O operations, misunderstanding of code writing, and memory leakage.

(2) Parse JSON data, load it into the list <map> object, traverse the list, and write all the data into the corresponding database table structure, you can search the database before each request is sent to the server.

Advantages of this method: if the local text cache exists, the read speed is faster.

Disadvantage: Increase the database data volume.

2. Multimedia File Cache

(1) image Cache

The image cache 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 corresponding records in the database can be recorded using two key fields, one is the URL address of the image, and the other is the local address of the image. the image is retrieved Based on the URL when the image is retrieved. If no image is retrieved, the image is downloaded from the server and recorded on the server.

(2) Video File Cache

Considering that video files are cartoons, the size of each set should exceed 20 mb.

The user's 3G traffic burden is huge, and after watching a set of cartoons, the user will basically not go to the second time.

In this case, we recommend that you do not download or cache video files. This also reduces the coding workload of programmers.

Of course, we can provide some animation enthusiasts with a button to download cartoons for them to download their favorite cartoons, or even set the cost if they want to download cartoons.

Cache file deletion policy:

1. Each module deletes the cache file of the corresponding module every time the client automatically or the user updates it manually, and downloads the new cache file again.

2. On the settings page, you can delete the cache and click it to delete all the local caches.

The interaction between the local client and the server is as follows:

 

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.