Skip to content

Instantly share code, notes, and snippets.

@Bajena
Created June 24, 2018 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bajena/bcc8dca0d43fc004dc30e1a0b56f8d10 to your computer and use it in GitHub Desktop.
Save Bajena/bcc8dca0d43fc004dc30e1a0b56f8d10 to your computer and use it in GitHub Desktop.
Simple cache for GDS
/**
* Constructor for DataCache.
* More info on caching: https://developers.google.com/apps-script/reference/cache/cache
*
* @param {object} cacheService - GDS caching service
* @param {Date} startDate - beggining of GDS request interval
* @param {Date} endDate - end of GDS request interval
*
* @return {object} DataCache.
*/
function DataCache(cacheService, startDate, endDate) {
this.service = cacheService;
this.cacheKey = this.buildCacheKey(startDate, endDate);
return this;
}
/** @const - 6 hours, Google's max */
DataCache.REQUEST_CACHING_TIME = 21600;
/**
* Builds a cache key for given GDS request
*
* @return {String} cache key
*/
DataCache.prototype.buildCacheKey = function(startDate, endDate) {
return startDate + '_' + endDate;
};
/**
* Gets stored value
*
* @return {String}
*/
DataCache.prototype.get = function() {
return this.service.get(this.cacheKey);
};
/**
* Stores value in cache.
*
* @param {String} key - cache key
* @param {String} value
*/
DataCache.prototype.set = function(value) {
this.service.put(this.cacheKey, value);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment