
function Cache(obj)
{

	this.obj = obj;
	this.cacheTime = new Date();

	this.getObj = function()
	{
		return this.obj;
	}


}

function RequestCachingMechanism()
{
	this.memory = new Array();
	

	this.createCache = function(key, obj)
	{
		this.memory[key] = new Cache(obj);
	}
	

	// this method should not be accessed from out of this class or its children.	
	this.getCache = function(key)
	{
		return this.memory[key];
	}

	
	this.removeCache = function(key)
	{
		this.memory[key] = null;
	}

	/* this is the one that will be called to retrieve cache */
	this.retrieve = function(key)
	{
	
	}
}


/* this mechanism simply returns the previous cache added (without checking any constraints.) */ 
CompleteCachingMechanism.prototype = new RequestCachingMechanism();
function CompleteCachingMechanism()
{
	this.retrieve = function(key)
	{
	
		var obj = null;
		var cache = this.getCache(key);
		if(cache != null)
			obj = cache.getObj();

		return obj;
	}

}


/* has time limit and flush option. */
RestrictedCachingMechanism.prototype = new RequestCachingMechanism();

function RestrictedCachingMechanism()
{

	this.retrieve = function(key)
	{
		var obj = null;
		var cache = this.getCache(key);
		if(cache != null)
			obj = cache.getObj();

		return obj;
	}
	
	// deletes all the cache.
	this.flush = function()
	{
		this.memory = null;
		this.memory = new Array();
	}

	this.setDelay = function(delay)
	{
		var instance = this;
		var expireCheckCallback = function()
		{	  
			var now = (new Date()).getTime();
			for(var key in instance.memory)
			{
				var cache = instance.memory[key];

				if(cache != null && cache.getObj() != null)
				{
					if( (cache.cacheTime.getTime() + delay ) < now)	
					{
						instance.removeCache(key);
					}
				}
			}
		}
		
		// check whether there is expired cache every 1 minute.
		window.setInterval( expireCheckCallback, 60000);
	}

}


