Docs
Guide
Data Caching

Data Caching

You can improve server performance by caching heavy data or operations for reuse.

#Usage

Data caching is still an experimental feature and is disabled by default. You need to enable it manually before use.

#Using Plugin

You can activate data caching through the caching plugin.

Typescript
import { defineConfig } from "dobs";
import { cachePlugin } from "dobs/experimental";
 
export default defineConfig({
  plugins: [cachePlugin()],
});

Then, to enable caching for specific handlers, prefix the handler key with $.

Typescript
import { defineRouter } from "dobs";
 
export default defineRouter({
  $GET(req, res) {
    // caching enabled
  },
});

#Using Wrapper

Caching is also provided through a helper function. To enable caching, pass a cache instance as the second argument to defineRouter().

Typescript
import { defineRouter } from "dobs";
import { useCache } from "dobs/experimental";
 
export default defineRouter((req, res) => {}, [useCache()]);

INFO

If you are not using defineRouter(), you can wrap your router object instead.

Typescript
import { useCache } from "dobs/experimental";
 
const cache = useCache();
 
module.exports = cache((req, res) => {});

#Cache Unique ID

Typescript
const id =
  normalizePath(req.URL.pathname) + "-" + (method ?? req.method.toUpperCase());

The unique cache ID is generated as shown above.

It includes:

  • pathname
  • method