如何在Google Apps脚本中缓存对象 [英] How can I cache an object in Google Apps scripts

查看:100
本文介绍了如何在Google Apps脚本中缓存对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从JIRA的脚本中获取Google云端硬盘中的电子表格的JSON数据。我有一个脚本可以完成抓取工作,而且我几乎只提取有关问题的数据。我回来的是JSON文本字段,它代表关于该特定JIRA问题的所有数据。

I am fetching JSON data from JIRA in a script for a spreadsheet in Google Drive. I have a script that does the fetch just fine, and I am pretty much only fetching the data for the issue. What I get back is JSON text field representing all the data about that particular JIRA issue.

每次我都想要调用UrlFetch服务时,一个特定的JIRA问题,我想在第一次抓取后缓存抓取的JSON数据。

Instead of making the call to the UrlFetch Service everytime I want one of the fields from a particular JIRA issue, I would like to cache the fetched JSON data after the first fetch.

不幸的是,Google的Google Apps缓存服务只会缓存字符串格式的数据,并将字符串限制为200个字符。从JIRA返回的JSON文本数据远远超过了200个字符。

Unfortunately, Google's Cache service for Google Apps scripts will only cache data in string format, and limits the string to 200 characters. The JSON text data that comes back from JIRA is far in excess of 200 characters.

是否有其他方式缓存JavaScript对象,如解析的JSON字符串或方式在Google Apps脚本中缓存超过200个字符?

Is there an alternate way of caching a javascript object like a parsed JSON string or a way to cache more than 200 characters in Google Apps scripts?

这是在我意识到它存储为字符串之前试图缓存数据的代码:

This is the code where I tried to cache the data before I realized it was stored as a string:

function _fetchJiraData(targetIssue) {
  Logger.log("Started fetching JIRA data for '%s'", targetIssue);

  var data = cache.get(targetIssue);
  if (data == null) {
    var options = {
      headers : {
        Authorization : "Basic " + Utilities.base64Encode('user:password')
      }
    };
    var url = Utilities.formatString(
      'https://rentrak.atlassian.net/rest/api/latest/issue/%s',
      targetIssue
    );
    var result = UrlFetchApp.fetch(url, options);
    var jsonText = result.getContentText();
    data = JSON.parse(jsonText);

    cache.put(targetIssue, data);
  }

  // TODO: validate data
  return data;
};

感谢您的帮助!

Thanks for any help!

推荐答案

Apps脚本缓存服务可以缓存要每键100KB ,您不应该只有200字符有问题。更严格的限制只适用于密钥,即250个字符。

Apps Script Cache Service can cache up to 100KB per key, you should not have an issue with only "200" characters. The tighter limitation is only for the key, which is 250 characters.

我在代码中看到的主要问题是您试图缓存解析的数据,而不是的文字。除非你的钥匙是那么大(在这种情况下,我建议你使用enconding来代替),否则这应该起作用:



The main issue I see with your code is that you're trying to cache the parsed data, instead of the text. Unless your keys are that big (in which case I recommend you used a enconding instead), this should work:

function _fetchJiraData(targetIssue) {
  var jsonText = cache.get(targetIssue);
  if (jsonText === null) {
    //...prepare your fetch
    var result = UrlFetchApp.fetch(url, options);
    jsonText = result.getContentText();
    cache.put(targetIssue, jsonText);
  }

  var data = JSON.parse(jsonText);
  return data;
}

这篇关于如何在Google Apps脚本中缓存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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