|
1 | 1 | --- |
2 | | -summary: Learn how to create a custom cache driver for BentoCache |
| 2 | +summary: Learn how to create a custom lock driver for Verrou |
3 | 3 | --- |
4 | 4 |
|
5 | | -# Create a custom cache driver |
| 5 | +# Create a custom lock driver |
6 | 6 |
|
7 | | -Extending BentoCache with your own cache driver is easy. What you need is a class that implements the `L1CacheDriver` or `L2CacheDriver` interfaces accessible from `bentocache/types`. The interface is defined as follows: |
| 7 | +Extending Verrou with your own cache driver is easy. What you need is a class that implements the `LockStore` interface accessible from `verrou/types`. The interface is defined as follows: |
8 | 8 |
|
9 | 9 | ```ts |
10 | | -interface L2CacheDriver { |
| 10 | +interface LockStore { |
11 | 11 | /** |
12 | | - * Returns a new instance of the driver namespace |
| 12 | + * Save the lock in the store if not already locked |
13 | 13 | */ |
14 | | - namespace(namespace: string): CacheDriver |
| 14 | + save(key: string, owner: string, ttl: number | null | undefined): Promise<boolean> |
15 | 15 |
|
16 | 16 | /** |
17 | | - * Get a value from the cache |
| 17 | + * Delete the lock from the store |
18 | 18 | */ |
19 | | - get(key: string): Promise<string | undefined> |
| 19 | + delete(key: string, owner: string): Promise<void> |
20 | 20 |
|
21 | 21 | /** |
22 | | - * Get the value of a key and delete it |
23 | | - * |
24 | | - * Returns the value if the key exists, undefined otherwise |
| 22 | + * Check if the lock exists |
25 | 23 | */ |
26 | | - pull(key: string): Promise<string | undefined> |
| 24 | + exists(key: string): Promise<boolean> |
27 | 25 |
|
28 | 26 | /** |
29 | | - * Put a value in the cache. |
30 | | - * If `ttl` is not defined, the value will be stored forever |
31 | | - * Returns true if the value was set, false otherwise |
| 27 | + * Extend the lock expiration |
32 | 28 | */ |
33 | | - set(key: string, value: string, ttl?: number): Promise<boolean> |
| 29 | + extend(key: string, duration: Duration): Promise<void> |
34 | 30 |
|
35 | 31 | /** |
36 | | - * Check if a key exists in the cache |
37 | | - */ |
38 | | - has(key: string): Promise<boolean> |
39 | | - |
40 | | - /** |
41 | | - * Remove all items from the cache |
42 | | - */ |
43 | | - clear(): Promise<void> |
44 | | - |
45 | | - /** |
46 | | - * Delete a key from the cache |
47 | | - * Returns true if the key was deleted, false otherwise |
48 | | - */ |
49 | | - delete(key: string): Promise<boolean> |
50 | | - |
51 | | - /** |
52 | | - * Delete multiple keys from the cache |
53 | | - */ |
54 | | - deleteMany(keys: string[]): Promise<boolean> |
55 | | - |
56 | | - /** |
57 | | - * Closes the connection to the cache. |
58 | | - * Some drivers may not need this |
| 32 | + * Disconnect the store |
59 | 33 | */ |
60 | 34 | disconnect(): Promise<void> |
61 | 35 | } |
62 | 36 | ``` |
63 | 37 |
|
64 | | -Similarly, the `L1CacheDriver` interface is the same, except that it is not async. |
65 | | - |
66 | | -So this should be quite easy to implement. Feel free to take a lot at [the existings drivers](https://github.com/Julien-R44/bentocache/tree/develop/drivers) implementations for inspiration. |
| 38 | +Feel free to take a lot at [the existing drivers](https://github.com/Julien-R44/verrou/tree/develop/src/drivers) implementations for inspiration. |
67 | 39 |
|
68 | | -Also note that your driver will receive two additional parameters in the constructor : `ttl` and `prefix`. These parameters are common to every drivers and their purpose is explained in the [options](../options.md) page. |
69 | | - |
70 | | -Once you defined you driver, you can create a factory function that will be used by Bentocache to create instances of your driver at runtime. The factory function must be something like this: |
| 40 | +Once you defined you driver, you can create a factory function that will be used by Verrou to create instances of your driver at runtime. The factory function must be something like this: |
71 | 41 |
|
72 | 42 | ```ts |
73 | | -import type { CreateDriverResult } from 'bentocache/types' |
| 43 | +import type { CreateDriverResult } from 'verrou/types' |
74 | 44 |
|
75 | | -export function myDriver(options: MyDriverOptions): CreateDriverResult<MyDriver> { |
76 | | - return { |
77 | | - options, |
78 | | - factory: (config: MyDriverOptions) => new MyDriver(config) |
79 | | - } |
| 45 | +export function myStore(config: MyStoreOptions): CreateDriverResult<MyStoreOptions> { |
| 46 | + return { config, factory: () => new MyDriver(config) } |
80 | 47 | } |
81 | 48 | ``` |
82 | 49 |
|
83 | | -Finally, you can use your driver when creating a new instance of Bentocache: |
| 50 | +Finally, you can use your driver when creating a new instance of Verrou: |
84 | 51 |
|
85 | 52 | ```ts |
86 | | -import { BentoCache, bentostore } from 'bentocache' |
| 53 | +import { Verrou } from 'verrou' |
| 54 | +import { myStore } from './my_store.js' |
87 | 55 |
|
88 | | -const bento = new BentoCache({ |
| 56 | +const verrou = new Verrou({ |
89 | 57 | default: 'myStore', |
90 | 58 | stores: { |
91 | | - myStore: bentostore() |
92 | | - .useL2Layer(myDriver({ /* Your driver options */ })) |
| 59 | + myStore: myStore({ /* Your driver options */ }) |
93 | 60 | } |
94 | 61 | }) |
95 | 62 | ``` |
96 | 63 |
|
97 | 64 | ## Tests |
98 | 65 |
|
99 | | -If you want to test your driver and its compliance, Bentocache is shipped with a test suite for [Japa](https://japa.dev/docs) that you can use. Note that you will also need to have `@japa/assert` installed. Then, you can use it like this: |
| 66 | +If you want to test your driver and its compliance, Verrou is shipped with a test suite for [Japa](https://japa.dev/docs) that you can use. Note that you will also need to have `@japa/assert` installed. Then, you can use it like this: |
100 | 67 |
|
101 | 68 | ```ts |
102 | 69 | // title: tests/my_driver.spec.ts |
103 | 70 | import { test } from '@japa/runner' |
104 | | -import { registerCacheDriverTestSuite } from 'bentocache/test_suite' |
105 | 71 | import { MyDriver } from '../src/my_driver.js' |
106 | | - |
107 | | -registerCacheDriverTestSuite({ |
108 | | - test, |
109 | | - driver: MyDriver, |
110 | | - config: { |
111 | | - // Your driver options |
112 | | - } |
| 72 | +import { registerStoreTestSuite } from 'verrou/test_suite' |
| 73 | + |
| 74 | +test.group('My Store', (group) => { |
| 75 | + registerStoreTestSuite({ |
| 76 | + test, |
| 77 | + store: MyStore, |
| 78 | + config: { |
| 79 | + // Your driver options |
| 80 | + } |
| 81 | + }) |
113 | 82 | }) |
114 | 83 | ``` |
115 | 84 |
|
|
0 commit comments