Grind’s Cache provider integrates Grind with cache-manager.

You can access cache via app.cache.

First, add the grind-cache package via your preferred package manager:

yarn add grind-cache

Next, you’ll need to add CacheProvider to your app providers in app/Boostrap.js:

import Grind from 'grind-framework'
import { CacheProvider } from 'grind-cache'
 
const app = new Grind()
app.providers.push(CacheProvider)

grind-cache is included as part of grind-core and grind-core-frontend. If you’re using one of these bundle packages, you don’t need to explicitly add the grind-cache package.

To retrieve a value from the cache, call cache.get(key). It will return a promise that will resolve with the value, or null if it doesn’t exist.

app.cache.get(`user-${id}`).then(user => {
  if(user) {
    Log.comment('Loaded cached user', user)
  } else {
    // …retreive from db… 
  }
})

To cache a value in the store, call cache.set(key, value). It will return a promise that resolves once it’s been stored.

app.cache.set(`user-${id}`, user, { ttl: 86400 }).then(() => {
  Log.comment('User has been stored')
})

The third { ttl } parameter is optional, if you don’t pass it, it uses the default value as defined in your config.

Cache also has a convenient cache.wrap(key, retreiver) function that will first try to read the key from the cache, and if it’s missing, call the retreiver and store it.

app.cache.wrap(`user-${id}`, () => User.findById(id), { ttl: 86400 }).then(user => {
  Log.comment('Loaded user', user)
})

The third { ttl } parameter is optional, if you don’t pass it, it uses the default value as defined in your config.

To remove a cached value from the store, call cache.del(key). It will return a promise that resolves once it’s been removed.

app.cache.del(`user-${id}`).then(() => {
  Log.comment('User has been purged')
})

By default, only in-memory is supported. If you wish to persist cache (or share between other instances), you’ll want to install a different engine.

cache-manager supports a variety of engines, including fs, redis and memcache:

yarn add cache-manager-redis
yarn add cache-manager-mongodb
yarn add cache-manager-mongoose
yarn add cache-manager-fs
yarn add cache-manager-fs-binary
yarn add cache-manager-memcached-store

Your cache config should live in /config/cache.json.

Here’s an example of a config file showing support for memory, fs and redis:

{
  "default": "memory",
  "stores": {
    "memory": {
      "driver": "memory",
      "max": 10000,
      "ttl": 86400
    },
    "fs": {
      "driver": "fs",
      "max": 1000000000,
      "path": "storage/cache",
      "ttl": 86400
    },
    "redis": {
      "driver": "redis",
      "host": "localhost",
      "port": 6379,
      "ttl": 86400
    }
  }
}

The default key tells the Cache provider which store it configure app.cache to use.

If you wish to access the non-default cache store, you can load it via the Cache function.

import { Cache } from 'grind-cache'
 
const store = Cache('redis', app)

The first parameter passed to Cache() is the store. This can either be the name of a store in your cache.json config file, or you can pass in a full config object.

The second parameter passed to Cache() is the instance of app. If you pass a config object, the app instance is not required.

Edit