CacheService.put全部忽略键超过某个值 [英] CacheService.putAll ignoring keys over a certain value

查看:108
本文介绍了CacheService.put全部忽略键超过某个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

putAll()方法有问题谷歌缓存的脚本.该方法似乎有效,但仅适用于前(〜100)对.

I have a problem with putAll() method of google cache for script. The method seems working but only with first (~100) pairs

在我的脚本中,我必须缓存大量的行(〜2500 * 3),每行使用不同的键,,然后发现缓存服务出现故障.为了找出这个问题我写了一个的简单代码

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...

谢谢

推荐答案

这个答案怎么样?我认为可能有几种方法.因此,请将此视为其中之一.

How about this answer? I think that there might be several approaches. So please think of this as one of them.

根据您的情况,当i为100时,a返回null.至此,for循环结束.使用以下脚本时,发现CacheService中的属性超过100个.

In your situation, when i is 100, a returns null. 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.

var cache = CacheService.getScriptCache();
var toCache = {};
var keys = [];
for (var i = 0; i < 2000; i++) {
  var key = "key" + i;
  keys.push(key);
  toCache[key] = "value" + i;
}
cache.putAll(toCache);
var r = cache.getAll(keys);

Logger.log(keys.length) // 2000
Logger.log(Object.keys(r).length) // 900

但是还发现,尽管推送了2,000个属性,但仅检索了900个属性.而且我找不到丢失的属性的规律性.从这个结果来看,我认为可能存在最多可以推送的属性.

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.

作为一个实验,我调查了检索到的属性的数量随推送属性的数量的增加而增加的情况.每个属性都被立即推送.下图是结果.

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.

根据该图,对于上面的脚本,发现900个属性是一次可以推送的最大属性数.这不取决于小于100 KB的值的大小.另外,可以获得以下几点.

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.

  • 当将属性从1扩展到900时,可以正确检索所有属性.
  • 当推送1,000个属性时,将检索1-900个属性. 900之后的属性无法检索.认为这些属性没有被推送.
  • 当推送2,000个属性时,将检索900个属性.但是我找不到它们的规律性.
  • 使用键名"keyA1,keyA2,keyA3,"推送900个属性后,使用键名"keyB1,keyB2,keyB3,..."推送100个属性时,可以正确检索1,000个属性.
    • 但是,使用键名"keyA1,keyA2,keyA3,"推入900个属性后,使用键名"keyB1,keyB2,keyB3,……"推入101个属性时,仅检索到900个属性.在这种情况下,"keyA"的属性被"keyB"的101个属性覆盖.
    • When the properties from 1 to 900 are pushed, all properties can be correctly retrieved.
    • When 1,000 properties are pushed, 1-900 properties are retrieved. Properties after 900 cannot be retrieved. It is considered that those properties are not pushed.
    • When 2,000 properties are pushed, 900 properties are retrieved. But I couldn't find the regularity of them.
    • After 900 properties were pushed using key names of "keyA1, keyA2, keyA3,,,", when 100 properties are pushed using key names of "keyB1, keyB2, keyB3,,,", 1,000 properties can be correctly retrieved.
      • But after 900 properties were pushed using key names of "keyA1, keyA2, keyA3,,,", when 101 properties are pushed using key names of "keyB1, keyB2, keyB3,,,", only 900 properties are retrieved. In this case, the properties of "keyA" were overwritten by 101 properties of "keyB".

      这些结果与CacheService.getScriptCache()CacheService.getUserCache()CacheService.getDocumentCache()相同.但是CacheService.getScriptCache()CacheService.getUserCache()CacheService.getDocumentCache()的每种方法都可以用作单独的存储.因此,当使用3种方法时,可以使用3,000个属性.

      These results were the same with CacheService.getScriptCache(), CacheService.getUserCache() and CacheService.getDocumentCache(). But each method of CacheService.getScriptCache(), CacheService.getUserCache() and CacheService.getDocumentCache() can be used as the individual storage. So when 3 methods are used, 3,000 properties can be used.

      1. 一次最多可以推送900个属性.
      2. 在添加900个属性后添加100个属性时,可以保存1,000个属性.
        • 认为这是最大数量.
      1. Maximum number of properties which can be pushed once is 900.
      2. When the properties are added 100 properties after 900 properties were pushed, 1,000 properties can be saved.
        • It is considered that this is the maximum number.
      • 使用CacheService.getScriptCache()CacheService.getUserCache()CacheService.getDocumentCache()时,可以使用3,000个属性.
      • When CacheService.getScriptCache(), CacheService.getUserCache() and CacheService.getDocumentCache() are used, 3,000 properties can be used.

      解决方法:

      根据以上结果,当您按下〜2500 * 3"的属性时,以下解决方法如何?

      Workaround:

      From above results, when you push the properties of "~2500 * 3", how about the following workaround?

      • 将多个值放入一个属性.
        • Put several values to one property.
          • The maximum size of a property is 100 kBytes.
          • 到此,可以使用3,000个属性.

          我认为可以结合使用这些属性将〜2500 * 3"的属性推入CacheService.

          I think that the properties of "~2500 * 3" can be pushed to CacheService by combination of these.

          • 使用CacheService.getDocumentCache()时,请使用容器绑定脚本.
          • 在此报告中,我确认了自己的帐户.我不确定其他用户的所有结果是否相同.对于这种情况,我感到抱歉.
          • When you use CacheService.getDocumentCache(), please use the container-bound script.
          • In this report, I confirmed with my account. I'm not sure whether all results of other users are the same. I'm sorry for this situation.

          如果已经报告了上述结果,并且/或者这不是您想要的,

          If above results has already been reported and/or this was not what you want, I'm sorry.

          这篇关于CacheService.put全部忽略键超过某个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆