项目作者: solkin

项目描述 :
💾 Disk LRU cache with persisted journal
高级语言:
项目地址: git://github.com/solkin/disk-lru-cache.git
创建时间: 2018-08-26T13:29:04Z
项目社区:https://github.com/solkin/disk-lru-cache

开源协议:MIT License

下载


Disk LRU Cache Android Arsenal

Disk LRU (least recently used) cache with persisted journal.
This cache has specific capacity and location.
Rarely requested files are evicted by actively used.

Lightweight and extremely easy to use.

Cache icon

Add dependency

Step 1. Add the JitPack repository to your build file

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Step 2. Add the dependency

  1. implementation 'com.github.solkin:disk-lru-cache:1.4'

Create DiskLruCache

  1. long CACHE_SIZE = 500 * 1024; // Size in bytes
  2. DiskLruCache cache = DiskLruCache.create(getCacheDir(), CACHE_SIZE);

Add file into cache

To manage some files by cache you just need to invoke put method like any Map.

Key - any string to request this file from cache.

File - file, that will be moved into cache.

  1. String key = "some-key";
  2. File file = File.createTempFile("random", ".dat");
  3. cache.put(key, file);

Getting file from cache

To get file from cache, just invoke get method. Yes, also like any Map.

Key is the same you put this file into cache

This method will return File you put into cache or null, if file was evicted from cache.

  1. String key = "some-key";
  2. File file = cache.get(key);

Delete file from cache

To delete file from cache, just invoke delete method.

Key is the same you put this file into cache

File will be deleted from cache and from journal.

  1. String key = "some-key";
  2. cache.delete(key);

Clear cache

Sometime you may need to clear whole cache and drop all stored files.

  1. cache().clearCache();

List keys in cache

To get all keys, managed by cache, invoke keySet() method.

This will return Set<String>.

List all keys in cache may be useful to check all files, stored in cache.

  1. Set<String> keys = cache.keySet();

Get cache status information

There are some useful cache status information, that you can request.

  1. cache.getCacheSize(); // Cache size in bytes, that you set up on cache creation.
  2. cache.getUsedSpace(); // Size of all files, stored in cache.
  3. cache.getFreeSpace(); // Free size in cache.
  4. cache.getJournalSize(); // Internal cache journal size in bytes.

License

  1. MIT License
  2. Copyright (c) 2022 Igor Solkin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.