Learning summary of local cache DiskLruCache

The local cache data is generally cached in / sdcard / Android / data / < application package > / cache. Because it is stored in the SDK, it will not affect the memory space of the mobile phone. This is the application cache path recognized by the Android system. When the program is uninstalled, the data here will be cleaned up together.

To create DiskLruCache, you need to call its open (File directory,int appVersion,int valueCount,long maxSize) method. You cannot directly create a new instance.

The first refers to the cache address, the second is the version number, the third is the number of files that can be cached by a key, and the fourth is the data with the most bytes cached.

To start a DiskLruCache:

DiskLruCache mDiskLruCache = null;
try {
	File cacheDir = getDiskCacheDir(context, "bitmap");
	if (!cacheDir.exists()) {
		cacheDir.mkdirs();
	}
	mDiskLruCache = DiskLruCache.open(cacheDir, getAppVersion(context), 1, 10 * 1024 * 1024);
} catch (IOException e) {
	e.printStackTrace();

The web address passed in after visiting the network is written to the local through outputStream. The write operation is completed by the DiskLruCache.Editor class, which cannot be instantiated by new. The edit() method of DiskLruCache needs to be called.

public Editor edit(String key) throws IOException

key becomes the file name of the cache file. Note that the file name should be the same as the picture name, and it needs to be encoded as MD5.

public String hashKeyForDisk(String key) {
	String cacheKey;
	try {
		final MessageDigest mDigest = MessageDigest.getInstance("MD5");
		mDigest.update(key.getBytes());
		cacheKey = bytesToHexString(mDigest.digest());
	} catch (NoSuchAlgorithmException e) {
		cacheKey = String.valueOf(key.hashCode());
	}
	return cacheKey;
}
 
private String bytesToHexString(byte[] bytes) {
	StringBuilder sb = new StringBuilder();
	for (int i = 0; i < bytes.length; i++) {
		String hex = Integer.toHexString(0xFF & bytes[i]);
		if (hex.length() == 1) {
			sb.append('0');
		}
		sb.append(hex);
	}
	return sb.toString();

The write operation is as follows:

new Thread(new Runnable() {
	@Override
	public void run() {
		try {
			String imageUrl = "http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg";
			String key = hashKeyForDisk(imageUrl);
			DiskLruCache.Editor editor = mDiskLruCache.edit(key);
			if (editor != null) {
				OutputStream outputStream = editor.newOutputStream(0);
				if (downloadUrlToStream(imageUrl, outputStream)) {
					editor.commit();
				} else {
					editor.abort();
				}
			}
			mDiskLruCache.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

The read cache is implemented by the get () method of DiskLruCache.

public synchronized Snapshot get(String key) throws IOException

The key value is the image URL MD5 encoded value.

String imageUrl = "http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg";
String key = hashKeyForDisk(imageUrl);
DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key)

A DiskLruCache.Snapshot object was read. Call its getInputStream() method to get the cached input stream.

try {
	String imageUrl = "http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg";
	String key = hashKeyForDisk(imageUrl);
	DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);
	if (snapShot != null) {
		InputStream is = snapShot.getInputStream(0);
		Bitmap bitmap = BitmapFactory.decodeStream(is);
		mImage.setImageBitmap(bitmap);
	}
} catch (IOException e) {
	e.printStackTrace();

 

Tags: snapshot Android SDK Mobile

Posted on Wed, 01 Jan 2020 16:58:48 -0800 by bskauge