Skip to main content

Make Your Own Cache Driver

Warlock ships with a few cache drivers out of the box, but you can easily make your own. All you need to do is implement the CacheDriver interface.

Example

Let's say we want to implement the MemCached cache driver. We'll start by creating a new file called memcached-cache-driver.ts in your project's src/cache directory.

import { CacheDriver } from "@mongez/warlock";
import Memcached from "memcached";

// first off let's define the options interface

export type MemCachedCacheDriverOptions = {
host: string;
port: number;
};

// now let's define our cache driver class

export class MemCachedCacheDriver
implements CacheDriver<Memcached, MemCachedCacheDriverOptions> {
//
}

Now let's see what we have done.

  • We have defined the options interface for our cache driver.
  • We have defined the cache driver class.

Our driver must implement the CacheDriver interface, it takes two type parameters:

  • The first one is the client type, this is the type of the client that the cache driver will be using.
  • The second one is the options type, this is the type of the options that the cache driver will be using.

Setting the Options

Now let's define our options property, this property will hold the cache driver options.

// ...

export class MemCachedCacheDriver
implements CacheDriver<Memcached, MemCachedCacheDriverOptions>
{
/**
* The cache driver options
*/
public options: MemCachedCacheDriverOptions = {} as any;

/**
* Set the cache driver options
*/
public setOptions(options: MemCachedCacheDriverOptions) {
this.options = options;

return this;
}
}

We defined the options property and set its type to MemCachedCacheDriverOptions. We also defined the setOptions method, this method will be used to set the cache driver options.

Connecting to the Cache Driver

To make the driver connection established, we need to define the connect method.

// ...

export class MemCachedCacheDriver
implements CacheDriver<Memcached, MemCachedCacheDriverOptions>
{
// ...

/**
* Connect to the cache driver
*/
public async connect() {
this.client = new Memcached(`${this.options.host}:${this.options.port}`);

return this;
}
}

Parsing the Key

The parseKey method is used to parse the key before it is used in the cache. This method is used to make sure that the key is in the correct format.

We will use Parse Cache Key utility to parse the key.

import { CacheDriver, parseCacheKey } from "@mongez/warlock";
import { GenericObject } from "@mongez/reinforcements";

// ...

export class MemCachedCacheDriver
implements CacheDriver<Memcached, MemCachedCacheDriverOptions>
{
// ...

/**
* Parse the key to be used in the cache
*/
public parseKey(key: string | GenericObject) {
return parseCacheKey(key);
}
}

This function will make sure that the key is in the correct format.

Setting a Value in the Cache

The set method is used to set a value in the cache. This method takes two parameters:

  • The first one is the key that will be used to store the value in the cache.
  • The second one is the value that will be stored in the cache.
// ...

export class MemCachedCacheDriver
implements CacheDriver<Memcached, MemCachedCacheDriverOptions>
{
// ...

/**
* Set a value in the cache
*/
public async set(key: string | GenericObject, value: any) {
const parsedKey = this.parseKey(key);

return new Promise((resolve, reject) => {
this.client?.set(parsedKey, value, 0, (error) => {
if (error) {
return reject(error);
}

resolve();
});
});
}
}