Lot of goodies
Breaking Changes
We've introduced an adapter system for the database store, which allows us to decouple Knex from Bentocache. This change brings a few breaking changes:
- All specific drivers like
mysqlDriver
,sqliteDriver
, etc., have been removed. You should now useknexDriver
instead. - The knex driver won't automatically create the knex instance anymore. You'll need to create the instance yourself and pass it to the adapter.
For more information, check out the documentation here.
Features
Database Adapter
We've added an adapter system to Bentocache, allowing us to decouple from Knex. This means you can now use a different ORM/Query builder such as Prisma, Kysely, Drizzle... You can use the same ORM that's already part of your application.
Kysely Adapter
With the new adapter system, we're introducing a built-in adapter for Kysely for the database store. You can find instructions on how to use Kysely with Bentocache here.
Adaptive Caching
Sometimes, we can't predetermine the TTL to use. For example, when fetching a token from an API that returns the token and its lifespan. It would be great to set the TTL to match the token's lifespan. This wasn't possible with the getOrSet
method before. That's why we've introduced adaptive caching:
const authToken = await bento.getOrSet('token', async (options) => {
const token = await fetchAccessToken();
options.setTtl(token.expiresIn);
return token;
});
In short, you can now dynamically set the TTL based on certain parameters from your factory function. For more information, check the documentation.
File Driver Automatic Eviction
Until now, using the file driver for our cache meant that files kept accumulating, never deleting even after the cached values expired. Because, well, File system doesn't have a TTL system.
To address this, we've added an automatic eviction system. The concept is simple: start a worker thread that regularly cleans up files with expired entries.
For more details, see here.
POJOs Methods
Most of the methods now accept arguments in two different ways : either as a single argument or as an object.
If you need to pass some specific options, the object way is probably the best choice since it is more "vertical" and may be easier to read. On the other hand, the single argument may be more concise when you don't need to pass specific options.
Example:
// multiple arguments
await bento.getOrSet('products', () => fetchProducts(), {
ttl: '5m',
gracePeriod: { enabled: true, duration: '1m' },
})
// is equivalent to
await bento.getOrSet({
key: 'products',
ttl: '5m',
factory: () => fetchProducts(),
gracePeriod: { enabled: true, duration: '1m' },
})
Boring Bus
Bentocache Bus has been exported into a generic package here. As a result, Bentocache was refactored to use this new package.
Along the way, we improved the bus, fixed some bugs, and added a retry interval
concept for the retry queue. Thanks to @RomainLanz for the work on this package!
Commits
- fix: connection options wrongly passed to redis transport (e92d835)
- chore: add hono example (3785f7b)
- feat: pojo methods syntax (#15) (01d1d4b)
- refactor: move to @boringnode/bus (#14) (ff32759)
- doc: fix invalid url (bb4ea66)
- refactor: remove deprecated dependency (393f316)
- feat: add adaptive caching (1ec8c29)
- refactor: move test_helpers to tests folder (0068d36)
- feat: cleaner worker thread for File Driver (#13) (3a839e7)
- refactor: add
Driver
suffix to all drivers (b718b95) - ci: update ci deps (58d4879)
- feat!: add adapter system for database store (#12) (08fff55)
- chore: remove upstash tests (9ed2738)
- chore: ts checks (146ecd4)
- chore: update dependencies (ee94407)