Skip to content

Instantly share code, notes, and snippets.

@wangye
Created September 26, 2012 06:13
Show Gist options
  • Save wangye/3786380 to your computer and use it in GitHub Desktop.
Save wangye/3786380 to your computer and use it in GitHub Desktop.
JScript TinyCache for Classic ASP
// Author : wangye
// For more information please visit http://wangye.org/blog/archives/706/
var RawCache = function() {
this.setItem = function(key, value) {
Application.Lock();
Application.Contents(key) = value;
Application.Unlock();
};
this.getItem = function(key, defvalue) {
value = Application.Contents(key);
return typeof(value) != 'undefined' ? value : defvalue;
};
this.remove = function(key) {
Application.Lock();
Application.Contents.Remove(key);
Application.Unlock();
};
this.isAvailable = function() {
return true;
};
}
var TinyCache = function(name, useStaticObjects) {
if (useStaticObjects) {
this.cache = Application.StaticObjects(name);
}
if (!this.cache) {
this.raw_cache = new RawCache();
this.cache = this.raw_cache.getItem(name, null);
if (this.cache != null) {
this.cache = this.cache.getItem(0);
}
if (!this.cache || typeof this.cache != "object") {
this.mem = Server.CreateObject("Scripting.Dictionary");
var msscript = Server.CreateObject("MSScriptControl.ScriptControl");
msscript.Language = "VBScript";
msscript.AllowUI = false;
msscript.AddObject("SafeVBArrayValue", this.mem);
this.raw_cache.setItem(name, msscript.Eval("Array(SafeVBArrayValue)"));
this.cache = this.raw_cache.getItem(name, null).getItem(0);
}
}
this.setItem = function(key, value) {
if (this.cache.Exists(key)) {
var prevalue = this.cache.Item(key);
this.cache.Item(key) = value;
return prevalue;
} else {
this.cache.Add(key, value);
return null;
}
};
this.getItem = function(key, defvalue) {
if (this.cache.Exists(key)) {
return this.cache.Item(key);
} else {
return defvalue;
}
};
this.remove = function(key) {
if (this.cache.Exists(key)) {
this.cache.Remove(key);
}
};
this.contains = function(key) {
return this.cache.Exists(key);
};
this.isAvailable = function() {
return true;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment