Configure Caching for a Model

What you’ll build

While following this guide, you will start an oeCloud Framework based NodeJS application and -

  • create a new Model called Product
  • configure the new model for caching

What you’ll need

To complete this guide, you will need the following -

  • an understanding of what a Model is in the context of Loopback, which is the basis of the oeCloud Framework. You can get this by perusing the documentation here
  • a running NodeJS application built using the oeCloud Framework. You can get this here
  • a working REST client. You can use the Linux cURL command as a REST client if you have access to a Linux machine or have Git Bash installed on your Windows machine. REST addons for browsers, like Postman for Firefox or Google Chrome also can be used to complete this guide.
  • a text editor for editing a configuration file

How to complete this guide

Caching can be enabled for a Model in a NodeJS application using the oeCloudFramework in one of two ways.

  • Method 1: Marking the Model to be cacheable within the definition of the Model
  • Method 2: Marking the Model to be cacheable in a configuration file

Method 1: Marking the Model to be cacheable within the definition of the Model

This is done at Model creation time. While creating the model, add the property

cacheable: true

to the Model definition JSON.

For example, create the Product model with the following definition:

{
    "cacheable": true,
    "name": "Product",
    "base": "BaseEntity",
    "strict": false,
    "plural": "Products",
    "idInjection": true,
    "options": {
        "validateUpsert": true
    },
    "properties": {
        "code": {
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "category": {
            "type": "string"
        },
        "price": {
            "type": "number"
        },
        "offeredSince": {
            "type": "date"
        },
        "active": {
            "type": "boolean"
        },
        "description": {
            "type": "string"
        }
    }
}

Do the following to create the above cached Model:

  • Open a command window (“cmd” in Windows OS, “bash” Terminal in Linux)
  • copy the following command to the command prompt, change the host and port, if necessary, and hit the ENTER key:

curl -v “http://localhost:3000/api/ModelDefinitions” -H “Content-Type: application/json” -H “Accept: application/json” –data “{"cacheable": true, "name": "Product","base": "BaseEntity","strict": false,"plural": "Products","idInjection": true,"options": {"validateUpsert": true},"properties": {"code": {"type": "string"},"name": {"type": "string"},"category": {"type": "string"},"price": {"type": "number"},"offeredSince": {"type": "date"},"active": {"type": "boolean"},"description": {"type": "string"}}}”

This creates the Product Model as a cacheable Model.

Method 2: Marking the Model to be cacheable in a configuration file

In case a model was not marked as cacheable at the time of creating it as described above, the model can still be made cacheable by editing a configuration file. The file to be edited is server/config.json

To use this feature, do the following:

  • edit the server/config.json file with a text editor and add the following entry to it:
"modelstocache": ["Product"]
  • restart the (node) application

This will enable caching for the Product model.

Note: Caching can be disabled globally for all Models irrespective of whether they are marked as cacheable. This is achieved by adding a property

"disablecaching": true

to the server/config.json file. By default, the disablecaching property is absent from the config file and hence its value is treated as false, thereby allowing any Model to be cached if marked cacheable by either of the two methods described above.

Summary

We have seen how we can enable caching for a Model in the oeCloudFramework based NodeJS application in two different ways.