Skip to content

Instantly share code, notes, and snippets.

@blindman2k
Created April 26, 2014 05:14
Show Gist options
  • Save blindman2k/11312315 to your computer and use it in GitHub Desktop.
Save blindman2k/11312315 to your computer and use it in GitHub Desktop.
Agent persistent storage class
class Persist {
cache = null;
// -------------------------------------------------------------------------
function read(key = null, def = null) {
if (cache == null) {
cache = server.load();
}
return (key in cache) ? cache[key] : def;
}
// -------------------------------------------------------------------------
function write(key, value) {
if (cache == null) {
cache = server.load();
}
if (key in cache) {
server.save(cache);
} else {
cache[key] <- value;
server.save(cache);
}
return value;
}
// -------------------------------------------------------------------------
function remove(key) {
if (cache == null) {
cache = server.load();
}
if (key in cache) {
delete cache[key];
server.save(cache);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment