Google Apps Script is a powerful tool that allows developers to automate various tasks, create custom applications, and integrate with other Google services. One of the features provided by Google Apps Script is caching, which can be used to improve the performance of a script by storing frequently accessed data in memory. However, some developers have reported issues with Google Apps Script commands not working with cache. In this blog post, we will explore the possible reasons for this issue and provide some troubleshooting tips.

Cache limitations

The first thing to check when Google Apps Script commands are not working with cache is whether the data being cached exceeds the cache limitations. According to the Google Apps Script documentation, the maximum cache size is 100KB per user per script. If the cached data exceeds this limit, the script will not be able to access the cache and the commands will fail.

To check the size of the cache, we can use the CacheService.getScriptCache() method, which returns a Cache object. We can then use the getLimit() method of the Cache object to get the maximum size of the cache. Here is an example code snippet:

function checkCacheSize() {
    var scriptCache = CacheService.getScriptCache();
    var limit = scriptCache.getLimit();
    Logger.log("Cache limit: " + limit + " bytes");
}

If the size of the cached data is close to the limit, we may need to consider using a different caching strategy, such as storing the data in a Google Sheet or a database.

Cache eviction

Another possible reason for Google Apps Script commands not working with cache is cache eviction. The CacheService may evict data from the cache if it determines that the memory is needed for other purposes. This can happen even if the cache has not reached its size limit.

To prevent data loss due to cache eviction, we can use the Cache.put(key, value, expirationInSeconds) method to set an expiration time for the cached data. This ensures that the data will be automatically evicted from the cache after a certain period of time, regardless of whether the cache limit has been reached or not. Here is an example code snippet:

function putDataInCache() {
    var scriptCache = CacheService.getScriptCache();
    var data = "Hello, world!";
    scriptCache.put("myData", data, 3600); // Cache for 1 hour
}

In this example, the data will be cached for one hour (3600 seconds) before it is automatically evicted.

Cache key conflicts

A third possible reason for Google Apps Script commands not working with cache is cache key conflicts. If we use the same key to store different data in the cache, the previous data will be overwritten and may cause unexpected results.

To avoid cache key conflicts, we can use a unique key for each data item. We can generate a unique key using a combination of a timestamp and a random number. Here is an example code snippet:

function putDataInCache() {
    var scriptCache = CacheService.getScriptCache();
    var data = "Hello, world!";
    var key = "myData_" + new Date().getTime() + "_" + Math.random();
    scriptCache.put(key, data);
}

In this example, the key is generated using the current timestamp and a random number, which ensures that it is unique for each data item.