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

org.apache.el.util.ConcurrentCache Maven / Gradle / Ivy

There is a newer version: 6.0.0beta12
Show newest version
package org.apache.el.util;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

public final class ConcurrentCache {

	private final int size;
	
	private final Map eden;
	
	private final Map longterm;
	
	public ConcurrentCache(int size) {
		this.size = size;
		this.eden = new ConcurrentHashMap(size);
		this.longterm = new WeakHashMap(size);
	}
	
	public V get(K k) {
		V v = this.eden.get(k);
		if (v == null) {
			v = this.longterm.get(k);
			if (v != null) {
				this.eden.put(k, v);
			}
		}
		return v;
	}
	
	public void put(K k, V v) {
		if (this.eden.size() >= size) {
			this.longterm.putAll(this.eden);
			this.eden.clear();
		}
		this.eden.put(k, v);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy