You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `MyCustomCacheProvider` class should extend `CacheProvider` from CommandKit and implement the required methods. You may use this to store the cache in redis, a database or a file system.
34
34
35
-
## Using the cache
35
+
CommandKit officially provides a `RedisCache` provider as well with `@commandkit/redis` package. You can use it as follows:
content:`Stats refreshed! New member count: ${newStats.members}`,
174
+
});
84
175
}
85
176
```
86
177
87
-
By default, the cached data will be stored forever until `revalidate()` or `expire()` is called on the cache object. You can also specify a custom TTL (time to live) for the cache by passing a second argument to the `cache` function.
178
+
## Cache Duration Format
179
+
180
+
You can specify cache duration in multiple formats:
181
+
182
+
- As milliseconds: `ttl:60000` (1 minute)
183
+
- As string shortcuts:
184
+
- `'5s'` - 5 seconds
185
+
- `'1m'` - 1 minute
186
+
- `'2h'` - 2 hours
187
+
- `'1d'` - 1 day etc.
188
+
189
+
## Best Practices
190
+
191
+
1. **Choose Good Cache Keys:**
192
+
193
+
- Use meaningful, unique cache tags
194
+
- Include relevant IDs (like `user-123` or `guild-456`)
195
+
196
+
2. **Set Appropriate TTL:**
197
+
198
+
- Short TTL for frequently changing data
199
+
- Longer TTL for static content
200
+
- Consider invalidating manually for important updates
201
+
202
+
3. **Handle Cache Misses:**
203
+
204
+
- Cache functions should handle cases when data isn't found
205
+
- Provide default values when appropriate
206
+
207
+
4. **Invalidate Strategically:**
208
+
- Invalidate cache when underlying data changes
209
+
- Consider using `revalidate()` for controlled updates
You may want to specify the cache parameters when using `"use cache"` directive. When using this approach, you can use `cacheTag()` to tag the cache with custom parameters.
244
+
By default, the cached data will be stored for 15 minutes unless `revalidate()` or `invalidate()` is called. You can specify a custom TTL (time to live) either as milliseconds or as a time string.
245
+
246
+
### Setting Cache Parameters
247
+
248
+
When using the `"use cache"` directive, you can use `cacheTag()` to set cache parameters:
`cacheTag()` will only tag the function when it first runs. Subsequent calls to the function will not tag the cache again.
126
-
If not tagged manually, commandkit assigns random tag name with 15 minutes TTL.
127
-
128
-
`cacheTag()` does not work with the `cache` function. It must be used with the `"use cache"` directive only.
286
+
`cacheTag()` will only tag the function when it first runs. If not tagged manually, CommandKit assigns a random tag name with 15 minutes TTL.
287
+
`cacheTag()` must be used with the `cache()` function or a function with `"use cache"` directive only.
129
288
:::
130
289
131
-
> You can alternatively use `cacheLife()` to set the TTL of the cache. Example: `cacheLife(10_000)` would set the TTL to 10 seconds.
290
+
## Managing Cache Data
132
291
133
-
## Invalidating/Revalidating the cache
292
+
### Invalidating Cache
134
293
135
-
Revalidating the cache is the process of updating the cached data with fresh data from the original source on demand. You can use the `unstable_revalidate()` function to revalidate the cache. CommandKit will not immediately revalidate the cache, but it will do so the next time the cached data is requested. Because of this, we can also term it as "lazy revalidation".
Expiring the cache is the process of removing the cached data or resetting the TTL of the cache. Use the `unstable_expire()` function to expire the cache.
0 commit comments