antoinechalifour/memento

Rehydrate the cache

Closed this issue ยท 1 comments

Clearing Memento's cache must be done manually for the moment, there's no exposed API. A basic addition would be to add a clearCache option (see #8). This option however creates friction because you sometimes want to add to/update the cache without erasing all previous entries.

This is a proposal to add a way to rehydrate the cache.

New option

On top of this clearCache option, a common use case is to re-populate the responses coming from the hostUrl. You want to use that to get fresh responses from your host to use in your development environement.

A possible option name for this is:

$ memento --hydrate

Note that it's not very clear from this option name that it's a "watch" mode.

Example

In this example:

  • /api/v2/pokemon/pikachu is already cached
  • /api/v2/pokemon/electabuzz is not cached
$ memento --hydrate

[๐Ÿ“ผ] Memento running @ http://localhost:3344
[๐Ÿ“ผ] Memento configured to proxy to https://pokeapi.co with a delay of 0ms
[๐Ÿ“ผ] Memento is run in hydrating mode. Each response already cached will be
     overwritten to the new response coming from the network. New entries will
     be added.

  <-- GET /api/v2/pokemon/pikachu
[๐Ÿ“ผ](debug) Fetching response from the network
[๐Ÿ“ผ](debug) Rehydrating response for /private/tmp/memento-example/.memento-cache-aHR0cHM6Ly9wb2tlYXBpLmNv/GET-_api_v2_pokemon_pikachu-9719b898ae4cc86d932840cefff21a8c7dec9eb9
  --> GET /api/v2/pokemon/pikachu 304 248ms

  <-- GET /api/v2/pokemon/electabuzz
[๐Ÿ“ผ](debug) Fetching response from the network
[๐Ÿ“ผ](debug) Persisting response for /private/tmp/memento-example/.memento-cache-aHR0cHM6Ly9wb2tlYXBpLmNv/GET-_api_v2_pokemon_electabuzz-72051936a11eab4b4820e1487f2cf35951447a08
  --> GET /api/v2/pokemon/electabuzz 200 790ms 180.15kb

In hydrating mode, although /api/v2/pokemon/pikachu was already cached, it's still fetched from the network and the cache response is overwritten. /api/v2/pokemon/electabuzz was not cached so it's fetched from the network and cached, as in the normal mode.

Alternative names

  • populate
  • register
  • overwrite

Love the idea, you're definitely right about this feature.

I'd love to suggest an alternative : if we decide to launch an interactive CLI (like Jest does), we could add a refresh option in the menu.

When selecting the refresh option, we then need to select the request, which can be fetched without the client needing to call memento (as we already have all the metadata in the cache to replay the request).

The UI would be something like that :

$ memento refresh
> Select the request you want to refresh :
- GET /pokemon/pikachu (at 14:22:34T00:00:00.000Z)
- GET /pokemon/tyranitar (at 14:22:34T00:00:00.000Z)
X GET /pokemon/electabuzz (at 14:22:34T00:00:00.000Z)

> Refreshing GET /pokemon/electabuzz data...
> Done