introduce

Android app caching is very common, and most apps use the DiskLruCache caching technology, and there are a lot of people who have talked about DiskLruCache, so I won’t cover it here.

DiskLruCache isn’t hard to use, but if you don’t package it, you’ll get all kinds of open and all kinds of combinations of methods. Anyway, it’s a lot of trouble not to package it, so here’s a blog, one line of code to cache.

Results demonstrate

This is the effect of saving cached data and reading cached data

support

What data can be cached?

  1. Any Java object, including the List collection.
  2. The picture

If you want to cache JSON data, you can cache it locally as a String object. If you want to read the String, you can cache the stream locally. If you want to read the stream, you can read the stream locally.

Use the DiskLruCache caching technology

The benefits of using DiskLruCache caching technology is that you don’t have to care about the cache expiration time, as well as the size of the cache problem, also need not care about version changes after data format change, he will automatically determine the software version, old data will be automatically deleted expired, ensure to get the data no problem, also need not care about SD card problem

How to use

In fact, the tool is much easier to use than you might think

1. Save the cache

1. Save the Java object

        String cachePath = getCacheDir(this);

        User user = new User();
        user.name = "fussen";
        user.age = "100";
        
        Cache.with(this)
             .path(cachePath)
             .saveCache("key", user);
Copy the code

2. Save the List data

       List<String> mData = new ArrayList<>();
   
       String cachePath = getCacheDir(this);
		
        Cache.with(this)
             .path(cachePath))
             .saveCache("key", mData);
Copy the code

3. Save the picture


	String imageUrl = "http://img.my.csdn.net/uploads/201407/26/1406383059_2237.jpg";
	String cachePath = getCacheDir(this);
	Cache.with(this)
	       .path(cachePath)
	       .saveImage(imageUrl);
Copy the code

2. Read the cache

1. Read the Java object cache

	String cachePath = getCacheDir(this);
	User user = Cache.with(this)
		         .path(cachePath)
		         .getCache("key", User.class);
Copy the code

2. Read the List collection data

	String cachePath = getCacheDir(this);
	List<String> cacheList = Cache.with(this)
				      .path(cachePath)
		                      .getCacheList("key", String.class);
Copy the code

3. Read the image cache

	String cachePath = getCacheDir(this);
	
	Bitmap cacheBitmap = Cache.with(this)
		                  .path(cachePath)
		                  .getImageCache(imageUrl);

        imageView.setImageBitmap(cacheBitmap);
Copy the code

After the above steps, your cache will be saved locally, as shown below:

Journal caches classic identity files for DiskLruCache.

3. Specification:

  1. The cache path can be set or not set. The default cache path is /sdcard/Android/data/ (application package name) /cache
  2. The key parameter is the unique identifier of the cache file, and the image cache is uniquely identified by the URL of the image
  3. The name of the cache file is the md5 encoded name

Rely on

Dependencies {the compile 'cc. Fussen: cachelibrary: 1.5.0'}Copy the code

The last show

  1. The package of this tool is relatively simple and will be optimized later. The package idea of this tool comes from Glide
  2. If you have any problems, you can contact me directly, okay
  3. To understand the tool demo and principle, you can directly reply to “cache” in wechat public number: AppCode to get the source address
  4. Scan the qr code below to follow AppCode