Refresh applicationScope variables with individual timeout


package de.eknori;

import static com.ibm.xsp.extlib.util.ExtLibUtil.getApplicationScope;

import java.util.Date;
import java.util.Map;

import lotus.domino.NotesException;
public class Tools {
	private boolean debugMode = true;

	public Tools() {}

	public boolean refreshCache(String key, long cacheInterval) throws NotesException{
		Map<String, Object> applicationScope = getApplicationScope();
		Long currentTime  = new Date().getTime();
		if (!applicationScope.containsKey(key + "_time")){
			applicationScope.put(key + "_time", currentTime);
			if (this.isDebugMode()) {
				System.out.println("refreshCache(): -- Initializing --");
			}
			return true;
		}
		long diffInSecs  = currentTime - (Long)applicationScope.get(key + "_time")  ;
		if (this.isDebugMode()) {
			System.out.println("refreshCache(): diffInSecs: " + diffInSecs);
		}
		if (diffInSecs < cacheInterval * 1000) {
			if (this.isDebugMode()) {
				System.out.println("refreshCache(): cache still valid");
			}
			return false;
		} else {
			applicationScope.put(key + "_time", currentTime);
			if (this.isDebugMode()) {
				System.out.println("refreshCache(): needs to be refreshed after " + cacheInterval + " seconds");
			}
			return true;
		}
	}

	public boolean isDebugMode() {
		return this.debugMode;
	}

	public void setDebugMode(final boolean debugMode) {
		this.debugMode = debugMode;
	}
}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...