Created
July 16, 2018 09:42
-
-
Save andrewroberts/acff7f8fd76ca30f9ee69546b682963f to your computer and use it in GitHub Desktop.
Google Apps Script snippet to manage and store persistent state. Uses both Firebase (FirebaseApp library), CacheService and BBLog for logging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 34567890123456789012345678901234567890123456789012345678901234567890123456789 | |
// JSHint - TODO | |
/* jshint asi: true */ | |
(function() {"use strict"})() | |
// Properties_.gs | |
// ============== | |
// | |
// A utility module to manage and store persistent state. | |
// Leverages both Firebase and CacheService. | |
// | |
// All property values are processed as JSON serialized strings. | |
var Properties_ = (function(ns) { | |
ns.SIX_HOURS_IN_SECONDS = 6 * 60 * 60 | |
/** | |
* Retrieve the property from persistent storage | |
* | |
* @param {string} oldKey Key used to retrieve the property | |
*/ | |
ns.getProperty = function(oldKey) { | |
Log_.functionEntryPoint() | |
var newKey = updateKey(oldKey) | |
var cached = Cache_.get(newKey) | |
if (cached === null) { | |
cached = Firebase_.getData(newKey) | |
cached && Cache_.put(newKey, cached, ns.SIX_HOURS_IN_SECONDS) | |
} | |
return cached | |
} // Properties_.getProperty() | |
/** | |
* Puts a property into persistent storage | |
* | |
* @param {string} key Key used to name and store the property | |
* @param {string} value Value of the property | |
* @param {boolean} addToFirebase If true cached values are mirrored in properties object [OPTIONAL, DEFAULT true] | |
*/ | |
ns.setProperty = function(oldKey, value, addToFirebase) { | |
Log_.functionEntryPoint() | |
value = (typeof value === 'string') ? value : JSON.stringify(value) | |
var newKey = updateKey(oldKey) | |
addToFirebase = (typeof addToFirebase === 'undefined') ? true : addToFirebase | |
if (addToFirebase) { | |
Firebase_.setData(newKey, value) | |
} | |
Cache_.remove(newKey) | |
Cache_.put(newKey, value, ns.SIX_HOURS_IN_SECONDS) | |
} // Properties_.setProperty() | |
/** | |
* Removes a property from persistent storage | |
* | |
* @param {string} oldKey Key used to reference a stored property | |
* @param {boolean} removeFromFirebase If true cached values are removed from properties object. | |
*/ | |
ns.deleteProperty = function(oldKey, removeFromFirebase) { | |
Log_.functionEntryPoint() | |
removeFromFirebase = removeFromFirebase || true | |
var newKey = updateKey(oldKey) | |
Cache_.remove(newKey) | |
if (removeFromFirebase) { | |
Firebase_.removeData(newKey) | |
} | |
} // Properties_.deleteProperty() | |
/** | |
* Add the user path to the key | |
* | |
* @param {string} oldKey | |
* | |
* @return {string} newKey | |
*/ | |
function updateKey(oldKey) { | |
Log_.functionEntryPoint() | |
return USERS_KEY_PREFIX + '/' + Utilities.base64EncodeWebSafe(User_) + '/' + oldKey | |
} // Properties_.updateKey() | |
return ns | |
})(Properties_ || {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment