Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Android's DiskLruCache disk caching mechanism

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the principle of Android's DiskLruCache disk caching mechanism? many novices are not very clear about it. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Why use DiskLruCache1, LruCache and DiskLruCache

Both LruCache and DiskLruCache use the LRU algorithm to manage the cache through the LRU algorithm, which deletes the most recently used data and retains the most commonly used data based on the least recent use.

LruCache is used for memory caching, while DiskLruCache is the storage device cache

2. Why use DiskLruCache

The significance of the existence of offline data, when there is no network or the network condition is not good, APP still has some functions is a good user experience

Suppose the news client such as NetEase News stores the data completely in the cache instead of using DiskLruCache technology, then when the client is destroyed and the cache is released, it means that it will be a blank to open APP again.

In addition, DiskLruCache technology can also provide technical support for the "offline reading" function of app.

The storage path of DiskLruCache can be customized, but it can also be the default storage path, and the default storage path is generally like this: / sdcard/Android/data/ package name / cache, which refers to the package name of APP. We can open it on the phone and browse this path.

2. DiskLruCache uses 1, adds dependency / / add dependence implementation 'com.jakewharton:disklrucache:2.0.2'2, and creates DiskLruCache object

/ * * directory-cache directory * appVersion-cache version * valueCount-number of value per key * maxSize-upper limit of cache size * / DiskLruCache diskLruCache = DiskLruCache.open (directory, 1, 1, 1024 * 1024 * 10) 3. Add / get cache (one-to-one) / * add a cache. A key corresponds to a value * / public void addDiskCache (String key, String value) throws IOException {File cacheDir = context.getCacheDir (); DiskLruCache diskLruCache = DiskLruCache.open (cacheDir, 1,1,1024 * 1024 * 10); DiskLruCache.Editor editor = diskLruCache.edit (key) / / index corresponds to valueCount, respectively: 0meme 1 editor.newOutputStream (0) .write (value.getBytes ()); editor.commit (); diskLruCache.close ();} / * get a cache, and a key corresponds to a value * / public void getDiskCache (String key) throws IOException {File directory = context.getCacheDir (); DiskLruCache diskLruCache = DiskLruCache.open (directory, 1, 1024 * 1024 * 10) String value = diskLruCache.get (key) .getString (0); diskLruCache.close ();} 4. Add / get cache (one to many) / * add a cache, 1 key corresponds to 2 value * / public void addDiskCache (String key, String value1, String value2) throws IOException {File directory = context.getCacheDir (); DiskLruCache diskLruCache = DiskLruCache.open (directory, 1, 2, 1024 * 1024 * 10); DiskLruCache.Editor editor = diskLruCache.edit (key) Editor.newOutputStream (0) .write (value1.getBytes ()); editor.newOutputStream (1) .write (value2.getBytes ()); editor.commit (); diskLruCache.close ();} / * add a cache, 1 key corresponds to 2 value * / public void getDiskCache (String key) throws IOException {File directory = context.getCacheDir (); DiskLruCache diskLruCache = DiskLruCache.open (directory, 1, 2, 1024); DiskLruCache.Snapshot snapshot = diskLruCache.get (key) String value1 = snapshot.getString (0); String value2 = snapshot.getString (1); diskLruCache.close ();} III. Source code analysis

1. Open ()

The construction method of DiskLruCache is private modification, which tells us that instances cannot be obtained through new DiskLruCache. The construction method is as follows:

Private DiskLruCache (File directory, int appVersion, int valueCount, long maxSize) {this.directory = directory; this.appVersion = appVersion; this.journalFile = new File (directory, JOURNAL_FILE); this.journalFileTmp = new File (directory, JOURNAL_FILE_TEMP); this.journalFileBackup = new File (directory, JOURNAL_FILE_BACKUP); this.valueCount = valueCount; this.maxSize = maxSize;}

However, the open () method is provided for us to obtain an instance of DiskLruCache. The open method is as follows:

/ * * Opens the cache in {@ code directory}, creating a cache if none exists * there. * * @ param directory a writable directory * @ param valueCount the number of values per cache entry. Must be positive. * @ param maxSize the maximum number of bytes this cache should use to store * @ throws IOException if reading or writing the cache directory fails * / public static DiskLruCache open (File directory, int appVersion, int valueCount, long maxSize) throws IOException {if (maxSize)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report