ali-sdk / ali-mc

Aliyun OCS(open cache storage) client. (Just a Memcache client)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

使用 mc.get 的时候出现 heap out of memory 错误

whxaxes opened this issue · comments

如图:
image

缓存的数据是一大段 json 字符串,大小为 5KB ,使用了 yield mc.get 就会抛出以上错误,如果不使用 yield 就不会抛出异常,能正常取到数据。

具体的使用代码是怎样的?

你截图明显是内存爆了。

extend context

mcCache(key, handle, time = 5 * 60000) {
    if (typeof key !== 'string') {
      throw new Error('key must be string');
    }

    key = key.replace(/\s/g, '_'); // 替换非法字符
    return co(this.mc.get(key)).then(res => {
      if (res) {
        this.app.logger.debug(`${key} has cache: ${res}`);
        return Promise.resolve(res);
      } else {
        return Promise.reject();
      }
    }).catch(() => {
      return co.apply(this, [handle]).then(res => {
        if (res && (!res.error || !_.isError(res.error))) {
          try {
            const resString = (typeof res === 'object') ? JSON.stringify(res) : res;
            this.mcSet(key, resString + '', time);
          } catch (e) {
            this.app.logger.error(e);
          }
        }

        return Promise.resolve(res);
      });
    });
  },

router

const result = yield this.mcCache(
    `pinyin_${pinyin}`,
    function* test() {
      const result = yield this.proxy.xxx.getXXX({
        args: pinyin,
      });

      return result;
    }
  );

返回的数据量大小就是一个接口响应的 json 数据,数据量为 5KB。

把coredump文件发给我 帮你分析下吧。这样看代码 不行的。

经测试内容在3K以上都会出现这个问题