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

    Interface Memorize

    The cache instance returned by memorize.

    It is both a callable that produces Express middleware and a namespace for cache management methods and event hooks. All middleware created from the same Memorize instance share a single underlying store.

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

    // Per-route middleware
    app.get('/users', cache(), handler);

    // Global middleware — caches every GET route automatically
    app.use(cache());

    // Cache management
    cache.delete('/users');
    cache.clear();
    interface Memorize {
        clear(): void;
        delete(key: string): boolean;
        get(key: string): CacheInfo | null;
        getAll(): Record<string, CacheInfo>;
        on(event: Set, handler: (e: MemorizeSetEvent) => void): void;
        on(event: Delete, handler: (e: MemorizeDeleteEvent) => void): void;
        on(event: Expire, handler: (e: MemorizeExpireEvent) => void): void;
        on(event: Empty, handler: (e: MemorizeEmptyEvent) => void): void;
        (options?: MemorizeCallOptions): RequestHandler;
    }
    • Returns an Express RequestHandler that caches GET responses with a 2xx status code.

      • On a cache miss the request proceeds normally. The response is intercepted and stored after being sent. Sets X-Cache: MISS.
      • On a cache hit the stored response is returned immediately without calling downstream handlers. Sets X-Cache: HIT.
      • Non-GET requests are forwarded to next() unchanged.

      Parameters

      Returns RequestHandler

      app.get('/users',    cache(),               handler); // global TTL
      app.get('/products', cache({ ttl: 5_000 }), handler); // 5-second override
      app.use(cache()); // global middleware
    Index

    Methods

    • Removes a single entry from the cache and emits a MemorizeEventType.Delete event.

      Parameters

      • key: string

        The full request URL to invalidate.

      Returns boolean

      true if the entry existed and was removed, false otherwise.

      app.post('/users', (req, res) => {
      users.push(req.body);
      cache.delete('/users');
      res.status(201).json(req.body);
      });
    • Returns the CacheInfo for a specific cache key, or null if the key does not exist or has expired.

      Parameters

      • key: string

        The full request URL used as the cache key (e.g. /users?page=1).

      Returns CacheInfo | null

      const info = cache.get('/users');
      if (info) console.log(`expires in ${info.remainingTtl}ms`);
    • Returns all active (non-expired) cache entries as a plain object keyed by URL.

      Returns Record<string, CacheInfo>

      console.log(Object.keys(cache.getAll())); // ['/users', '/products']