alltherooms / cached-request

Node.js module to perform HTTP requests with caching support
MIT License
61 stars 23 forks source link

Cached not deleting. #35

Open jhemmmm opened 4 years ago

jhemmmm commented 4 years ago
var gRequest = cachedRequest({
            url: photoFetch.data.results,
            ttl: 1000 * 60 * 10 //1 minutes
        }).on("error", function(err) {
            console.log(err);
        });
        gRequest.pipe(res);

Cached file does not seem to be deleting automatically. I am doing it wrong?.

elhoyos commented 4 years ago

Hi @jhemmmm, your code seems ok to me. Notice that at this moment cached-request does not have such a functionality. The cached entries are not removed automatically after they expire, but the request will be made as expected.

We have not considered adding an automated cleanup of expired cache entries, but it sounds useful to have it when setting your cache directory. Something like

cachedRequest.setCacheDirectory(cacheDirectory, {
  cleanupInterval: 24 * 60 * 60 * 1000 // cleanup expired items every 24 hours
});

If you feel like wanting to contribute, feel free to open up a pull request. I would be happy to help you.

jhemmmm commented 4 years ago

I see,

That might not be a good idea on my side. To Identify the expired cache entries, I have to load all saved json files on /tmp/cache. There's at least 2TB of files on my cache directory.

jhemmmm commented 4 years ago

I have tried added a queue job that delete the files base on it's ttl. Here's the forked repo on it. https://github.com/jhemmmm/cached-request/commit/a65b9087aa56cfa9c9e385753d1ce0766ca1a0b5 https://github.com/jhemmmm/cached-request/commit/e40e2869a7e59ca35a1afb5140c42b735c0dc724

It works perfectly fine. Thanks.

EDIT: This thing requires redis. I am still looking for other solution.

jhemmmm commented 4 years ago

I have added a cleanup interval. https://github.com/jhemmmm/cached-request/commit/3cce8e1ff5f070ed7066496cd710ec9f4939bebb https://github.com/jhemmmm/cached-request/commit/5cc08abc1d95fea8766d585c0331a780c7d75d2c https://github.com/jhemmmm/cached-request/commit/24e71a07cd2b6e3ed6c64e94d7658a1267ecfc64

cachedRequest.cleaUpCacheDirectory({
    age: 60 * 60 * 3, // 3 hours
    cleanupInterval: 1000 * 60 * 30 // 30 minutes
});
elhoyos commented 4 years ago

This looks way better without redis. Here are some comments:

Thank you for your ideas and contributions. Please keep them coming.

jhemmmm commented 4 years ago

I used age because the find+remove uses it to check the "creation" of the file. Instead of the ttl given by the cached-request. I'll take a look for it.