IgDiskCache


Source link: https://github.com/Instagram/ig-disk-cache

IgDiskCache

Exception handling is always a cumbersome but unavoidable part of dealing with disk cache on Android. Complex error handling not only makes your code hard to understand, but also prone to developer errors. IgDiskCache is a fault-tolerant Android disk cache library that helps simplify the error handling logic and makes your file caching code cleaner and much easier to maintain.

For more Instagram engineering updates and shared insights, please visit the Instagram Engineering blog.

Getting Started

Usage

Initialization

  • When initializing the IgDiskCache, we can limit the number of bytes, and the number of file entries that can be stored in the cache. We can also use our own serialExecutor to handle Journal logging tasks.

  • For the following cases, the class constructor will return a stub instance of the IgDiskCache: cache directory is NULL or not accessible, maxCacheSizeInBytes or maxFileCount is invalid.

  • A non-UI thread assertion is introduced before the IgDiskCache initialization to prevent running expensive disk IO operations on the UI thread.

  • Note: the cache limits are not strict: the cache may temporarily exceed the cache size or file count limit when waiting for the least-recently-used files to be removed.

IgDiskCache(File directory)  IgDiskCache(File directory, long maxCacheSizeInBytes) IgDiskCache(File directory, long maxCacheSizeInBytes, int maxFileCount) IgDiskCache(File directory, long maxCacheSizeInBytes, int maxFileCount, Executor serialExecutor)  mDiskCache = new IgDiskCache(cacheDir, maxCacheSizeInBytes);

Writing

  • Call edit(key) to get an outputStream for the cache entry. The cache key must match the regex [a-z0-9_-]{ 1,120 } . The method will return an OptionalStream.

  • If the cache is a stub instance or the cache entry is not available for editing, the edit operation will return an OptionalStream.absent() instead.

  • If an error occurs while writing to an EditorOutputStream, the operation will fail silently without throwing IOExceptions. The partial change will not be committed to the cache, and the stale entry with the same key (if exist) will also be discarded.

  • After editing, instead of close() the output stream, the EditorOutputStream need to either commit() or abort() the change.

  • If we try to edit the same cache entry from two different places at the same time, an IllegalStateException will be thrown to notify the developer there's a race condition.

OptionalStream<EditorOutputStream> outputStream = mDiskCache.edit(key);
 if (outputStream.isPresent()) {
  try {

writeFileToStream(outputStream.get());

outputStream.get().commit();
  
}
 finally {

outputStream.get().abortUnlessCommitted();
  
}
 
}

Reading

  • Call has(key) to know if the cache entry associated with a certain key exists and ready-for-read. The method will return True if the entry is available, is not currently under editing, and not corrupted because of the previous writing failure.

  • Call get(key) to get an inputStream of the cache entry using a cache key. The method will return an OptionalStream.

  • If the cache is a stub instance, the file entry is not available, or the file entry is still under editing, an OptionalStream.absent() will be returned.

  • If any error occurs while reading from the SnapshotInputStream, IOExceptions will still be thrown out as normal FileInputStream does.

  • Similar to FileInputStream, use close() to close the SnapshotInputStream after use.

OptionalStream<SnapshotInputStream> inputStream = mDiskCache.get(key);
 if (inputStream.isPresent()) {
  try {

readFromInputStream(inputStream.get());
  
}
 finally {

inputStream.get().close();
  
}
 
}

Closing

  • Request the disk cache to trim to size or file count.
mDiskCache.flush();
  • Finish using the disk cache, you could use close() to close the cache (note: close() can only be called from non-UI thread):
mDiskCache.close();

Compile a AAR

./gradlew clean assembleRelease 

Outputs can be found in igdiskcache/build/outputs/

Run the Tests

./gradlew clean test 

Download

Maven

<dependency>
<groupId>com.instagram.igdiskcache</groupId>
<artifactId>ig-disk-cache</artifactId>
<version>1.0.0</version>
<type>aar</type> </dependency>

Gradle

dependencies {

  compile 'com.instagram.igdiskcache:ig-disk-cache:1.0.0@aar' 
}

Other Instagram Android Projects

License

Copyright (c) 2016-present, Facebook, Inc. All rights reserved.  This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree. An additional grant of patent rights can be found in the PATENTS file in the same directory. 

Resources

Gradle plugin for Github releases. You can fully automate releases via gradle-github-plugin.

Modification of ZXING Barcode Scanner project for easy Android QR-Code detection in portrait mode and AR purposes.

This project implements an Android view which show camera and notify when there's a QR code inside the preview.

LoadingView is easy to implement loading indicators for your views or layouts.

An in-memory data store backed by shared preferences, for Android.

Easy to use inline @Keep annotation to keep class.

A UI widget that allows for headers and footers on lists backed by RecyclerView.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes