com.jakewharton.disklrucache.DiskLruCache Java Examples

The following examples show how to use com.jakewharton.disklrucache.DiskLruCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BitmapDiskLruCache.java    From Presentation with Apache License 2.0 6 votes vote down vote up
public Bitmap get(String key) throws IOException {
    Bitmap bitmap = null;
    DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
    if (snapshot == null) {
        return null;
    }

    final InputStream in = snapshot.getInputStream(0);
    if (in != null) {
        final BufferedInputStream buffIn = new BufferedInputStream(in, BUFFER_SIZE);
        bitmap = BitmapFactory.decodeStream(buffIn);
        buffIn.close();
    }

    snapshot.close();
    return bitmap;
}
 
Example #2
Source File: DiskCache.java    From ObjectCache with MIT License 6 votes vote down vote up
public String getValue ( String key ) throws IOException {
	String value = null;
	DiskLruCache.Snapshot snapshot = null;

	try {
		snapshot = diskLruCache.get(getHashOf(key));
		if (snapshot == null) {
			return null;
		}

		value = snapshot.getString(0);
	} finally {
		if (snapshot != null) {
			snapshot.close();
		}
	}

	return value;
}
 
Example #3
Source File: BitmapLoader.java    From CombineBitmap with Apache License 2.0 6 votes vote down vote up
private Bitmap loadBitmapFromHttp(String url, int reqWidth, int reqHeight)
        throws IOException {
    String key = Utils.hashKeyFormUrl(url);
    DiskLruCache.Editor editor = mDiskLruCache.edit(key);
    if (editor != null) {
        OutputStream outputStream = editor.newOutputStream(0);
        if (downloadUrlToStream(url, outputStream)) {
            editor.commit();
        } else {
            editor.abort();
        }
        mDiskLruCache.flush();

        executeUndoTasks(url);
    }
    return loadBitmapFromDiskCache(url, reqWidth, reqHeight);
}
 
Example #4
Source File: DiskCache.java    From ObjectCache with MIT License 6 votes vote down vote up
public boolean contains ( String key ) throws IOException {
	boolean found = false;
	DiskLruCache.Snapshot snapshot = null;

	try {
		snapshot = diskLruCache.get(getHashOf(key));
		if (snapshot != null) {
			found = true;
		}
	} finally {
		if (snapshot != null) {
			snapshot.close();
		}
	}

	return found;
}
 
Example #5
Source File: CacheManager.java    From xposed-rimet with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized <T> void put(String key, T value) {

    if (mDiskLruCache == null) return;

    DiskLruCache.Editor editor = null;

    try {
        editor = mDiskLruCache.edit(key);
        if (editor != null) {
            editor.set(0, mGson.toJson(value));
            editor.commit();
        }
        mDiskLruCache.flush();
    } catch (Throwable tr) {
        abortQuietly(editor);
        Alog.e("put异常", tr);
    }
}
 
Example #6
Source File: DiskCache.java    From ObjectCache with MIT License 6 votes vote down vote up
public void setKeyValue ( String key, String value ) throws IOException {
	DiskLruCache.Editor editor = null;
	try {
		editor = diskLruCache.edit(getHashOf(key));
		if (editor == null) {
			return;
		}

		if (writeValueToCache(value, editor)) {
			diskLruCache.flush();
			editor.commit();
		} else {
			editor.abort();
		}
	} catch (IOException e) {
		if (editor != null) {
			editor.abort();
		}

		throw e;
	}
}
 
Example #7
Source File: BitmapLoader.java    From CombineBitmap with Apache License 2.0 6 votes vote down vote up
private Bitmap loadBitmapFromDiskCache(String url, int reqWidth,
                                       int reqHeight) throws IOException {
    Bitmap bitmap = null;
    String key = Utils.hashKeyFormUrl(url);
    DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);
    if (snapShot != null) {
        FileInputStream fileInputStream = (FileInputStream) snapShot.getInputStream(0);
        FileDescriptor fileDescriptor = fileInputStream.getFD();
        bitmap = compressHelper.compressDescriptor(fileDescriptor, reqWidth, reqHeight);
        if (bitmap != null) {
            lruCacheHelper.addBitmapToMemoryCache(key, bitmap);
        }
    }

    return bitmap;
}
 
Example #8
Source File: LruDiskCache.java    From RxEasyHttp with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> T doLoad(Type type, String key) {
    if (mDiskLruCache == null) {
        return null;
    }
    try {
        DiskLruCache.Editor edit = mDiskLruCache.edit(key);
        if (edit == null) {
            return null;
        }

        InputStream source = edit.newInputStream(0);
        T value;
        if (source != null) {
            value = mDiskConverter.load(source,type);
            Utils.close(source);
            edit.commit();
            return value;
        }
        edit.abort();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #9
Source File: Images.java    From twitt4droid with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the disk cache when is closed or null.
 * 
 * @param context the application context.
 */
private static void intDiskCacheIfNeeded(Context context) {
    if (DISK_CACHE == null || DISK_CACHE.isClosed()) {
        try {
            long size = 1024 * 1024 * 10;
            String cachePath = 
                    Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || Files.isExternalStorageRemovable()
                    ? Files.getExternalCacheDir(context).getPath() 
                    : context.getCacheDir().getPath();
            File file = new File(cachePath + File.separator + IMAGE_CACHE_DIR);
            DISK_CACHE = DiskLruCache.open(file, 1, 1, size); // Froyo sometimes fails to initialize
        } catch (IOException ex) {
            Log.e(TAG, "Couldn't init disk cache", ex);
        }
    }
}
 
Example #10
Source File: LruDiskCache.java    From RxEasyHttp with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> boolean doSave(String key, T value) {
    if (mDiskLruCache == null) {
        return false;
    }
    try {
        DiskLruCache.Editor edit = mDiskLruCache.edit(key);
        if (edit == null) {
            return false;
        }
        OutputStream sink = edit.newOutputStream(0);
        if (sink != null) {
            boolean result = mDiskConverter.writer(sink, value);
            Utils.close(sink);
            edit.commit();
            return result;
        }
        edit.abort();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #11
Source File: DiskCache.java    From RxCache with Apache License 2.0 6 votes vote down vote up
@Override
public boolean put(String key, byte[] data) {
    Utilities.checkNullOrEmpty(key, "key is null or empty.");

    key = Utilities.Md5(key);
    if (lruCache == null) return false;
    try {
        DiskLruCache.Editor edit = lruCache.edit(key);
        if (edit == null) {
            return false;
        }
        OutputStream sink = edit.newOutputStream(0);
        if (sink != null) {
            sink.write(data, 0, data.length);
            sink.flush();
            Utilities.closeQuietly(sink);
            edit.commit();
            LogUtil.i("DiskCache save success!");
            return true;
        }
        edit.abort();
    } catch (IOException e) {
        LogUtil.t(e);
    }
    return false;
}
 
Example #12
Source File: DiskCacheManager.java    From LittleFreshWeather with Apache License 2.0 6 votes vote down vote up
public DiskCacheManager(Context context, String cacheDirName) {
    if (null == context || null == cacheDirName) {
        throw new IllegalArgumentException("arguments cannot be null");
    }
    this.mContext = context;
    this.mCacheDirName = cacheDirName;
    mJsonProcesser = new JsonProcesser();
    File cacheDir = FileUtil.getDiskCacheDir(mContext, mCacheDirName);
    if (!cacheDir.exists())
        cacheDir.mkdir();
    try {
        mDiskLruCache = DiskLruCache.open(cacheDir, PackageUtil.getAppVersion(mContext), 1, DISK_CACHE_SIZE);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #13
Source File: BitmapDiskLruCache.java    From Presentation with Apache License 2.0 6 votes vote down vote up
public boolean isContains(String key) {
    boolean contained = false;
    DiskLruCache.Snapshot snapshot = null;

    try {
        snapshot = mDiskLruCache.get(key);
        contained = (snapshot != null);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (snapshot != null) {
            snapshot.close();
        }
    }

    return contained;
}
 
Example #14
Source File: RestVolleyImageCache.java    From RestVolley with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isCached(String cacheKey) {
    String key = generateKey(cacheKey);
    boolean isInMem = mMemCache.get(key) != null;
    boolean isInDisk = false;
    if (getDiskCache() != null) {
        try {
            DiskLruCache.Snapshot snapshot = getDiskCache().get(key);
            if (snapshot != null) {
                isInDisk = snapshot.getInputStream(0) != null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return isInMem && isInDisk;
}
 
Example #15
Source File: CacheManager.java    From xposed-rimet with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized  <T> T get(String key, Class<T> tClass) {

    if (mDiskLruCache == null) return null;

    DiskLruCache.Snapshot snapshot = null;

    try {
        snapshot = mDiskLruCache.get(key);
        if (snapshot != null) {
            final String temp = snapshot.getString(0);
            return mGson.fromJson(temp, tClass);
        }
    } catch (Throwable tr) {
        Alog.e("get异常", tr);
    } finally {
        FileUtil.closeQuietly(snapshot);
    }
    return null;
}
 
Example #16
Source File: BasicCaching.java    From SmartCache with Apache License 2.0 6 votes vote down vote up
@Override
public <T> byte[] getFromCache(Request request) {
    String cacheKey = urlToKey(request.url().url());
    byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey);
    if(memoryResponse != null){
        Log.d("SmartCall", "Memory hit!");
        return memoryResponse;
    }

    try {
        DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey);
        if(cacheSnapshot != null){
            Log.d("SmartCall", "Disk hit!");
            return cacheSnapshot.getString(0).getBytes();
        }else{
            return null;
        }
    }catch(IOException exc){
        return null;
    }
}
 
Example #17
Source File: LruCacheActivity.java    From android-open-framework-analysis with Apache License 2.0 6 votes vote down vote up
private void writeDiskCache() throws IOException {

        File directory = getCacheDir();
        int appVersion = 1;
        int valueCount = 1;
        long maxSize = 10 * 1024;
        DiskLruCache diskLruCache = DiskLruCache.open(directory, appVersion, valueCount, maxSize);

        DiskLruCache.Editor editor = diskLruCache.edit(String.valueOf(System.currentTimeMillis()));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(editor.newOutputStream(0));
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bufferedOutputStream);

        editor.commit();
        diskLruCache.flush();
        diskLruCache.close();
    }
 
Example #18
Source File: CacheIOHelper.java    From RichText with MIT License 6 votes vote down vote up
@Override
public DrawableSizeHolder readFromCache(String key, DiskLruCache cache) {
    if (cache != null) {

        try {
            DiskLruCache.Snapshot snapshot = cache.get(key);

            if (snapshot == null) {
                return null;
            }

            InputStream inputStream = snapshot.getInputStream(0);

            DrawableSizeHolder drawableSizeHolder = DrawableSizeHolder.read(inputStream, key);

            inputStream.close();

            return drawableSizeHolder;
        } catch (IOException e) {
            Debug.e(e);
        }
    }
    return null;
}
 
Example #19
Source File: CacheIOHelper.java    From RichText with MIT License 6 votes vote down vote up
@Override
public void writeToCache(String key, DrawableSizeHolder input, DiskLruCache cache) {
    if (cache != null) {

        try {
            DiskLruCache.Editor edit = cache.edit(key);

            if (edit == null) {
                return;
            }

            OutputStream outputStream = edit.newOutputStream(0);

            input.save(outputStream);

            outputStream.flush();
            outputStream.close();

            edit.commit();
        } catch (IOException e) {
            Debug.e(e);
        }
    }
}
 
Example #20
Source File: LruDiskCache.java    From RxCache with Apache License 2.0 6 votes vote down vote up
public <T> CacheHolder<T> load(String key, Type type) {
    if (mDiskLruCache == null) {
        return null;
    }
    try {
        DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
        if (snapshot != null) {
            InputStream source = snapshot.getInputStream(0);
            T value = mDiskConverter.load(source, type);
            long timestamp = 0;
            String string = snapshot.getString(1);
            if (string != null) {
                timestamp = Long.parseLong(string);
            }
            snapshot.close();
            return new CacheHolder<>(value, timestamp);
        }
    } catch (IOException e) {
        LogUtils.log(e);
    }
    return null;
}
 
Example #21
Source File: DiskLruCacheHelper.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
public DiskLruCache.Editor editor(String key) {
    try {
        key = Utils.hashKeyForDisk(key);
        //wirte DIRTY
        DiskLruCache.Editor edit = mDiskLruCache.edit(key);
        //edit maybe null :the entry is editing
        if (edit == null) {
            Log.w(TAG, "the entry spcified key:" + key + " is editing by other . ");
        }
        return edit;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
 
Example #22
Source File: DiskLruCacheHelper.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
private DiskLruCache generateCache(Context context, File dir, int maxCount) throws IOException {
    if (!dir.exists() || !dir.isDirectory()) {
        throw new IllegalArgumentException(
                dir + " is not a directory or does not exists. ");
    }

    int appVersion = context == null ? DEFAULT_APP_VERSION : Utils.getAppVersion(context);

    DiskLruCache diskLruCache = DiskLruCache.open(
            dir,
            appVersion,
            DEFAULT_VALUE_COUNT,
            maxCount);

    return diskLruCache;
}
 
Example #23
Source File: DiskLruImageCache.java    From DaVinci with Apache License 2.0 6 votes vote down vote up
public boolean containsKey(String key) {

        boolean contained = false;
        DiskLruCache.Snapshot snapshot = null;
        try {
            snapshot = mDiskCache.get(key);
            contained = snapshot != null;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (snapshot != null) {
                snapshot.close();
            }
        }

        return contained;

    }
 
Example #24
Source File: RestVolleyImageCache.java    From RestVolley with Apache License 2.0 6 votes vote down vote up
private void putBitmapToDiskLruCache(final String key, final Bitmap bitmap) {
    TaskScheduler.execute(new Runnable() {
        @Override
        public void run() {
            try {
                if (getDiskCache() != null) {
                    DiskLruCache.Editor editor = getDiskCache().edit(key);
                    if (editor != null) {
                        OutputStream outputStream = editor.newOutputStream(0);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
                        editor.commit();

                        outputStream.close();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
 
Example #25
Source File: CacheManager.java    From hacker-news-android with Apache License 2.0 6 votes vote down vote up
public void put(String key, Object object) throws IOException {
    mMemCache.put(key, object);
    DiskLruCache.Editor editor = null;
    try {
        editor = mDiskLruCache.edit(getHashOf(key));
        if (editor == null) {
            return;
        }
        if (writeValueToCache(mGson.toJson(object), editor)) {
            mDiskLruCache.flush();
            editor.commit();
        }
        else {
            editor.abort();
        }
    } catch (IOException e) {
        if (editor != null) {
            editor.abort();
        }

        throw e;
    }

}
 
Example #26
Source File: DiskLruImageCache.java    From DaVinci with Apache License 2.0 5 votes vote down vote up
public DiskLruImageCache(Context context, String uniqueName, int diskCacheSize,
                         Bitmap.CompressFormat compressFormat, int quality) {
    try {
        final File diskCacheDir = getDiskCacheDir(context, uniqueName);
        mDiskCache = DiskLruCache.open(diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize);
        mCompressFormat = compressFormat;
        mCompressQuality = quality;
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #27
Source File: DiskLruImageCache.java    From DaVinci with Apache License 2.0 5 votes vote down vote up
private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor)
        throws IOException, FileNotFoundException {
    OutputStream out = null;
    try {
        bitmap.setHasAlpha(true);
        //bitmap.setConfig(Bitmap.Config.ARGB_8888);
        out = new BufferedOutputStream(editor.newOutputStream(0), IO_BUFFER_SIZE);
        return bitmap.compress(mCompressFormat, mCompressQuality, out);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}
 
Example #28
Source File: CacheManager.java    From xposed-rimet with Apache License 2.0 5 votes vote down vote up
public CacheManager(Context context) {
    mGson = new Gson();
    File cacheDir = new File(context.getCacheDir(), "config");
    try {
        mDiskLruCache = DiskLruCache
                .open(cacheDir, 2, 1, MAX_SIZE);
    } catch (IOException e) {
        Alog.e("异常了", e);
    }
}
 
Example #29
Source File: BitmapPool.java    From RichText with MIT License 5 votes vote down vote up
private static DiskLruCache getTempDiskLruCache() {
    if (tempDiskLruCache == null && cacheDir != null) {
        try {
            tempDiskLruCache = DiskLruCache.open(tempDir, version, 1, MAX_TEMP_LOCAL_CACHE_SIZE);
        } catch (IOException e) {
            Debug.e(e);
        }
    }
    return tempDiskLruCache;
}
 
Example #30
Source File: SimpleDiskCache.java    From Reservoir with MIT License 5 votes vote down vote up
StringEntry getString(String key) throws IOException {
    DiskLruCache.Snapshot snapshot = diskLruCache.get(toInternalKey(key));
    if (snapshot == null) {
        return null;
    }

    try {
        return new StringEntry(snapshot.getString(VALUE_IDX));
    } finally {
        snapshot.close();
    }
}