Extending prototypes is always a danger in any case - for example Google may introduce a method that conflicts with one of yours. Similar effects can be achieved by simply creating a wrapper for the underlying class and using your wrapper instead of the underlying class. For example, this small sample adds zipping/unzipping, object and key hashing to the CacheService class
function myCache (type, sec) {
/**
* @param {[*]} arguments unspecified number and type of args
* @return {string} a digest of the arguments to use as a key
*/
function keyDigest () {
// convert args to an array and digest them
return Utilities.base64EncodeWebSafe (
Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1,Array.prototype.slice.call(arguments).map(function (d) {
return (Object(d) === d) ? JSON.stringify(d) : d.toString();
}).join("-"),Utilities.Charset.UTF_8));
};
/**
* crush for writing to cache.props
* @param {string} crushThis the string to crush
* @return {string} the b64 zipped version
*/
function crush (crushThis) {
return Utilities.base64Encode(Utilities.zip ([Utilities.newBlob(JSON.stringify(crushThis))]).getBytes());
};
/**
* uncrush for writing to cache.props
* @param {string} crushed the crushed string
* @return {string} the uncrushed string
*/
function uncrush (crushed) {
return Utilities.unzip(Utilities.newBlob(Utilities.base64Decode(crushed),'application/zip'))[0].getDataAsString();
};
// export these
this.sec = sec || 5000;
this.cache =
(type === 'document' && CacheService.getDocumentCache()) ||
(type === 'script' && CacheService.getScriptCache()) ||
(type === 'user' && CacheService.getUserCache())
if(!this.cache) throw new Error('invalid cache type:'+type);
this.write = function (data,keys) {
const args = Array.prototype.slice.call (arguments,1);
this.cache.put(keyDigest.apply(args) , crush(data))
}
this.read = function (keys) {
const args = Array.prototype.slice.call (arguments);
const crushed = this.cache.get(keyDigest.apply(args));
return (crushed && JSON.parse(uncrush(crushed))) || null;
}
}
// examples
function mytest () {
const script = new myCache ('script');
script.write ({a:'x',b:1,c:23}, 'x',1);
const user = new myCache ('user');
user.write ({a:'y',b:100,name:'john'},{user:'smith'});
Logger.log(user.read({user:'smith'}));
Logger.log(script.read('x',1));
}