hotelde/node-cache-manager-fs

ENOENT: no such file or directory on the odd occasion when cache retrieves up around 50 per second

Opened this issue · 0 comments

The following error was observed when diskCache.get() was called at a frequency of around 50 per second. At lower rates like 5 to 10 per second we never observed the error.

[Error: ENOENT: no such file or directory, open '/tmp/diskcache/cache_99e4ffff-6392-45b1-ba69-21786819c784.dat'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/tmp/diskcache/cache_99e4ffff-6392-45b1-ba69-21786819c784.dat'
}

Following cache-manager code used in an AWS Lambda.

"use strict";

const cacheManager = require("cache-manager");
const fsStore = require("cache-manager-fs");

const diskCache = cacheManager.caching({
   store: fsStore,
   options: {
      ttl: 300,
      maxsize: 10485760,
      path: "/tmp/diskcache",
      preventfill: true
   }
});

The following code was being used when the error above was encountered.

   diskCache.get("SOME KEY FOR CACHE", function (err, result) {
      if (err) {
         console.error({ error: err, result: result });
      } else if (result) {
         // do something based on result from cache
      } else {
         var args = {
            "Bucket": "SOME AWS S3 BUCKET",
            "Key": "SOME AWS S3 KEY"
         };

         s3.getObject(args, function (err, data) {
            if (err) {
               console.error({ error: err, data: data });
            } else {
               var json = data.Body.toString("ascii");
               diskCache.set("SOME KEY FOR CACHE", json);
               // do something based on result from cache
            }
         });
      }
   });

Changing to the following will address and work-around the odd occurrence of the error, however we figured we'd raise this incase we're not using cache-manager correctly.

   diskCache.get("SOME KEY FOR CACHE", function (err, result) {
      if (result) {
         // do something based on result from cache
      } else {
         if (err) {
            console.error({ error: err, result: result });
         }
         var args = {
            "Bucket": "SOME AWS S3 BUCKET",
            "Key": "SOME AWS S3 KEY"
         };

         s3.getObject(args, function (err, data) {
            if (err) {
               console.error({ error: err, data: data });
            } else {
               var json = data.Body.toString("ascii");
               diskCache.set("SOME KEY FOR CACHE", json);
               // do something based on result from cache
            }
         });
      }
   });