Android Cache:
The use of cache, can further greatly alleviate the pressure of data interaction, but also to provide a certain offline browsing. Let me briefly list the applicable environment 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.
There will be some details of the processing, will be elaborated in detail later. Based on this principle, the two most common methods of cache management currently used by individuals are: Database and file (TXT).
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.
Of course, this method from some of the practical application of the present, I did not find any problem, the estimated use of the amount is relatively small.
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.
This article refers to the URL (http://blog.csdn.net/zhuanshenweiliu/article/details/31744673).
Android Clears the local data cache code : http://www.cnblogs.com/rayray/p/3413673.html
Android Cache Processing