Java Code Examples for com.jakewharton.disklrucache.DiskLruCache#Editor

The following examples show how to use com.jakewharton.disklrucache.DiskLruCache#Editor . 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: 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 2
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 3
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 4
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 5
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 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: 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 8
Source File: DiskCacheManager.java    From LittleFreshWeather with Apache License 2.0 5 votes vote down vote up
public void putCityWeather(WeatherEntity weatherEntity, String cityId) {
    if (null == mDiskLruCache || null == weatherEntity || null == cityId)
        return;

    try {
        DiskLruCache.Editor editor = mDiskLruCache.edit(StringUtil.bytesToMd5String((CITY_WEATHER_ENTITY_CACHE_KEY + cityId).getBytes()));
        if (editor != null) {
            editor.set(0, mJsonProcesser.weatherEntityToJson(weatherEntity));
            editor.commit();
            mDiskLruCache.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: DiskCache.java    From ObjectCache with MIT License 5 votes vote down vote up
protected boolean writeValueToCache ( String value, DiskLruCache.Editor editor ) throws IOException {
	OutputStream outputStream = null;
	try {
		outputStream = new BufferedOutputStream(editor.newOutputStream(0));
		outputStream.write(value.getBytes(STRING_ENCODING));
	} finally {
		if (outputStream != null) {
			outputStream.close();
		}
	}

	return true;
}
 
Example 10
Source File: BasicCaching.java    From SmartCache with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void addInCache(Response<T> response, byte[] rawResponse) {
    String cacheKey = urlToKey(response.raw().request().url().url());
    memoryCache.put(cacheKey, rawResponse);

    try {
        DiskLruCache.Editor editor = diskCache.edit(urlToKey(response.raw().request().url().url()));
        editor.set(0, new String(rawResponse, Charset.defaultCharset()));
        editor.commit();
    }catch(IOException exc){
        Log.e("SmartCall", "", exc);
    }
}
 
Example 11
Source File: CacheIOHelper.java    From RichText with MIT License 5 votes vote down vote up
@Override
public void writeToCache(String key, InputStream input, DiskLruCache cache) {
    if (cache != null) {

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

            if (edit == null) {
                return;
            }

            OutputStream outputStream = edit.newOutputStream(0);

            byte[] buffer = new byte[BUFFER_SIZE];

            int len;

            while ((len = input.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }

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

            input.close();

            edit.commit();
        } catch (IOException e) {
            Debug.e(e);
        }

    }
}
 
Example 12
Source File: LruDiskCache.java    From RxCache with Apache License 2.0 5 votes vote down vote up
public <T> boolean save(String key, T value) {
    if (mDiskLruCache == null) {
        return false;
    }
    //如果要保存的值为空,则删除
    if (value == null) {
        return remove(key);
    }
    DiskLruCache.Editor edit = null;
    try {
        edit = mDiskLruCache.edit(key);
        OutputStream sink = edit.newOutputStream(0);
        mDiskConverter.writer(sink, value);
        long l = System.currentTimeMillis();
        edit.set(1, String.valueOf(l));
        edit.commit();
        LogUtils.log("save:  value=" + value + " , status=" + true);
        return true;
    } catch (IOException e) {
        LogUtils.log(e);
        if (edit != null) {
            try {
                edit.abort();
            } catch (IOException e1) {
                LogUtils.log(e1);
            }
        }
        LogUtils.log("save:  value=" + value + " , status=" + false);
    }
    return false;
}
 
Example 13
Source File: DiskLruImageCache.java    From Android-ImageManager with MIT License 5 votes vote down vote up
private boolean writeBitmapToFile(final Bitmap bitmap, final DiskLruCache.Editor editor) throws IOException {
    OutputStream out = null;
    try {
        out = new BufferedOutputStream(editor.newOutputStream(0), Utils.IO_BUFFER_SIZE);
        return bitmap.compress(compressFormat, compressQuality, out);
    } finally {
        if (out != null) {
            out.close();
        }
    }
}
 
Example 14
Source File: DiskCacheManager.java    From LittleFreshWeather with Apache License 2.0 5 votes vote down vote up
public void putCityEntities(List<CityEntity> cityEntities) {
    if (null == mDiskLruCache || null == cityEntities)
        return;

    try {
        DiskLruCache.Editor editor = mDiskLruCache.edit(StringUtil.bytesToMd5String(CITY_ENTITIES_CACHE_KEY.getBytes()));
        if (editor != null) {
            editor.set(0, mJsonProcesser.cityEntitiesToJson(cityEntities));
            editor.commit();
            mDiskLruCache.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: DualCache.java    From dualcache with Apache License 2.0 5 votes vote down vote up
/**
 * Put an object in cache.
 *
 * @param key    is the key of the object.
 * @param object is the object to put in cache.
 */
public void put(String key, T object) {
    // Synchronize put on each entry. Gives concurrent editions on different entries, and atomic
    // modification on the same entry.
    if (ramMode.equals(DualCacheRamMode.ENABLE_WITH_REFERENCE)) {
        ramCacheLru.put(key, object);
    }

    String ramSerialized = null;
    if (ramMode.equals(DualCacheRamMode.ENABLE_WITH_SPECIFIC_SERIALIZER)) {
        ramSerialized = ramSerializer.toString(object);
        ramCacheLru.put(key, ramSerialized);
    }

    if (diskMode.equals(DualCacheDiskMode.ENABLE_WITH_SPECIFIC_SERIALIZER)) {
        try {
            dualCacheLock.lockDiskEntryWrite(key);
            DiskLruCache.Editor editor = diskLruCache.edit(key);
            if (ramSerializer == diskSerializer) {
                // Optimization if using same serializer
                editor.set(0, ramSerialized);
            } else {
                editor.set(0, diskSerializer.toString(object));
            }
            editor.commit();
        } catch (IOException e) {
            logger.logError(e);
        } finally {
            dualCacheLock.unLockDiskEntryWrite(key);
        }
    }
}
 
Example 16
Source File: CacheManager.java    From hacker-news-android with Apache License 2.0 5 votes vote down vote up
protected boolean writeValueToCache(String value, DiskLruCache.Editor editor) throws IOException {
    OutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(editor.newOutputStream(0));
        outputStream.write(value.getBytes(STRING_ENCODING));
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }

    return true;
}
 
Example 17
Source File: BitmapLruCache.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CacheableBitmapDrawable putInDiskCache(final String url, final CacheableBitmapDrawable drawable,
                                              Bitmap.CompressFormat compressFormat, int compressQuality) {

    if (null != mDiskCache) {
        checkNotOnMainThread();

        final String key = transformUrlForDiskCacheKey(url);
        final ReentrantLock lock = getLockForDiskCacheEdit(key);
        lock.lock();

        OutputStream os = null;

        try {
            DiskLruCache.Editor editor = mDiskCache.edit(key);
            os = editor.newOutputStream(0);
            drawable.getBitmap().compress(compressFormat, compressQuality, os);
            os.flush();
            editor.commit();
        } catch (IOException e) {
            Log.e(Constants.LOG_TAG, "Error while writing to disk cache", e);
        } finally {
            IoUtils.closeStream(os);
            lock.unlock();
            scheduleDiskCacheFlush();
        }
    }

    return drawable;
}
 
Example 18
Source File: CacheManager.java    From xposed-rimet with Apache License 2.0 5 votes vote down vote up
/**
 * 保存失败
 * @param editor
 */
private void abortQuietly(DiskLruCache.Editor editor) {

    if (editor == null) return;

    try {
        editor.abort();
    } catch (Throwable tr) {
        Alog.e("abort异常", tr);
    }
}
 
Example 19
Source File: BitmapDiskLruCache.java    From Presentation with Apache License 2.0 4 votes vote down vote up
SaveBitmapTask(Bitmap bitmap, DiskLruCache.Editor editor) {
    mBitmap = bitmap;
    mEditor = editor;
}
 
Example 20
Source File: SimpleDiskCache.java    From Reservoir with MIT License 4 votes vote down vote up
private CacheOutputStream(OutputStream os, DiskLruCache.Editor editor) {
    super(os);
    this.editor = editor;
}