diff --git a/README.md b/README.md index 31e3883..49ce6c6 100644 --- a/README.md +++ b/README.md @@ -123,10 +123,11 @@ Options: ```js { ttl: 1000, // Time to live. How long (in ms) responses remain cached before being automatically ejected. If undefined, responses are never automatically ejected from the cache. - global: true, // If true, uses the global cache, which is shared together by all MemoryCaches that specify this option. If false (or undefined), every MemoryCache uses a separate cache. } ``` +Note that by default (if you don't use `withCache()`) a **shared** MemoryCache will be used (you can import this module in multiple files and they will all share the same cache). If you instantiate and provide a `new MemoryCache()` as shown above however, the cache is *NOT* shared unless you explicitly pass it around and pass it into `withCache()` in each of your source files. + ### FileSystemCache Cache to a directory on disk. This allows the cache to survive the process exiting. @@ -147,14 +148,18 @@ const fetch = fetchBuilder.withCache(new FileSystemCache(options)); ### Provide Your Own -You can implement a caching layer yourself. The cache simply needs to be an object that has `get(key)` and `set(key, value)` functions. +You can implement a caching delegate yourself. The cache simply needs to be an object that has `set(key, value)`, `get(key)`, and `remove(key)` functions. The set function must accept a key (which will be a string) and a value (which will be a JSON-serializable JS object) and store them. The get function should accept a key and return whatever value was set for that key (or `undefined`/`null` if there is no value for that key). +The remove function should accept a key and remove the cached value associated with that key, if any. + Both functions can be async. +It is safe to remove values from the cache arbitrarily (for example if you implement a TTL in the caching delegate). + For example you could make and use your own simple memory cache like this: ```js diff --git a/classes/caching/file_system_cache.js b/classes/caching/file_system_cache.js index 074c2d1..cdc616a 100644 --- a/classes/caching/file_system_cache.js +++ b/classes/caching/file_system_cache.js @@ -26,4 +26,4 @@ module.exports = class FileSystemCache { this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key)); } } -} +}; diff --git a/classes/caching/key_timeout.js b/classes/caching/key_timeout.js index 635c117..42e1367 100644 --- a/classes/caching/key_timeout.js +++ b/classes/caching/key_timeout.js @@ -13,4 +13,4 @@ module.exports = class KeyTimeout { callback(); }, durationMs); } -} \ No newline at end of file +}; diff --git a/classes/caching/memory_cache.js b/classes/caching/memory_cache.js index 9428f5d..d6bf0bb 100644 --- a/classes/caching/memory_cache.js +++ b/classes/caching/memory_cache.js @@ -4,14 +4,7 @@ module.exports = class MemoryCache { constructor(options = {}) { this.ttl = options.ttl; this.keyTimeout = new KeyTimeout(); - - if (options.global && !globalThis.nodeFetchCache) { - globalThis.nodeFetchCache = {}; - } - - this.cache = options.global - ? globalThis.nodeFetchCache - : {}; + this.cache = {}; } get(key) { @@ -30,4 +23,4 @@ module.exports = class MemoryCache { this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key)); } } -} +}; diff --git a/classes/response.js b/classes/response.js index 2f2c3e4..4081054 100644 --- a/classes/response.js +++ b/classes/response.js @@ -1,4 +1,3 @@ -const fs = require('fs'); const stream = require('stream'); const Headers = require('./headers.js'); diff --git a/index.js b/index.js index 2787119..87eae54 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,6 @@ const fetch = require('node-fetch'); const fs = require('fs'); const { URLSearchParams } = require('url'); const crypto = require('crypto'); -const path = require('path'); const Response = require('./classes/response.js'); const MemoryCache = require('./classes/caching/memory_cache.js'); @@ -97,19 +96,18 @@ async function getResponse(cache, requestArguments) { if (cachedValue) { return new Response(cachedValue, ejectSelfFromCache, true); - } else { - const fetchResponse = await fetch(...requestArguments); - const rawResponse = await createRawResponse(fetchResponse); - await cache.set(cacheKey, rawResponse); - return new Response(rawResponse, ejectSelfFromCache, false); } + const fetchResponse = await fetch(...requestArguments); + const rawResponse = await createRawResponse(fetchResponse); + await cache.set(cacheKey, rawResponse); + return new Response(rawResponse, ejectSelfFromCache, false); } function createFetchWithCache(cache) { - const fetch = (...args) => getResponse(cache, args); - fetch.withCache = createFetchWithCache; + const fetchCache = (...args) => getResponse(cache, args); + fetchCache.withCache = createFetchWithCache; - return fetch; + return fetchCache; } module.exports = createFetchWithCache(new MemoryCache());