I have a problem with putAll() method of google cache for script. The method seems working but only with first (~100) pairs
In my script i have to cache a big number of rows (~2500 * 3), each row in a different key, and I found malfunction in cache service. To identify the problem I wrote a simple code
function myFunction() {
var cache = CacheService.getScriptCache();
var toCache = {};
for (var i = 0; i < 2000; i++){
toCache["key"+i] = "value"+i;
}
cache.putAll(toCache);
var tmp;
for (var i = 0; i < 2000; i+=10){
var a = cache.get("key"+i);
if (a == null) { tmp = "key"+i; break; }
}
tmp = tmp;
}
As in the past the cache service seems to be unpredictable, and does not handle errors during processing. Putting cache with multiple calls is slower, and does not ensure that the limit will not change in the future, since it's not even mentioned in the documentation...
Thanks
How about this answer? I think that there might be several approaches. So please think of this as one of them.
In your situation, when
i
is 100,a
returnsnull
. By this, the for loop is finished. When the following script is used, it is found that there are the properties more than 100 in the CacheService.But also it is found that although 2,000 properties are pushed, only 900 properties are retrieved. And I couldn't find the regularity of the lost properties. From this result, I thought that there might be the maximum number of properties which can be pushed.
Experiment:
As an experiment, I investigated the number of retrieved properties with the increase of the number of pushed properties. Each property was pushed at once. Following figure is the result.
From this figure, for above script, it is found that 900 properties is the maximum number of properties which can be pushed at once. This didn't depend on the size of values less than 100 kBytes. Also the following points could be obtained.
These results were the same with
CacheService.getScriptCache()
,CacheService.getUserCache()
andCacheService.getDocumentCache()
. But each method ofCacheService.getScriptCache()
,CacheService.getUserCache()
andCacheService.getDocumentCache()
can be used as the individual storage. So when 3 methods are used, 3,000 properties can be used.Summary:
CacheService.getScriptCache()
,CacheService.getUserCache()
andCacheService.getDocumentCache()
independent.CacheService.getScriptCache()
,CacheService.getUserCache()
andCacheService.getDocumentCache()
are used, 3,000 properties can be used.Workaround:
From above results, when you push the properties of "~2500 * 3", how about the following workaround?
CacheService.getScriptCache()
,CacheService.getUserCache()
andCacheService.getDocumentCache()
.I think that the properties of "~2500 * 3" can be pushed to CacheService by combination of these.
Note:
CacheService.getDocumentCache()
, please use the container-bound script.Reference:
If above results has already been reported and/or this was not what you want, I'm sorry.