Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache isn't working #873

Closed
2 tasks done
yovanoc opened this issue Sep 16, 2019 · 16 comments
Closed
2 tasks done

Cache isn't working #873

yovanoc opened this issue Sep 16, 2019 · 16 comments
Labels
question The issue is a question about Got

Comments

@yovanoc
Copy link

yovanoc commented Sep 16, 2019

Describe the bug

  • Node.js version: 12.10
  • OS & version: MacOS Catalina

Actual behavior

When I put a cache system it does not work. I'm currently using master branch, and different module like keyv-file and the file is never saved.

Expected behavior

The file is here and used in case of subsequent request.

Code to reproduce

image
image

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak
Copy link
Collaborator

szmarczak commented Sep 16, 2019

Please, don't kill us. We won't rewrite your code character by character. Copy and paste it here, otherwise the issue will be closed.

@yovanoc
Copy link
Author

yovanoc commented Sep 16, 2019

import DTConstants from "@/protocol/DTConstants";
import { stringify } from "querystring";
import { Languages } from "@/configurations/language/Languages";
import got from "got";
import { appDataPath } from "@lazyspace/common";
import KeyvFile from "keyv-file";
import { join } from "path";
import { Data } from "./data";

const cache = new KeyvFile({
  filename: join(appDataPath, "data.json"), // the file path to store the data
  expiredCheckDelay: 10 * 24 * 3600 * 1000, // ms, check and remove expired data in each ms
  writeDelay: 120, // ms, batch write to disk in a specific duration, enhance write performance.
  encode: JSON.stringify, // serialize function
  decode: JSON.parse // deserialize function
});

export interface RequestData {
  lang?: Languages;
  id?: string | number;
  ids?: (string | number)[];
  class?: keyof Data;
}

export async function request<T extends unknown>(
  serverPath: string,
  path: string,
  data: RequestData,
  defaultLang: Languages
): Promise<T> {
  const query = {
    lang: data.lang || defaultLang,
    v: DTConstants.buildVersion
  };

  const req = serverPath + path + "?" + stringify(query);

  const { body } = await got.post(req, {
    cache,
    retry: 4,
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(data),
    responseType: "json"
  });

  return body;
}

Sorry for that

@szmarczak
Copy link
Collaborator

What URL were you trying to cache?

@yovanoc
Copy link
Author

yovanoc commented Sep 16, 2019

@szmarczak
Copy link
Collaborator

szmarczak commented Sep 16, 2019

I get ERR_CONNECTION_REFUSED, could you please link something that should work without a proxy?

@yovanoc
Copy link
Author

yovanoc commented Sep 16, 2019

A simple POST request with the Content-Type header with this in body:

{
	"class": "Pets",
	"ids": []
}

@szmarczak
Copy link
Collaborator

Is this specific for keyv-file? Does it work with just keyv?

@szmarczak szmarczak added the question The issue is a question about Got label Sep 20, 2019
@yovanoc
Copy link
Author

yovanoc commented Sep 21, 2019

It doesn't work either

@szmarczak
Copy link
Collaborator

So the method is POST, and what's the status code?

@yovanoc
Copy link
Author

yovanoc commented Sep 21, 2019

200

@yovanoc
Copy link
Author

yovanoc commented Sep 21, 2019

In fact, even with a new Map() as a cache, and a console.log(cache) after each request it doesn't seem to work too

image

@szmarczak
Copy link
Collaborator

And what are the response headers?

@yovanoc
Copy link
Author

yovanoc commented Sep 21, 2019

image

@szmarczak
Copy link
Collaborator

I don't see any outstanding behavior. It works as expected. https://httpwg.org/specs/rfc7234.html#rfc.section.4.4

A cache MUST invalidate the effective Request URI (Section 5.5 of [RFC7230]) as well as the URI(s) in the Location and Content-Location response header fields (if present) when a non-error status code is received in response to an unsafe request method.

POST is one of the unsafe methods.

@szmarczak
Copy link
Collaborator

I think what you're looking for is #875

@yovanoc
Copy link
Author

yovanoc commented Sep 21, 2019

Oh I was thinking it would store the response body in the cache if the request doesn't change. Like params etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question The issue is a question about Got
Projects
None yet
Development

No branches or pull requests

2 participants