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

Question about caching, fetch vs alfy.fetch #174

Open
kalaschnik opened this issue Dec 6, 2023 · 1 comment
Open

Question about caching, fetch vs alfy.fetch #174

kalaschnik opened this issue Dec 6, 2023 · 1 comment

Comments

@kalaschnik
Copy link
Contributor

Hey! I am writing my first Alfred workflow, and I have a general question about caching;

(1) How do I see if something I fetched gets cached?
(2) Where is it stored?
(3) Does my second execution and all subsequent executions of alfy.fetch use the cache?
(3a) How can I very that?
(4) The main difference between alfy.fetch and native fetch is that alfy.fetch does caching + json parse?

@LitoMore
Copy link
Sponsor Collaborator

LitoMore commented Feb 24, 2024

  1. How do I see if something I fetched gets cached?

You can't see the cached status. Code is here:

alfy/index.js

Lines 115 to 173 in 3d277f8

alfy.fetch = async (url, options) => {
options = {
resolveBodyOnly: true,
...options,
};
// TODO: Remove this in 2024.
if (options.query) {
throw new Error('The `query` option was renamed to `searchParams`.');
}
if (typeof url !== 'string') {
throw new TypeError(`Expected \`url\` to be a \`string\`, got \`${typeof url}\``);
}
if (options.transform && typeof options.transform !== 'function') {
throw new TypeError(`Expected \`transform\` to be a \`function\`, got \`${typeof options.transform}\``);
}
const rawKey = url + JSON.stringify(options);
// This must be below the cache key generation.
const {transform, maxAge} = options;
delete options.transform;
delete options.maxAge;
const key = rawKey.replaceAll('.', '\\.');
const cachedResponse = alfy.cache.get(key, {ignoreMaxAge: true});
if (cachedResponse && !alfy.cache.isExpired(key)) {
return cachedResponse;
}
if ('json' in options && options.json === false) {
delete options.json;
options.responseType = 'text';
} else {
options.responseType = 'json';
}
let response;
try {
response = await got(url, options);
} catch (error) {
if (cachedResponse) {
return cachedResponse;
}
throw error;
}
const data = transform ? transform(response) : response;
if (maxAge) {
alfy.cache.set(key, data, {maxAge});
}
return data;
};

  1. Where is it stored?

The Alfred workflow_cache. See https://www.alfredapp.com/help/workflows/script-environment-variables/.

  1. Does my second execution and all subsequent executions of alfy.fetch use the cache?

The cache max-age is 5000 ms. You can catch the HTTP traffic to see if there are any requests.

  1. The main difference between alfy.fetch and native fetch is that alfy.fetch does caching + json parse?

Alfy is using https://github.com/sindresorhus/got. And we implemented the cache logic by ourself inside Alfy. You can check our source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants