A cache that can be generated server-side and brought back to life client-side.
An in-memory cache that can be restored/rehydrated (brought back to life) via a string or array to rebuild the cache Map.
This is particularly handy for frameworks that can generate a cache server-side, and would want to reflect that same cache client-side. For example, anything built in Next or React that is rendered server-side.
I realized I was using the same/similar cache instance in
a new package that I am currently writing, so it just made sense to make this its
npm install --save creature-cache
yarn add creature-cache
How to use
There really isn’t anything too magical about this package. Just a
with a bit of an interface around it to handle initializing, accessing and extracting
the current cached values.
Intializing a new
creature-cache instance is pretty straight forward. Just create
your new instance, and optionally include a
cache value to rehydrate the cache with.
cache can be an
Object, or a
string that can converted back into
an object. Internally, this package uses
to parse the string and build the internal
import Cache from 'creature-cache'; const cache = new Cache(); ... [go nuts]
clear(): Cache- Clear the internal cache
Mapand return a reference back to
Cacheinstance so methods can be chained.
extract(): Array- Extract the internal cache
Mapas an array.
has(key): boolean- Check to see if the cache has an instance for the
read(key): any- Read the cached value associated to the provided
restore(cache): Cache- In the event you want to restore a cache after an instance has been initialized, call this.
write(key, value): Cache- Write a cache value to the associated key.