Menu Developer Moovweb University

moov_cache

Introduction

moov_cache is a MoovJS Node module that provides a number of methods to enable caching for your Moovweb-transformed project site.

Documentation

Example usage

To instantiate a new moov_cache object in your MoovJS project, you can use the following:

var moov_cache = require("moov_cache");
var cache = new moov_cache(fns, $, env);

In this example, we are using fns, $, and env global objects available in the Node.js VM where your code is executed. fns refers to moov_stdlib, used as a source of exports, $ is an instance of moov_cheerio, used as the HTML content selector, and env is a reference to the set of environment variables associated with a request as it goes through the Moovweb server.

API

cache.enable()

Enables the caching header for the page.

This function will set the cache header of your page to true with no additional parameters

cache.enable_for(seconds)

Enables caching of things for a specified period of time.

This function will set the necessary headers to tell Moovweb to cache the current response. The parameter is the number of seconds to cache things for.

cache.force()

Adds a forced caching header to the page.

This function will set the cache control header to a non-zero TTL. The cache headers will be set regardless of the upstream response's cache headers (even if its set to max-age 0 for example).

cache.force_for(seconds)

Forces caching of things for a specified period of time in Moovweb's cache only.

This function will set the necessary headers to tell Moovweb to cache the current response and strip other caching headers out. The parameter is the number of seconds to cache things for.

html(tag,enc)

This html function sets up the page so that content you label dynamic is fetched as needed, but still allows the static section of the page to be cached.

This will set up the page so that all chosen tag parts(e.g. div) with the "dynamic" attribute are stripped out and will be loaded later. If it is a fragment/dynamic hit, content in the cachebox will be stored into a JSON array and get returned.

Unlike tritium html scope, enc argument will not change the encoding but just export "Content-Type-Charset" to be the enc. If enc is undefined, the encoding flag will set "utf-8" as default.

dynamic_section(tag,selector)

This function goes hand-in-hand with the cache function to allow dynamic sections to be flagged and loaded later. Use this function to specify dynamic content that should not be cached.

Development

Quick Start

$ cp .npmrc.example .npmrc
$ npm install

This will build the Node modules that moov_cache depends under the local moov_cache directory. The npm-shrinkwrap.json file is used to version-lock all dependencies.

Run tests

$ npm test

Run Locally via Node CLI

$ node --harmony
...
> var moov_cache = require("./main.js");
Last updated Tue Nov 15 2016 22:28:28 GMT+0000 (UTC)