update readme
This commit is contained in:
parent
4cae72dce2
commit
ba067e65fd
@ -123,10 +123,11 @@ Options:
|
|||||||
```js
|
```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.
|
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
|
### FileSystemCache
|
||||||
|
|
||||||
Cache to a directory on disk. This allows the cache to survive the process exiting.
|
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
|
### 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 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 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.
|
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:
|
For example you could make and use your own simple memory cache like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
@ -26,4 +26,4 @@ module.exports = class FileSystemCache {
|
|||||||
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
@ -13,4 +13,4 @@ module.exports = class KeyTimeout {
|
|||||||
callback();
|
callback();
|
||||||
}, durationMs);
|
}, durationMs);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
@ -4,14 +4,7 @@ module.exports = class MemoryCache {
|
|||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
this.ttl = options.ttl;
|
this.ttl = options.ttl;
|
||||||
this.keyTimeout = new KeyTimeout();
|
this.keyTimeout = new KeyTimeout();
|
||||||
|
this.cache = {};
|
||||||
if (options.global && !globalThis.nodeFetchCache) {
|
|
||||||
globalThis.nodeFetchCache = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
this.cache = options.global
|
|
||||||
? globalThis.nodeFetchCache
|
|
||||||
: {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key) {
|
get(key) {
|
||||||
@ -30,4 +23,4 @@ module.exports = class MemoryCache {
|
|||||||
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
this.keyTimeout.updateTimeout(key, this.ttl, () => this.remove(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
const fs = require('fs');
|
|
||||||
const stream = require('stream');
|
const stream = require('stream');
|
||||||
const Headers = require('./headers.js');
|
const Headers = require('./headers.js');
|
||||||
|
|
||||||
|
10
index.js
10
index.js
@ -2,7 +2,6 @@ const fetch = require('node-fetch');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { URLSearchParams } = require('url');
|
const { URLSearchParams } = require('url');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const path = require('path');
|
|
||||||
const Response = require('./classes/response.js');
|
const Response = require('./classes/response.js');
|
||||||
const MemoryCache = require('./classes/caching/memory_cache.js');
|
const MemoryCache = require('./classes/caching/memory_cache.js');
|
||||||
|
|
||||||
@ -97,19 +96,18 @@ async function getResponse(cache, requestArguments) {
|
|||||||
|
|
||||||
if (cachedValue) {
|
if (cachedValue) {
|
||||||
return new Response(cachedValue, ejectSelfFromCache, true);
|
return new Response(cachedValue, ejectSelfFromCache, true);
|
||||||
} else {
|
}
|
||||||
const fetchResponse = await fetch(...requestArguments);
|
const fetchResponse = await fetch(...requestArguments);
|
||||||
const rawResponse = await createRawResponse(fetchResponse);
|
const rawResponse = await createRawResponse(fetchResponse);
|
||||||
await cache.set(cacheKey, rawResponse);
|
await cache.set(cacheKey, rawResponse);
|
||||||
return new Response(rawResponse, ejectSelfFromCache, false);
|
return new Response(rawResponse, ejectSelfFromCache, false);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createFetchWithCache(cache) {
|
function createFetchWithCache(cache) {
|
||||||
const fetch = (...args) => getResponse(cache, args);
|
const fetchCache = (...args) => getResponse(cache, args);
|
||||||
fetch.withCache = createFetchWithCache;
|
fetchCache.withCache = createFetchWithCache;
|
||||||
|
|
||||||
return fetch;
|
return fetchCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createFetchWithCache(new MemoryCache());
|
module.exports = createFetchWithCache(new MemoryCache());
|
||||||
|
Reference in New Issue
Block a user