mcollina / async-cache-dedupe

Async cache with dedupe support
MIT License
612 stars 39 forks source link

Is there a way to tell if the result comes from a cache hit or miss? #142

Open ligreman opened 5 months ago

ligreman commented 5 months ago

First of all thanks for this great plugin.

I'm using it to cache some database searchs and I want to answer the clients with a X-Cache-Status header that informs if the data was taken from cache or from database (header values Hit or Miss).

cache.define('fetchSomething', async (k) => {
  return { k }
})

fastify.get('/foo', async function (request, reply) {
        const p1 = await cache.fetchSomething(42)

        // If p1 comes from a cache Hit, set reply header[X-Cache-Status] to Hit. Else Miss.

        return reply.send({msg: 'Hello'});
    });

Is there a way to tell if the result of fetchSomething comes from the cache (Hit) or from database (Miss)? I've been struggling with the onHit, onMiss... events but they only receive the key as parameter.

Thanks!

mcollina commented 5 months ago

Actually it would be a great feature to add (maybe behind an option).