Skip to content

Commit

Permalink
feat: pojo methods syntax (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 8, 2024
1 parent ff32759 commit 01d1d4b
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 122 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ See the [events documentation](https://bentocache.dev/docs/events) for more info
All TTLs can be passed in a human-readable string format. We use [lukeed/ms](https://github.com/lukeed/ms) under the hood. (this is optional, and you can pass a `number` in milliseconds if you prefer)

```ts
bento.getOrSet('foo', () => getFromDb(), {
ttl: '2.5h'
gracePeriod: { enabled: true, duration: '6h' }
bento.getOrSet({
key: 'foo',
ttl: '2.5h',
factory: () => getFromDb(),
})
```

Expand All @@ -132,8 +133,10 @@ bento.getOrSet('foo', () => getFromDb(), {
When you cached item will expire soon, you can refresh it in advance, in the background. This way, next time the entry is requested, it will already be computed and thus returned to the user super quickly.

```ts
bento.getOrSet('foo', () => getFromDb(), {
bento.getOrSet({
key: 'foo',
earlyExpiration: 0.8
factory: () => getFromDb(),
})
```

Expand Down
11 changes: 7 additions & 4 deletions docs/content/docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ See the [events documentation](./digging_deeper/events.md) for more information.
All TTLs can be passed in a human-readable string format. We use [lukeed/ms](https://github.com/lukeed/ms) under the hood. (this is optional, and you can pass a `number` in milliseconds if you prefer)

```ts
bento.getOrSet('foo', () => getFromDb(), {
bento.getOrSet({
key: 'foo',
ttl: '2.5h',
gracePeriod: { enabled: true, duration: '6h' }
factory: () => getFromDb(),
})
```

Expand All @@ -143,8 +144,10 @@ bento.getOrSet('foo', () => getFromDb(), {
When you cached item will expire soon, you can refresh it in advance, in the background. This way, next time the entry is requested, it will already be computed and thus returned to the user super quickly.

```ts
bento.getOrSet('foo', () => getFromDb(), {
earlyExpiration: 0.8
bento.getOrSet({
key: 'foo',
earlyExpiration: 0.8,
factory: () => getFromDb(),
})
```

Expand Down
69 changes: 68 additions & 1 deletion docs/content/docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ summary: "Comprehensive list of all methods available when using BentoCache"

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

### Single object vs multiple arguments

Most of the methods 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 :

```ts
// 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' },
})
```

---

### namespace

Returns a new instance of the driver namespace. See [Namespaces](./namespaces.md) for more information.
Expand All @@ -22,7 +48,7 @@ usersNamespace.clear();

### get

`get` has multiple signatures and so it can be used in different ways.
`get` allows you to retrieve a value from the cache. It returns `undefined` if the key does not exist.

#### get(key: string)

Expand Down Expand Up @@ -53,6 +79,17 @@ const products = await bento.get<string>('products', [], {
});
```

#### get<T>(options: GetPojoOptions<T>)

Same as above, but with an object as argument.

```ts
const products = await bento.get({
key: 'products',
defaultValue: [],
});
```

### set

Set a value in the cache.
Expand All @@ -62,6 +99,12 @@ await bento.set('products', products);
await bento.set('products', products, {
gracePeriod: { enabled: true, duration: '5m' }
});

await bento.set({
key: 'products',
value: products,
gracePeriod: { enabled: true, duration: '5m' }
})
```

### setForever
Expand All @@ -70,6 +113,12 @@ Set a value in the cache forever. It will never expire.

```ts
await bento.setForever('products', products);

await bento.setForever({
key: 'products',
value: products,
gracePeriod: { enabled: true, duration: '5m' }
})
```

### getOrSet
Expand All @@ -87,6 +136,14 @@ const products = await bento.getOrSet('products', () => fetchProducts(), {
ttl: '5m',
gracePeriod: { enabled: true, duration: '1m' },
})

// with options as object
const products = await bento.getOrSet({
key: 'products',
ttl: '5m',
factory: () => fetchProducts(),
gracePeriod: { enabled: true, duration: '1m' },
})
```

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.
Expand All @@ -107,6 +164,11 @@ Same as `getOrSet`, but the value will never expire.

```ts
const products = await bento.getOrSetForever('products', () => fetchProducts())

const products = await bento.getOrSetForever({
key: 'products',
factory: () => fetchProducts(),
})
```

### has
Expand All @@ -115,6 +177,7 @@ Returns `true` if the key exists in the cache, `false` otherwise.

```ts
const hasProducts = await bento.has('products');
const hasProducts = await bento.has({ key: 'products' })
```

### missing
Expand All @@ -123,6 +186,7 @@ Returns `true` if the key does not exist in the cache, `false` otherwise.

```ts
const missingProducts = await bento.missing('products');
const missingProducts = await bento.missing({ key: 'products' })
```

### pull
Expand All @@ -131,6 +195,7 @@ Get the value of the key, and then delete it from the cache. Returns `undefined`

```ts
const products = await bento.pull('products');
const products = await bento.pull({ key: 'products' })
```

### delete
Expand All @@ -139,6 +204,7 @@ Delete a key from the cache.

```ts
await bento.delete('products');
await bento.delete({ key: 'products' })
```

### deleteMany
Expand All @@ -147,6 +213,7 @@ Delete multiple keys from the cache.

```ts
await bento.deleteMany(['products', 'users']);
await bento.deleteMany({ keys: ['products', 'users'] })
```

### clear
Expand Down
11 changes: 6 additions & 5 deletions docs/content/docs/stampede_protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ Imagine this simple route that allows retrieving a post by its ID.
```ts
router.get('/posts/:id', async (request) => {
const { id } = request.params
const post = await bento.getOrSet(
`post:${id}`,
() => getPostFromDb(id),
{ ttl: '1h' }
)

const post = await bento.getOrSet({
key: `post:${id}`,
ttl: '1h',
factory: () => getPostFromDb(id),
})

return user
})
Expand Down
40 changes: 20 additions & 20 deletions docs/content/docs/walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ const bento = new BentoCache({

router.get('/products/:id', async (req, res) => {
const productId = req.params.id
const product = await bento.getOrSet(
`product:${productId}`,
() => Product.find(productId),
{ ttl: '1m' }
)
const product = await bento.getOrSet({
key: `product:${productId}`,
ttl: '1m',
factory: () => Product.find(productId),
})

res.json(product)
})
Expand Down Expand Up @@ -135,11 +135,11 @@ const bento = new BentoCache({

router.get('/products/:id', async (req, res) => {
const productId = req.params.id
const product = await bento.getOrSet(
`product:${productId}`,
() => Product.find(productId),
{ ttl: '1m' }
)
const product = await bento.getOrSet({
key: `product:${productId}`,
ttl: '1m',
factory: () => Product.find(productId),
})

res.json(product)
})
Expand Down Expand Up @@ -207,11 +207,11 @@ const bento = new BentoCache({

router.get('/products/:id', async (req, res) => {
const productId = req.params.id
const product = await bento.getOrSet(
`product:${productId}`,
() => Product.find(productId),
{ ttl: '1m' }
)
const product = await bento.getOrSet({
key: `product:${productId}`,
ttl: '1m',
factory: () => Product.find(productId),
})

res.json(product)
})
Expand Down Expand Up @@ -258,11 +258,11 @@ const bento = new BentoCache({

router.get('/products/:id', async (req, res) => {
const productId = req.params.id
const product = await bento.getOrSet(
`product:${productId}`,
() => Product.find(productId),
{ ttl: '1m' }
)
const product = await bento.getOrSet({
key: `product:${productId}`,
ttl: '1m',
factory: () => Product.find(productId),
})

res.json(product)
})
Expand Down

0 comments on commit 01d1d4b

Please sign in to comment.