|
| 1 | +/** |
| 2 | + * The fork of |
| 3 | + * https://gist.github.com/pilbot/9d0567ef1daf556449fb, |
| 4 | + * https://gist.github.com/glade-at-gigwell/4e080771d685fbf1908edbd98eb2d88c |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Using the Google Apps Script Cache Service for objects above 100Kb |
| 9 | + */ |
| 10 | +class ChunkyCache { |
| 11 | + /** |
| 12 | + * |
| 13 | + * @param {GoogleAppsScript.Cache.Cache} cache |
| 14 | + */ |
| 15 | + constructor(cache) { |
| 16 | + this.cache = cache || CacheService.getScriptCache(); |
| 17 | + this.chunkSize = 100 * 1024; |
| 18 | + } |
| 19 | + /** |
| 20 | + * Gets the cached value for the given key, or null if none is found. |
| 21 | + * https://developers.google.com/apps-script/reference/cache/cache#getkey |
| 22 | + * |
| 23 | + * @param {string} key |
| 24 | + * @returns {any} A JSON.parse result |
| 25 | + */ |
| 26 | + get(key) { |
| 27 | + const superKeyjson = this.cache.get(key); |
| 28 | + if (superKeyjson === null) return null; |
| 29 | + const superKey = JSON.parse(superKeyjson); |
| 30 | + const cache = this.cache.getAll(superKey.chunks); |
| 31 | + const chunks = superKey.chunks.map((key) => cache[key]); |
| 32 | + if ( |
| 33 | + chunks.every(function (c) { |
| 34 | + return c !== undefined; |
| 35 | + }) |
| 36 | + ) { |
| 37 | + return JSON.parse(chunks.join('')); |
| 38 | + } |
| 39 | + } |
| 40 | + /** |
| 41 | + * Adds a key/value pair to the cache. |
| 42 | + * https://developers.google.com/apps-script/reference/cache/cache#putkey,-value |
| 43 | + * |
| 44 | + * @param {string} key |
| 45 | + * @param {string} value |
| 46 | + * @param {number} expirationInSeconds |
| 47 | + */ |
| 48 | + put(key, value, expirationInSeconds = 600) { |
| 49 | + const json = JSON.stringify(value); |
| 50 | + const chunks = []; |
| 51 | + const chunkValues = {}; |
| 52 | + let index = 0; |
| 53 | + while (index < json.length) { |
| 54 | + const chunkKey = key + '_' + index; |
| 55 | + chunks.push(chunkKey); |
| 56 | + chunkValues[chunkKey] = json.substr(index, this.chunkSize); |
| 57 | + index += this.chunkSize; |
| 58 | + } |
| 59 | + const superKey = { |
| 60 | + chunkSize: this.chunkSize, |
| 61 | + chunks: chunks, |
| 62 | + length: json.length, |
| 63 | + }; |
| 64 | + chunkValues[key] = JSON.stringify(superKey); |
| 65 | + this.cache.putAll(chunkValues, expirationInSeconds); |
| 66 | + } |
| 67 | + /** |
| 68 | + * Removes an entry from the cache using the given key. |
| 69 | + * |
| 70 | + * @returns {null} |
| 71 | + */ |
| 72 | + remove(key) { |
| 73 | + const superKeyjson = this.cache.get(key); |
| 74 | + if (superKeyjson !== null) { |
| 75 | + const superKey = JSON.parse(superKeyjson); |
| 76 | + this.cache.removeAll([...superKey.chunks, key]); |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Using the Google Apps Script Cache Service for blobs |
| 84 | + */ |
| 85 | +class BlobCache extends ChunkyCache { |
| 86 | + /** |
| 87 | + * Extends of ChunkyCache |
| 88 | + */ |
| 89 | + constructor(cache) { |
| 90 | + super(cache); |
| 91 | + this.splitter = '1c16c2eb-a4a7-4cac-bf79-064cedfbb346'; |
| 92 | + this.defaultName = '772fff0c-4207-4893-834c-aec73c498eeb'; |
| 93 | + this.prefixSize = 250; |
| 94 | + } |
| 95 | + /** |
| 96 | + * Adds a key/blob pair to the cache. |
| 97 | + * |
| 98 | + * @param {string} key |
| 99 | + * @param {GoogleAppsScript.Base.Blob | GoogleAppsScript.Base.BlobSource} blob |
| 100 | + * @param {number} expirationInSeconds |
| 101 | + */ |
| 102 | + putBlob(key, blob, expirationInSeconds = 600) { |
| 103 | + let name = blob.getName(); |
| 104 | + if (name === null) name = this.defaultName; |
| 105 | + const contentType = blob.getContentType(); |
| 106 | + const prefix = [name, this.splitter, contentType, this.splitter] |
| 107 | + .join('') |
| 108 | + .padEnd(this.prefixSize, ' '); |
| 109 | + const data = prefix + Utilities.base64Encode(blob.getBytes()); |
| 110 | + this.put(key, data, expirationInSeconds); |
| 111 | + } |
| 112 | + /** |
| 113 | + * Gets the cached blob for the given key, or null if none is found. |
| 114 | + * |
| 115 | + * @param {*} key |
| 116 | + */ |
| 117 | + getBlob(key) { |
| 118 | + const data = this.get(key); |
| 119 | + if (data !== null) { |
| 120 | + const prefix = data.slice(0, this.prefixSize).split(this.splitter); |
| 121 | + const blob = Utilities.newBlob(''); |
| 122 | + blob.setBytes(Utilities.base64Decode(data.slice(this.prefixSize))); |
| 123 | + if (prefix[0] !== this.defaultName) blob.setName(prefix[0]); |
| 124 | + blob.setContentType(prefix[2]); |
| 125 | + return blob; |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | +} |
0 commit comments