Skip to content

Commit ab109b4

Browse files
README cache control
Signed-off-by: rmlearney-digicatapult <robert.learney@digicatapult.org.uk>
1 parent a1a47c8 commit ab109b4

1 file changed

Lines changed: 41 additions & 14 deletions

File tree

README.md

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,66 @@ const doc = await resolver.resolve('did:ethr:0xF3beAC30C498D9E26865F34fCAa57dBB9
8686

8787
## Caching
8888

89-
Resolving DID Documents can be expensive. It is in most cases best to cache DID documents. Caching has to be
90-
specifically enabled using the `cache` parameter
89+
Resolving DID Documents can be expensive. It is in most cases best to cache DID documents. Caching is controlled via the `cache` option, which accepts a boolean value or a custom `DIDCache` function.
9190

92-
The built-in cache uses a Map, but does not have an automatic TTL, so entries don't expire. This is fine in most web,
93-
mobile and serverless contexts. If you run a long-running process you may want to use an existing configurable caching
94-
system.
91+
### Built-in caching
9592

96-
The built-in Cache can be enabled by passing in a `true` value to the constructor:
93+
The built-in cache uses a `Map` and does not have an automatic TTL, so entries don't expire. This is fine in most web, mobile and serverless contexts. If you run a long-running process, consider using a custom cache with expiration.
94+
95+
Enable the built-in cache by passing `cache: true` to the constructor:
9796

9897
```js
99-
const resolver = new DIDResolver({
98+
const resolver = new Resolver({
10099
ethr,
101100
web
102101
}, {
103102
cache: true
104103
})
105104
```
106105

107-
Here is an example using `js-cache` which has not been tested.
106+
### Disabling cache
107+
108+
To disable caching entirely, pass `cache: false`:
109+
110+
```js
111+
const resolver = new Resolver({
112+
ethr,
113+
web
114+
}, {
115+
cache: false
116+
})
117+
```
118+
119+
### Per-call cache control
120+
121+
You can override the global cache setting on a per-call basis using the `cache` option in `DIDResolutionOptions`:
122+
123+
```js
124+
// Global cache enabled, but disable for this specific resolution
125+
const doc = await resolver.resolve('did:ethr:0xabcd...', { cache: false })
126+
127+
// Global cache disabled, but enable for this specific resolution
128+
const doc = await resolver.resolve('did:ethr:0xabcd...', { cache: true })
129+
```
130+
131+
### Custom cache implementation
132+
133+
For advanced use cases, implement a custom `DIDCache` function. It receives the parsed DID, a resolve function, and optional resolution options:
108134

109135
```js
110-
var cache = require('js-cache')
111-
const customCache : DIDCache = (parsed, resolve) => {
112-
// DID spec requires to not cache if no-cache param is set
113-
if (parsed.params && parsed.params['no-cache'] === 'true') return await resolve()
136+
const customCache: DIDCache = async (parsed, resolve, options) => {
137+
// Respect per-call cache control
138+
if (options?.cache === false) return await resolve()
139+
114140
const cached = cache.get(parsed.didUrl)
115141
if (cached !== undefined) return cached
142+
116143
const doc = await resolve()
117-
cache.set(parsed, doc, 60000)
144+
cache.set(parsed.didUrl, doc, 60000) // 60s TTL
118145
return doc
119146
}
120147

121-
const resolver = new DIDResolver({
148+
const resolver = new Resolver({
122149
ethr,
123150
web
124151
}, {

0 commit comments

Comments
 (0)