public T get ( String key ) { Object ramResult = null ; String diskResult = null ; DiskLruCache . Snapshot snapshotObject = null ; // Try to get the object from RAM. boolean isRamSerialized = ramMode . equals ( DualCacheRamMode . ENABLE_WITH_SPECIFIC_SERIALIZER ) ; boolean isRamReferenced = ramMode . equals ( DualCacheRamMode . ENABLE_WITH_REFERENCE ) ; if ( isRamSerialized || isRamReferenced ) { ramResult = ramCacheLru . get ( key ) ; } if ( ramResult == null ) { // Try to get the cached object from disk. loggerHelper . logEntryForKeyIsNotInRam ( key ) ; if ( diskMode . equals ( DualCacheDiskMode . ENABLE_WITH_SPECIFIC_SERIALIZER ) ) { try { dualCacheLock . lockDiskEntryWrite ( key ) ; snapshotObject = diskLruCache . get ( key ) ; } catch ( IOException e ) { logger . logError ( e ) ; } finally { dualCacheLock . unLockDiskEntryWrite ( key ) ; } if ( snapshotObject != null ) { loggerHelper . logEntryForKeyIsOnDisk ( key ) ; try { diskResult = snapshotObject . getString ( 0 ) ; } catch ( IOException e ) { logger . logError ( e ) ; } } else { loggerHelper . logEntryForKeyIsNotOnDisk ( key ) ; } } T objectFromStringDisk = null ; if ( diskResult != null ) { // Load object, no need to check disk configuration since diskresult != null. objectFromStringDisk = diskSerializer . fromString ( diskResult ) ; // Refresh object in ram. if ( ramMode . equals ( DualCacheRamMode . ENABLE_WITH_REFERENCE ) ) { if ( diskMode . equals ( DualCacheDiskMode . ENABLE_WITH_SPECIFIC_SERIALIZER ) ) { ramCacheLru . put ( key , objectFromStringDisk ) ; } } else if ( ramMode . equals ( DualCacheRamMode . ENABLE_WITH_SPECIFIC_SERIALIZER ) ) { if ( diskSerializer == ramSerializer ) { ramCacheLru . put ( key , diskResult ) ; } else { ramCacheLru . put ( key , ramSerializer . toString ( objectFromStringDisk ) ) ; } } return objectFromStringDisk ; } } else { loggerHelper . logEntryForKeyIsInRam ( key ) ; if ( ramMode . equals ( DualCacheRamMode . ENABLE_WITH_REFERENCE ) ) { return ( T ) ramResult ; } else if ( ramMode . equals ( DualCacheRamMode . ENABLE_WITH_SPECIFIC_SERIALIZER ) ) { return ramSerializer . fromString ( ( String ) ramResult ) ; } } // No data is available. return null ; }
Return the object of the corresponding key from the cache . In no object is available return null .