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

org.xson.web.cache.FIFOCache Maven / Gradle / Ivy

Go to download

xco-web is an easy to use control layer framework, is part of the SOA system, using xml language to describe the controller.

The newest version!
package org.xson.web.cache;

import java.util.LinkedList;

public class FIFOCache extends AbstractCache {

	private final ICache		delegate;
	private LinkedList	keyList;
	private int					size;

	// public void setSize(int size) {
	// this.size = size;
	// }

	public FIFOCache(ICache delegate, int size) {
		this.delegate = delegate;
		this.keyList = new LinkedList();
		this.size = size;
		// this.size = 1024;
	}

	@Override
	public String getId() {
		return delegate.getId();
	}

	@Override
	public int getSize() {
		return delegate.getSize();
	}

	// @Override
	// public void putObject(Object key, Object value) {
	// cycleKeyList(key);
	// delegate.putObject(key, value);
	// }

	@Override
	public void putObject(Object key, Object value, Integer time) {
		cycleKeyList(key);
		delegate.putObject(key, value);
	}

	@Override
	public Object getObject(Object key) {
		return delegate.getObject(key);
	}

	@Override
	public Object removeObject(Object key) {
		return delegate.removeObject(key);
	}

	@Override
	public void clear() {
		delegate.clear();
		keyList.clear();
	}

	private void cycleKeyList(Object key) {
		keyList.addLast(key);
		if (keyList.size() > size) {
			Object oldestKey = keyList.removeFirst();
			delegate.removeObject(oldestKey);
		}
	}
}