All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.android.http.MemoryCache Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
/*
 * Copyright 2013 serso aka se.solovyev
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Contact details
 *
 * Email: [email protected]
 * Site:  http://se.solovyev.org
 */

package org.solovyev.android.http;

import android.graphics.Bitmap;
import android.util.Log;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MemoryCache {

	private static final String TAG = "MemoryCache";

	// Last argument true for LRU ordering
	private Map cache = Collections.synchronizedMap(new LinkedHashMap(10, 1.5f, true));

	// current allocated size
	private long size = 0;

	// max memory in bytes
	private long limit = 1000000;

	public MemoryCache() {
		// use 25% of available heap size
		setLimit(Runtime.getRuntime().maxMemory() / 4);
	}

	public void setLimit(long limit) {
		this.limit = limit;
		Log.i(TAG, "MemoryCache will use up to " + this.limit / 1024. / 1024. + "MB");
	}


	public Bitmap get(@Nonnull String key) {
		return cache.get(key);
	}

	public void put(@Nonnull String key, @Nonnull Bitmap value) {
		remove(key);

		cache.put(key, value);
		size += getSizeInBytes(value);

		checkSize();
	}

	private void remove(@Nonnull String key) {
		final Bitmap value = cache.get(key);
		if (value != null) {
			size -= getSizeInBytes(value);
		}
	}

	private void checkSize() {
		Log.i(TAG, "Cache memory usage = " + size + " size = " + cache.size());
		if (size > limit) {
			Log.i(TAG, "Cache cleaning started!");

			// least recently accessed item will be the first one iterated
			for (final Iterator> it = cache.entrySet().iterator(); it.hasNext() && size > limit; ) {
				final Entry entry = it.next();
				size -= getSizeInBytes(entry.getValue());
				it.remove();
			}

			Log.i(TAG, "New cache size " + cache.size());
		}
	}

	public void clear() {
		cache.clear();
	}

	private static long getSizeInBytes(@Nonnull Bitmap bitmap) {
		return bitmap.getRowBytes() * bitmap.getHeight();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy