express-memorize - v1.2.0
    Preparing search index...

    Function memorize

    • Creates an in-memory cache for an Express application.

      Returns a Memorize instance that can be used as per-route middleware, a global app.use() middleware, or a cache management API — all sharing the same underlying store.

      Only GET requests are cached. Responses are cached only when the HTTP status code is in the 2xx range. The cache key is req.originalUrl, which includes the query string.

      Parameters

      Returns Memorize

      const cache = memorize({ ttl: 30_000 });

      app.get('/users', cache(), (req, res) => {
      res.json({ data: users });
      });
      const cache = memorize({ ttl: 60_000 });
      app.use(cache()); // caches all GET routes
      const cache = memorize({ ttl: 30_000 });

      app.post('/users', (req, res) => {
      users.push(req.body);
      cache.delete('/users');
      res.status(201).json(req.body);
      });
      cache.on(MemorizeEventType.Set,    (e) => console.log('stored',  e.key));
      cache.on(MemorizeEventType.Delete, (e) => console.log('deleted', e.key));
      cache.on(MemorizeEventType.Expire, (e) => console.log('expired', e.key));
      cache.on(MemorizeEventType.Empty, () => console.log('cache is empty'));