Skip to content

Commit 797a00a

Browse files
committed
docs: docs about ctx.skip/ctx.fail
1 parent 7afdb10 commit 797a00a

File tree

1 file changed

+63
-18
lines changed

1 file changed

+63
-18
lines changed

docs/content/docs/methods.md

+63-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ summary: "Comprehensive list of all methods available when using BentoCache"
66

77
Below is a list of all the methods available when using BentoCache.
88

9-
### namespace
9+
## namespace
1010

1111
Returns a new instance of the driver namespace. See [Namespaces](./namespaces.md) for more information.
1212

@@ -20,7 +20,7 @@ usersNamespace.set('3', { name: 'Doe' });
2020
usersNamespace.clear();
2121
```
2222

23-
### get
23+
## get
2424

2525
`get` allows you to retrieve a value from the cache. It returns `undefined` if the key does not exist.
2626

@@ -35,7 +35,7 @@ const products = await bento.get({
3535
});
3636
```
3737

38-
### set
38+
## set
3939

4040
Set a value in the cache.
4141

@@ -47,7 +47,7 @@ await bento.set({
4747
})
4848
```
4949

50-
### setForever
50+
## setForever
5151

5252
Set a value in the cache forever. It will never expire.
5353

@@ -59,7 +59,7 @@ await bento.setForever({
5959
})
6060
```
6161

62-
### getOrSet
62+
## getOrSet
6363

6464
This is the most powerful method of BentoCache. You should probably use this method most of the time.
6565

@@ -81,24 +81,69 @@ const products = await bento.getOrSet({
8181
})
8282
```
8383

84-
The `getOrSet` factory function accepts an `options` object as argument that can be used to dynamically set some cache options. This can be particulary useful when caching options depends on the value itself.
84+
The `getOrSet` factory function accepts an `ctx` object as argument that can be used to do multiple things:
85+
86+
### ctx.setTtl
87+
88+
`setTtl` allows you to set the TTL of the key dynamically. This is useful when the TTL depends on the value itself.
8589

8690
```ts
8791
const products = await bento.getOrSet({
8892
key: 'token',
89-
factory: async (options) => {
93+
factory: async (ctx) => {
9094
const token = await fetchAccessToken()
91-
95+
9296
options.setTtl(token.expiresIn)
93-
97+
9498
return token
9599
}
96100
})
97101
```
98102

99-
Auth tokens are a perfect example of this use case. The cached token should expire when the token itself expires. And we know the expiration time only after fetching the token.
103+
Auth tokens are a perfect example of this use case. The cached token should expire when the token itself expires. And we know the expiration time only after fetching the token. See [Adaptive Caching docs](./adaptive_caching.md) for more information.
104+
105+
### ctx.skip
106+
107+
Returning `skip` in a factory will not cache the value, and `getOrSet` will returns `undefined` even if there is a stale item in cache.
108+
It will force the key to be recalculated on the next call.
109+
110+
```ts
111+
cache.getOrSet({
112+
key: 'foo',
113+
factory: ({ skip, fail }) => {
114+
const item = await getFromDb()
115+
if (!item) {
116+
return skip()
117+
}
118+
119+
return item
120+
}
121+
})
122+
```
123+
124+
### ctx.fail
125+
126+
Returning `fail` in a factory will not cache the value and will throw an error. If there is a stale item in cache, it will be used.
127+
128+
```ts
129+
cache.getOrSet({
130+
key: 'foo',
131+
factory: ({ skip, fail }) => {
132+
const item = await getFromDb()
133+
if (!item) {
134+
return skip()
135+
}
136+
137+
if (item.isInvalid) {
138+
return fail('Item is invalid')
139+
}
140+
141+
return item
142+
}
143+
})
144+
```
100145

101-
### getOrSetForever
146+
## getOrSetForever
102147

103148
Same as `getOrSet`, but the value will never expire.
104149

@@ -109,55 +154,55 @@ const products = await bento.getOrSetForever({
109154
})
110155
```
111156

112-
### has
157+
## has
113158

114159
Returns `true` if the key exists in the cache, `false` otherwise.
115160

116161
```ts
117162
const hasProducts = await bento.has({ key: 'products' })
118163
```
119164

120-
### missing
165+
## missing
121166

122167
Returns `true` if the key does not exist in the cache, `false` otherwise.
123168

124169
```ts
125170
const missingProducts = await bento.missing({ key: 'products' })
126171
```
127172

128-
### pull
173+
## pull
129174

130175
Get the value of the key, and then delete it from the cache. Returns `undefined` if the key does not exist.
131176

132177
```ts
133178
const products = await bento.pull({ key: 'products' })
134179
```
135180

136-
### delete
181+
## delete
137182

138183
Delete a key from the cache.
139184

140185
```ts
141186
await bento.delete({ key: 'products' })
142187
```
143188

144-
### deleteMany
189+
## deleteMany
145190

146191
Delete multiple keys from the cache.
147192

148193
```ts
149194
await bento.deleteMany({ keys: ['products', 'users'] })
150195
```
151196

152-
### clear
197+
## clear
153198

154199
Clear the cache. This will delete all the keys in the cache if called from the "root" instance. If called from a namespace, it will only delete the keys in that namespace.
155200

156201
```ts
157202
await bento.clear();
158203
```
159204

160-
### disconnect
205+
## disconnect
161206

162207
Disconnect from the cache. This will close the connection to the cache server, if applicable.
163208

0 commit comments

Comments
 (0)