creature-cache
v0.1.13
Published
A cache that can be generated server-side and brought back to life client-side.
Downloads
76
Maintainers
Readme
🧟♂️ creature-cache
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 react-contentful
,
react-request-block
, and
a new package that I am currently writing, so it just made sense to make this its
own package.
Install
Via npm
npm install --save creature-cache
Via Yarn
yarn add creature-cache
How to use
There really isn’t anything too magical about this package. Just a Map
instance
with a bit of an interface around it to handle initializing, accessing and extracting
the current cached values.
Initialization
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.
The cache
can be an Array
, Object
, or a string
that can converted back into
an object. Internally, this package uses flatted
to parse the string and build the internal Map
instance.
import Cache from 'creature-cache';
const cache = new Cache();
... [go nuts]
Methods
clear(): Cache
- Clear the internal cacheMap
and return a reference back toCache
instance so methods can be chained.extract(): Array
- Extract the internal cacheMap
as an array.has(key): boolean
- Check to see if the cache has an instance for thekey
provided.read(key): any
- Read the cached value associated to the providedkey
.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.