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

jodd.cache.TimedCache Maven / Gradle / Ivy

Go to download

Jodd Core tools and utilities, including type converters, JDateTime, cache etc.

There is a newer version: 5.3.0
Show newest version
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.cache;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Timed cache. Not limited by size, objects are removed only when they are expired.
 * Prune is not invoked explicitly by standard {@link Cache} methods, however,
 * it is possible to schedule prunes on fined-rate delays.
 */
public class TimedCache extends AbstractCacheMap {

	public TimedCache(long timeout) {
		this.cacheSize = 0;
		this.timeout = timeout;
		cacheMap = new HashMap>();
	}

	// ---------------------------------------------------------------- prune

	/**
	 * Prunes expired elements from the cache. Returns the number of removed objects.
	 */
	@Override
	protected int pruneCache() {
        int count = 0;
		Iterator> values = cacheMap.values().iterator();
		while (values.hasNext()) {
			CacheObject co = values.next();
			if (co.isExpired() == true) {
				values.remove();
				count++;
			}
		}
		return count;
	}


	// ---------------------------------------------------------------- auto prune

	protected Timer pruneTimer;

	/**
	 * Schedules prune.
	 */
	public void schedulePrune(long delay) {
		if (pruneTimer != null) {
			pruneTimer.cancel();
		}
		pruneTimer = new Timer();
		pruneTimer.schedule(
				new TimerTask() {
					@Override
					public void run() {
						prune();
					}
				}, delay, delay
		);
	}

	/**
	 * Cancels prune schedules.
	 */
	public void cancelPruneSchedule() {
		if (pruneTimer != null) {
			pruneTimer.cancel();
			pruneTimer = null;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy