|
1 | | -# cacheous |
| 1 | +# cacheism |
2 | 2 | Simple caching library |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +The goal of cacheism is to wrap an async function with caching logic where we |
| 7 | +can easily specify under what circumstances we want to return the cache or fetch |
| 8 | +the live data. |
| 9 | + |
| 10 | +Your callback will get passed to it a Hit if there is an existing cache stored |
| 11 | +or a Miss if there is no existing cache. |
| 12 | + |
| 13 | +```js |
| 14 | +const Cacheism = require('@andrewshell/cacheism'); |
| 15 | + |
| 16 | +const datadir = __dirname + '/data'; |
| 17 | +const cache = new Cacheism(Cacheism.store.filesystem({ datadir })); |
| 18 | + |
| 19 | +async function run() { |
| 20 | + let result = await cache.go('-internal', 'hoopla', Cacheism.Status.cacheOnFail, async (existing) => { |
| 21 | + if (Math.random() < 0.5) { |
| 22 | + throw Error('Death'); |
| 23 | + } |
| 24 | + return { message: 'Hoopla!' }; |
| 25 | + }); |
| 26 | + |
| 27 | + if (result.isHit) { |
| 28 | + console.dir(result.data); |
| 29 | + } |
| 30 | + |
| 31 | + if (result.error) { |
| 32 | + console.error(result.error); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +run().catch(err => console.error(err)); |
| 37 | +``` |
| 38 | + |
| 39 | +## Statuses |
| 40 | + |
| 41 | +### Only Fresh |
| 42 | + |
| 43 | +The onlyFresh status is for times where we never want to use the cache, but we |
| 44 | +want to fetch the fresh data and store it in the cache for other requests. |
| 45 | + |
| 46 | +### Cache on Fail |
| 47 | + |
| 48 | +The cacheOnFail status is for times where we want to try to fetch fresh data, |
| 49 | +but if an error is thrown, use the cache if present. |
| 50 | + |
| 51 | +### Prefer Cache |
| 52 | + |
| 53 | +The preferCache status is for times where we want to use the cache if available |
| 54 | +and only fetch fresh if the cache is not available. |
| 55 | + |
| 56 | +### Only Cache |
| 57 | + |
| 58 | +The onlyCache status if for times where we don't want to attempt to fetch fresh |
| 59 | +data and only return the cache if present. |
| 60 | + |
| 61 | +## Results |
| 62 | + |
| 63 | +The cache.go function will always return either a Hit or a Miss object. |
| 64 | + |
| 65 | +### Hit |
| 66 | + |
| 67 | +A hit is returned when we have good data. The `cached` param will be `true` if |
| 68 | +the data was fetched from cache versus fresh data. |
| 69 | + |
| 70 | +``` |
| 71 | +Hit { |
| 72 | + version: 2, |
| 73 | + cacheName: '-internal/hoopla', |
| 74 | + cached: true, |
| 75 | + created: 2022-04-22T21:05:14.094Z, |
| 76 | + data: { message: 'Hoopla!' }, |
| 77 | + error: Error: Death |
| 78 | + at /Users/andrewshell/code/personal/test-cacheism/index.js:8:15 |
| 79 | + at Cacheism.go (/Users/andrewshell/code/personal/cacheism/lib/cacheism.js:29:30) |
| 80 | + at async run (/Users/andrewshell/code/personal/test-cacheism/index.js:7:13), |
| 81 | + etag: '"15-QcHvuZdyxCmLJ4zoYIPsP6pkNoM"', |
| 82 | + isHit: true, |
| 83 | + isMiss: false |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +In the case of Cache on Fail, the error param may be set which is the error |
| 88 | +thrown while fetching fresh data. |
| 89 | + |
| 90 | +### Miss |
| 91 | + |
| 92 | +A miss is returned when we don't have good data. For instance, if there wasn't |
| 93 | +cached data and an error was thrown while fetching fresh data. You'll also get a |
| 94 | +miss if you fetch with the onlyCache status and there isn't a cache. |
| 95 | + |
| 96 | +``` |
| 97 | +Miss { |
| 98 | + version: 2, |
| 99 | + cacheName: '-internal/hoopla', |
| 100 | + cached: false, |
| 101 | + created: 2022-04-22T21:30:56.275Z, |
| 102 | + data: null, |
| 103 | + error: Error: Missing cache |
| 104 | + at Cacheism.go (/Users/andrewshell/code/personal/cacheism/lib/cacheism.js:27:19) |
| 105 | + at async run (/Users/andrewshell/code/personal/test-cacheism/index.js:7:18), |
| 106 | + etag: null, |
| 107 | + isHit: false, |
| 108 | + isMiss: true |
| 109 | +} |
| 110 | +``` |
0 commit comments