Android development lightweight caching framework-ASimpleCache

Students who are doing Android application development believe that they are familiar with the word "cache". There may be many concepts of cache. Here are some examples of caches for program development:

  • 1. Server control cache

For example, the Volley request library uses the "Cache-Control" and "max-age" on the server to tell the client whether there is cache and the time of the cache. This is also the recommended way to use it, but it requires the cooperation of the server and is more flexible.

  • 2. The client directly controls the cache

Sometimes, if the server does not need to support, the client can directly do a layer of caching. The idea is to cache the data locally after the request. The most common is to cache the file locally. Of course, you can cache it locally. sqlite, cache data processing in the form of sqlite file is more flexible, and then the client handles the cache time by itself, and then clears the data directly when it expires. For pages that change less frequently, using this cache can reduce client traffic and reduce server concurrency.

For some news or timelines, data changes are very frequent. For this situation, it may not be suitable to set the cache time. In this case, consider making the page refresh automatically every time it comes in to get the latest data. When it is not good or disconnected, you can directly read the local cache to increase user experience. Of course, if you want more complex processing, you can coordinate with time to determine whether the current page should be refreshed.

Today I will introduce a simple caching framework, which is very simple to use.

ASimpleCache

ASimpleCache is a lightweight open source caching framework developed for android. Lightweight to only one java file (condensed from more than a dozen classes).

What can it cache?

Ordinary strings, JsonObject, JsonArray, Bitmap, Drawable, serialized java objects, and byte data.

How to use ASimpleCache?

ACache mCache = ACache.get(this);
mCache.put("test_key1", "test value");
mCache.put("test_key2", "test value", 10);//保存10秒,如果超过10秒去获取这个key,将为null
mCache.put("test_key3", "test value", 2 * ACache.TIME_DAY);//保存两天,如果超过两天去获取这个key,将为null

retrieve data

ACache mCache = ACache.get(this);
String value = mCache.getAsString("test_key1");

The last open source address of this project:  ASimpleCache

Guess you like

Origin blog.csdn.net/xhf_123/article/details/50012539