DiskLruCache icon indicating copy to clipboard operation
DiskLruCache copied to clipboard

add: cache entry creation time param to Snapshot

Open feivur opened this issue 5 years ago • 1 comments

I added cache entry creation time param to Snapshot. With this simple feature we can make expiration cache. Example: private Bitmap getExpired( String key, long expire, TimeUnit timeUnit ) { Bitmap bitmap = null; try ( DiskLruCache.Snapshot snapshot = mDiskCache.get( key ) ) { if ( snapshot == null ) return bitmap; long expiredTime = new Date().getTime() - timeUnit.toMillis( expire ); if ( expire > 0 && snapshot.getCreatedTime() < expiredTime ) return null; final InputStream in = snapshot.getInputStream( 0 ); if ( in != null ) { final BufferedInputStream buffIn = new BufferedInputStream( in, IO_BUFFER_SIZE ); bitmap = BitmapFactory.decodeStream( buffIn ); } } catch ( IOException e ) { e.printStackTrace(); } return bitmap; }

feivur avatar Mar 02 '20 13:03 feivur

I added cache entry creation time param to Snapshot. With this simple feature we can make expiration cache. Example: private Bitmap getExpired( String key, long expire, TimeUnit timeUnit ) { Bitmap bitmap = null; try ( DiskLruCache.Snapshot snapshot = mDiskCache.get( key ) ) { if ( snapshot == null ) return bitmap; long expiredTime = new Date().getTime() - timeUnit.toMillis( expire ); if ( expire > 0 && snapshot.getCreatedTime() < expiredTime ) return null; final InputStream in = snapshot.getInputStream( 0 ); if ( in != null ) { final BufferedInputStream buffIn = new BufferedInputStream( in, IO_BUFFER_SIZE ); bitmap = BitmapFactory.decodeStream( buffIn ); } } catch ( IOException e ) { e.printStackTrace(); } return bitmap; }

Christo0852 avatar Jun 25 '22 01:06 Christo0852