1. Preface
There are several blogs that mention caching, but they are relatively fragmented and recently have a new understanding of the data cache, which has been recorded in my understanding of caching and how I use it in my project.
2. Why data caching is required
Here is my personal opinion.
- Consider the user experience in extreme environments
- Off-screen condition
- Slow speed of the situation
- Save Client traffic
- Reduce server pressure
3. What kind of data needs to be cached
- First, the most need to cache is the picture (this is not much to say)
- Next is some very important data (such as the app home page data)
4. Common data caching methods and pros and cons
- File cache
- The advantage is that you can cache very large data (very small in actual use)
- Disadvantages of reading and writing cost large
- Database Cache
- Advantages data structure is clear and easy to manage
- Disadvantages same read and write overhead
- Sharedpreferences
- Advantages of simple operation, speed is also compared to block
- The disadvantage only supports basic data types
In the above three methods, I personally like Sharedpreference, in the case of sharedpreference can not meet the requirements, will choose the database cache. Snappy easy to use, fast and worth a try
5. What I know about caching policies
- Pre-load cache, manual refresh of data (Sina Weibo)
- Pre-loading the cache while requesting new data and updating the cached data, but not immediately refresh the interface (CSDN client)
- Pre-load cache, request new data and update the interface inside, cut and update cached data
The above three strategies each have their own advantages and disadvantages, need more practical needs to choose the right strategy.
6. The entire process
- Request data
- Read cache
- Callback Updateview
- Continue to request new data
- Server returns results
- The cache data is updated successfully and a more selective method is selected for immediate updating
- Failed
7. Summary
Data caching is important, data caching is important, and data caching is important. At present, also is to have some understanding to the data cache, also have some practice, take out to share to everybody.
Android Practical Tips-Caching for use