在做项目的时候,难免需要将一些数据存储在手机中,之前用sqlite和sharepreference,但是使用起来不是很方便。最近用到了一些缓存的类,非常方便,特此记录下来。
Asimplecache
Project Address: Https://github.com/yangfuhai/ASimpleCache
Asimplecache is a lightweight, open-source caching framework developed for Android. Lightweight to only one Java file (reduced by more than 10 classes). It is convenient to cache normal strings, jsonobject, Jsonarray, Bitmap, drawable, serialized Java objects, and byte data.
//store data acache Mcache = ACache.< Span class= "Hljs-keyword" >get (this ); Mcache.put ( "Test_key1" , "test value" ); Mcache.put ( "Test_key2" , "test value" , ); //saved for 10 seconds, if more than 10 seconds to get this key, will be null mcache.put ( "Test_ Key3 ", " test value ", 2 * acache.time_day); //saved for two days, if more than two days to get this key, will be null //get Data Acache Mcache = Acache. get (this ); String value = mcache.getasstring ( "test_key1" ) ;
LruCache
Reference: Android efficient loading big picture, multi-graph solution, effectively avoid program oom (Guo Lin blog)
Memory caching technology provides quick access to images that consume valuable memory from applications. The most core of these classes is LRUCache (this class is provided in the ANDROID-SUPPORT-V4 package). This class is ideal for caching images, and its main algorithm is to store recently used objects in linkedhashmap with strong references, and to remove the least recently used objects from memory before the cached value reaches a predetermined value.
PrivateLrucache<string, bitmap> Mmemorycache;@Override protected void onCreate(Bundle savedinstancestate) {//Gets the maximum amount of available memory, using memory exceeding this value can cause a OutOfMemory exception. //LRUCache pass through the constructor function to the cache value, in kilobytes. intMaxMemory = (int) (Runtime.getruntime (). MaxMemory ()/1024x768);//Use 1/8 of the maximum available memory value as the size of the cache. intCacheSize = maxmemory/8; Mmemorycache =NewLrucache<string, bitmap> (cacheSize) {@Override protected int sizeOf(String key, Bitmap Bitmap) {//Override this method to measure the size of each picture, returning the number of pictures by default. returnBitmap.getbytecount ()/1024x768; } }; } Public void Addbitmaptomemorycache(String key, Bitmap Bitmap) {if(Getbitmapfrommemcache (key) = =NULL) {Mmemorycache.put (key, bitmap); } } PublicBitmapGetbitmapfrommemcache(String key) {returnMmemorycache.get (key); }
Disklrucache
Reference: Android Disklrucache full parsing, hard disk caching best practices
(Refer also to the Guo Lin of the Great God's blog, which describes in detail the use of Disklrucache.) )
LRUCache only manages the storage and release of images in memory, and if the images are removed from memory, it is obviously time-consuming to reload the image from the network. In this, Google also provides a set of hard disk caching solutions: Disklrucache (not officially written by Google, but obtained official certification).
LRUCache and Disklrucache combined
Android Photo Wall full version, perfect combination of LRUCache and Disklrucache (or Guo Lin's blog)
Download the pictures from the network and cache them locally. The specific process and source code, refer to this blog.
Some of Android's classes about caching