11# cacheism
2- Simple caching library
2+
3+ Simple caching library
34
45[ ![ Node.js CI] ( https://github.com/andrewshell/cacheism/actions/workflows/node.js.yml/badge.svg )] ( https://github.com/andrewshell/cacheism/actions/workflows/node.js.yml )
56
7+ ## Installation
8+
9+ ``` bash
10+ npm install @andrewshell/cacheism
11+ ```
12+
613## Overview
714
815The goal of cacheism is to wrap an async function with caching logic where we
@@ -13,29 +20,63 @@ Your callback will get passed to it a Hit if there is an existing cache stored
1320or a Miss if there is no existing cache.
1421
1522``` js
16- const Cacheism = require (' @andrewshell/cacheism' );
23+ const { Cacheism } = require (" @andrewshell/cacheism" );
1724
18- const datadir = __dirname + ' /data' ;
25+ const datadir = __dirname + " /data" ;
1926const cache = new Cacheism (Cacheism .store .filesystem ({ datadir }));
2027
2128async function run () {
22- let result = await cache .go (' -internal ' , ' hoopla ' , Cacheism . Status . cacheOnFail , async ( existing ) => {
23- if ( Math . random () < 0.5 ) {
24- throw Error ( ' Death ' );
25- }
26- return { message : ' Hoopla! ' };
27- });
28-
29- if ( result . isHit ) {
30- console . dir ( result . data ) ;
29+ let result = await cache .go (
30+ " -internal " ,
31+ " hoopla " ,
32+ Cacheism . Status . cacheOnFail ,
33+ async ( existing ) => {
34+ if ( Math . random () < 0.5 ) {
35+ throw Error ( " Death " );
36+ }
37+ return { message : " Hoopla! " } ;
3138 }
39+ );
40+
41+ if (result .isHit ) {
42+ console .dir (result .data );
43+ }
44+
45+ if (result .error ) {
46+ console .error (result .error );
47+ }
48+ }
49+
50+ run ().catch ((err ) => console .error (err));
51+ ```
52+
53+ ### ESM
54+
55+ ``` js
56+ import Cacheism from " @andrewshell/cacheism" ;
57+
58+ const datadir = new URL (" ./data" , import .meta.url).pathname;
59+ const cache = new Cacheism (Cacheism .store .filesystem ({ datadir }));
3260
33- if (result .error ) {
34- console .error (result .error );
61+ const result = await cache .go (
62+ " -internal" ,
63+ " hoopla" ,
64+ Cacheism .Status .cacheOnFail ,
65+ async (existing ) => {
66+ if (Math .random () < 0.5 ) {
67+ throw Error (" Death" );
3568 }
69+ return { message: " Hoopla!" };
70+ }
71+ );
72+
73+ if (result .isHit ) {
74+ console .dir (result .data );
3675}
3776
38- run ().catch (err => console .error (err));
77+ if (result .error ) {
78+ console .error (result .error );
79+ }
3980` ` `
4081
4182## Statuses
@@ -57,9 +98,29 @@ and only fetch fresh if the cache is not available.
5798
5899### Only Cache
59100
60- The onlyCache status if for times where we don't want to attempt to fetch fresh
101+ The onlyCache status is for times where we don't want to attempt to fetch fresh
61102data and only return the cache if present.
62103
104+ ## Stores
105+
106+ Cacheism supports different storage backends:
107+
108+ ### Filesystem Store
109+
110+ Persists cache to JSON files in a directory. Good for production use.
111+
112+ ` ` ` js
113+ const cache = new Cacheism (Cacheism .store .filesystem ({ datadir: " ./cache" }));
114+ ` ` `
115+
116+ ### Memory Store
117+
118+ Stores cache in memory. Useful for testing or ephemeral caching.
119+
120+ ` ` ` js
121+ const cache = new Cacheism (Cacheism .store .memory ());
122+ ` ` `
123+
63124## Results
64125
65126The cache.go function will always return either a Hit or a Miss object.
@@ -76,10 +137,7 @@ Hit {
76137 cached: true ,
77138 created: 2023 - 04 - 02T22 : 00 : 49 .320Z ,
78139 data: { message: ' Hoopla!' },
79- error: Error: Death
80- at /Users/andrewshell/code/test-cacheism/index.js:9:19
81- at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:30:30)
82- at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
140+ error: Error : Death,
83141 errorTime: 2023 - 04 - 02T22 : 00 : 49 .928Z ,
84142 consecutiveErrors: 1 ,
85143 etag: ' "15-QcHvuZdyxCmLJ4zoYIPsP6pkNoM"' ,
@@ -104,9 +162,7 @@ Miss {
104162 cached: false ,
105163 created: 2023 - 04 - 02T22 : 02 : 30 .294Z ,
106164 data: null ,
107- error: Error: Missing cache
108- at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:28:19)
109- at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
165+ error: Error : Missing cache,
110166 errorTime: 2023 - 04 - 02T22 : 02 : 30 .294Z ,
111167 consecutiveErrors: 1 ,
112168 etag: null ,
0 commit comments