0.1.13 • Published 3 years ago

creature-cache v0.1.13

Weekly downloads
110
License
MIT
Repository
github
Last release
3 years ago

🧟‍♂️ creature-cache

npm NPM npm Coveralls github CircleCI Snyk Vulnerabilities for GitHub Repo

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 cache Map and return a reference back to Cache instance so methods can be chained.

  • extract(): Array - Extract the internal cache Map as an array.

  • has(key): boolean - Check to see if the cache has an instance for the key provided.

  • read(key): any - Read the cached value associated to the provided key.

  • 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.

License

MIT © Ryan Hefner

0.1.13

3 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago