/*MIT LICENSECopyright (c) 2007 Monsur Hossain (http://www.monsur.com)Permission is hereby granted, free of charge, to any personobtaining a copy of this software and associated documentationfiles (the "Software"), to deal in the Software withoutrestriction, including without limitation the rights to use,copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom theSoftware is furnished to do so, subject to the followingconditions:The above copyright notice and this permission notice shall beincluded in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIESOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHTHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISINGFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.*/
var CachePriority={Low:1,Normal:2,High:4};function Cache(maxSize){this.items={};this.count=0;if(maxSize==null)
maxSize=-1;this.maxSize=maxSize;this.fillFactor=.75;this.purgeSize=Math.round(this.maxSize*this.fillFactor);this.stats={};this.stats.hits=0;this.stats.misses=0;}
Cache.prototype.getItem=function(key){var item=this.items[key];if(item!=null){if(!this._isExpired(item)){item.lastAccessed=new Date().getTime();}else{this._removeItem(key);item=null;}}
var returnVal=null;if(item!=null){returnVal=item.value;this.stats.hits++;}else{this.stats.misses++;}
return returnVal;};Cache.prototype.setItem=function(key,value,options){function CacheItem(k,v,o){if((k==null)||(k==''))
throw new Error("key cannot be null or empty");this.key=k;this.value=v;if(o==null)
o={};if(o.expirationAbsolute!=null)
o.expirationAbsolute=o.expirationAbsolute.getTime();if(o.priority==null)
o.priority=CachePriority.Normal;this.options=o;this.lastAccessed=new Date().getTime();}
if(this.items[key]!=null)
this._removeItem(key);this._addItem(new CacheItem(key,value,options));if((this.maxSize>0)&&(this.count>this.maxSize)){this._purge();}};Cache.prototype.clear=function(){for(var key in this.items){this._removeItem(key);}};Cache.prototype._purge=function(){var tmparray=new Array();for(var key in this.items){var item=this.items[key];if(this._isExpired(item)){this._removeItem(key);}else{tmparray.push(item);}}
if(tmparray.length>this.purgeSize){tmparray=tmparray.sort(function(a,b){if(a.options.priority!=b.options.priority){return b.options.priority-a.options.priority;}else{return b.lastAccessed-a.lastAccessed;}});while(tmparray.length>this.purgeSize){var ritem=tmparray.pop();this._removeItem(ritem.key);}}};Cache.prototype._addItem=function(item){this.items[item.key]=item;this.count++;};Cache.prototype._removeItem=function(key){var item=this.items[key];delete this.items[key];this.count--;if(item.options.callback!=null){var callback=function(){item.options.callback(item.key,item.value);};setTimeout(callback,0);}};Cache.prototype._isExpired=function(item){var now=new Date().getTime();var expired=false;if((item.options.expirationAbsolute)&&(item.options.expirationAbsolute<now)){expired=true;}
if(!expired&&(item.options.expirationSliding)){var lastAccess=item.lastAccessed+(item.options.expirationSliding*1000);if(lastAccess<now){expired=true;}}
return expired;};Cache.prototype.toHtmlString=function(){var returnStr=this.count+" item(s) in cache<br /><ul>";for(var key in this.items){var item=this.items[key];returnStr=returnStr+"<li>"+item.key.toString()+" = "+item.value.toString()+"</li>";}
returnStr=returnStr+"</ul>";return returnStr;};
