hapijs / catbox

Multi-strategy object caching service
Other
494 stars 72 forks source link

Ability to generate and cache "batches" of items at once #203

Closed EvanK closed 5 years ago

EvanK commented 6 years ago

My rationale

I'm doing a ton of cache operations where I have a list of values ("id"s in the parlance of the policy api) for which I'm generating values with a database query.

Design-wise, I have two clear options:

Proposed addition to API

It would be useful to support generating and getting/setting in cache, batches of items at a time. The underlying strategies could optimize for as little network overhead as possible -- eg, catbox-redis could use MGET/MSET and/or pipelining.

I know that operating with a batch of ids and a superset of cache values adds an order of magnitude in complexity, but you could foist that complexity onto the user of the lib by accepting a collection of functions in place of generateFunc:

So the proposed batching API would be something like this:

options = {
  // same options we have now
  expiresIn: "1 hour",
  staleIn: "5 minutes",
  // and then something different...
  generateFunc: {
    batchFunc: async (ids) => { return knex('some_table').select('id', 'priority', 'fk_id').whereIn('fk_id', ids) },
    filterFunc: async (collection, id) => { return collection.filter((item) => id === item.fk_id) },
    mergeFunc: async (collections) => { return collections.reduce((accum, value) => accum.concat(value), []) },
  }
}

// new policy with above options
policy = new Catbox.Policy(options, client, `cacheNamespace.`)

// will miss cache, then generate (and set) four separate items for each arg
firstBatch = await policy.batch(1,2,3,4)

// will hit cache for both items, that were set previously
secondBatch = await policy.batch(3,4)
hueniverse commented 5 years ago

I think this is just beyond the scope of this module. This will work better for you if you just write your own cache wrapper around your exact needs.

I'll let @kanongil decide if he wants to reconsider.

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.