Android cache details, android details

Source: Internet
Author: User

Android cache details, android details

Android cache:

Caching can further relieve the Data Interaction pressure and provide offline browsing. Below I will briefly list the applicable environment of Cache Management:

1. applications that provide Network Services

2. Data Update does not need to be updated in real time. A cache mechanism can be used even if the delay is 3-5 minutes.

3. the cache expiration time is acceptable (similar to Netease's news reading and supports offline reading)

Benefits:

1. reduce the pressure on the server

2. Improve the client response speed (local data extraction)

3. support offline browsing to a certain extent (you can refer to Netease's news application. I personally feel that offline reading is very good .)

 

I. Cache Management Methods

The principle of Cache Management is very simple: you can set the time to determine whether to read the cache or re-download it. There is nothing to say when the network is disconnected, just go to the cache directly.

There will be some detailed processing, which will be detailed later. Based on this principle, two common cache management methods that I have used today are: database and file (txt ).

 

Ii. Database (SQLite) caching Method

After downloading the data file, store the file-related information such as url, path, download time, and expiration time in the database, of course, I personally suggest using a url as a unique identifier. During the next download, the local file is first queried from the database based on the url. If the current time does not expire, the local file is read Based on the path to achieve the cache effect.

From the implementation, we can see that this method can flexibly store the attributes of files, which provides great scalability and support for other functions.

You need to create a database from the operation, and query the database each time. If the database needs to be updated after expiration, you also need to delete the database data when clearing the cache, which is a little troublesome, improper database operations are prone to a series of performance, ANR problems, pointer errors, and implementation with caution. For specific operations, it is only a matter of adding a tool class or method.

Another problem is that the cache database is stored in the/data/<package>/databases/directory, which occupies the memory space. If the cache is accumulated, it is easy to waste memory, cache needs to be cleared in a timely manner.

Of course, I have not found any problems in some practical applications. It is estimated that this method is used less often.

I am not very fond of databases in this article, because operations are troublesome, especially the statements for creating tables by myself, you know. I focus on file caching.

 

Iii. File Cache

In this way, use the File. lastModified () method to get the last modification time of the File, and determine whether the File expires with the current time to achieve the cache effect.

This attribute can only be used for implementation, without providing technical support for other functions. The operation is simple, and the time can be compared, and the data obtained is the JSON data in the file. It is not easy to solve other problems, and the cost is low.

 

Iv. Two instructions on File Cache Methods

1. Different types of files have different cache times.

In general, the cache time for unchanged files is permanent, and the cache time for changing files is the maximum tolerable duration. To put it bluntly, the content of the image file remains unchanged. Normally, it is stored on the SD card until it is cleared. We can always read the cache. The configuration file content may be updated and an acceptable cache time needs to be set.

2. the cache time standards are different in different environments.

In the absence of a network environment, we can only read cached files. For the sake of application display, there is nothing to say about expiration.

In a Wi-Fi network, the cache time can be set to a little shorter. First, the network speed is fast, but the traffic is not money.

In a 3G traffic environment, you can set a longer cache time to save traffic, which means saving money and better user experience.

GPS is slow enough. The cache duration can be long.

Of course, as a good application, it will not be fixed. It is necessary to change different forms of cache functions for different networks. In addition, this time is set based on your actual situation: data update frequency, data importance, and so on.

 

5. When to refresh

On the one hand, developers want to read the cache as much as possible. On the one hand, users want to refresh in real time, but the faster the response speed, the better the traffic consumption. (I did not think about it during development, after all, there are so many interfaces. Now the company's products can be accessed almost at once, and there are also some redundant features. It's a conflict.

I don't know when to refresh it. Here I provide two suggestions:

1. the maximum length of data remains unchanged, which has no significant impact on applications.

For example, if your data is updated for 4 hours, the cache time is set to 1 ~ 2 hours is suitable. That is, the update time/cache time is 2, but some Manual updates, such as personal modifications and website editors, are also described. One day users will always see updates, even if there is latency, depending on the purpose of your product; if you think you are an information application, then decrease, 2 ~ If you think the data is important or popular for 4 hours, users will often play and reduce it ~ 2 hours, and so on.

Of course, the data similar to this interface I think the update time can be as long as possible. If you take the data from the backend, it will change. I will tell you: This is just a guiding interface. There is no relationship between how many games you have with the user's half-cent payment, and the 1 billion game has nothing to do with him, he just needs to confirm that he can find the Tom cat he is looking. Otherwise, you lose another user.

2. the refresh button is provided.

If necessary, or in the safest way, provide a refresh button on the relevant interface, or use the pull-down list refresh method that is currently popular. It is a cache and provides a re-access opportunity for loading failure. After all, when I drink bone soup, I don't mind how many chopsticks are beside the bowl.

All in all, for better user experience, there will be an endless stream of methods. Looking forward to a better solution

(Reference: http://blog.csdn.net/lnb333666/article/details/8460159)

Image cache:

Translation:

Loading a Bitmap (Bitmap) to your UI is very simple, but if you want to load a large number at a time, it will become much more complicated. In most cases (components such as ListView, GridView, or ViewPager), the number of images on the screen and the total number of images that will be immediately rolled to the screen, in essence, it is unrestricted.

Components like this will recycle the view after the sub-view is removed from the screen, and the memory usage will be retained. However, if you do not retain any reference for long-term survival, the garbage collector will release the Bitmap you load. This is a good choice, but in order to maintain a smooth and fast loading UI, you must avoid re-processing when the image is returned to the screen. Memory and hard disk cache can solve this problem, and cache allows components to quickly load and process images.

This lesson will show you how to use memory and hard disk cache Bitmap to improve UI responsiveness and smoothness when loading multiple bitmaps.

Use memory cache

At the cost of valuable application memory, the memory cache provides a fast Bitmap access method. The LruCache class (which can be obtained in the Support Library and supported by API Level 4 or later, or version 1.6 or later) is very suitable for caching Bitmap tasks, it stores recently referenced objects in a strongly referenced LinkedHashMap and releases infrequently used objects after the cache size exceeds the specified size.

Note: In the past, a very popular memory cache implementation was SoftReference (soft reference) or WeakReference (weak reference) Bitmap cache solution. However, it is not recommended now. Since Android2.3 (API Level 9), the garbage collector focuses more on the collection of Soft/weak references, which makes the above solution quite ineffective. In versions earlier than Android 3.0 (API Level 11), the backup data of Bitmap is directly stored in the local memory and released from the memory in an unpredictable way, it is very likely that the program will crash if it exceeds the memory limit temporarily.

To select a proper size for LruCache, we need to consider many reasons, such:

Are other activities and/or programs very memory-consuming?

How many images will be displayed on the screen at one time? How many images will be displayed on the screen?

What is the screen size and density of a device? A device with an ultra-HD screen (xhdpi), such as Galaxy Nexus, needs a larger cache space to cache the same number of images than Nexus S (hdpi.

What is the size, configuration, and memory size of each Bitmap image?

Are images frequently accessed? Will some be accessed more frequently than others? If so, you may need to keep some images in the memory, or even allocate multiple LruCache objects to different groups of bitmaps.

Can you balance the quality and quantity of images? Sometimes it is more useful to store a large number of low-quality images, and then you can load images of another high-quality version in the background task.

There is no specification for setting the cache size for all applications. It depends on the appropriate solution provided after memory usage analysis. If the cache space is too small, there is no benefit, but it will lead to additional overhead. If it is too large, it may cause java. lang. OutOfMemory exceptions again or leave only a small space for other applications to run.


What is the cache problem in Android? Who can explain it in detail,

Computer cache refers to the temporary file exchange zone. The computer puts the most commonly used files out of the memory and puts them in the cache temporarily, just like moving tools and materials onto the workbench, in this way, it is more convenient to retrieve data from the warehouse. Because the cache often uses RAM (non-permanent storage after power failure), the files will still be sent to the hard disk and other storage for permanent storage after busy. The largest cache in the computer is the memory, the fastest is the L1 and L2 cache on the CPU, the graphics card is the cache for GPU, the hard disk also has 16 m or 32 m cache. Never regard the cache as a thing. It is a collectively referred to as a processing method!

The current mobile phone can be regarded as a combination of mobile phones and portable computers. Therefore, the android cache and icrazy cache all mean the same thing, it is a "workbench" that temporarily takes out "place" from the "warehouse" and you will need to use it later. These caches are pre-read attachments and deletion does not affect usage, but it will speed up the running of mobile phones, just as you put thousands of books on the shelves in an orderly arrangement, although every time you have to climb up to get books before the summer vacation, however, these books won't affect your walking speed because they are easily put on the ground.

Hope to help you.

How to Implement the android cache technology?

... Has never used cache ?? Impossible ...! Just give me some advice .. Thank you...
 

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.