Menu Developer Moovweb University

cache Namespace

A namespace containing methods for caching purposes.


.enable()
moov_cache
Source (L14)

Enables caching for the response, provided that the origin response allows for caching.

Return Value

`undefined`

Example

let cache = new Cache(fns, $, env);
cache.enable();
// => Result: the response is cached if the desktop response is cached; or
//    caching is ignored if the desktop response is not cached.

Back to top


.enable_for(seconds)
moov_cache
Source (L31)

Enables caching for the response for a specified period of time, provided that the origin response allows for caching.

Parameters

Name Type Description
seconds Number | String A number or string containing the number for the amount of time in seconds for which to cache the response.

Return Value

`undefined`

Example

let cache = new Cache(fns, $, env);
cache.enable_for(3300);
// => Result: the response is cached for 3300 seconds (via the Cache-Time
//    header) if the desktop response is cached; or caching is ignored if the
//    desktop response is not cached.

Back to top


.force()
moov_cache
Source (L52)

Enables caching for the response, regardless of whether the origin response allows for caching.

Return Value

`undefined`

Example

let cache = new Cache(fns, $, env);
cache.force();
// => Result: the response is cached.

Back to top


.force(seconds)
moov_cache
Source (L68)

Enables caching for the response for a specified period of time, regardless of whether the origin response allows for caching.

Parameters

Name Type Description
seconds Number | String A number or string containing the number for the amount of time in seconds for which to force cache the repsonse.

Return Value

`undefined`

Example

let cache = new Cache(fns, $, env);
cache.force_for(3300);
// => Result: the response is cached for 3300 seconds (via the Cache-Time
//    header).

Back to top


.html(tag, enc)
moov_cache
Source (L96)

Sets up HTML responses so that content (matching a given selector) passed in is fetched as needed, but still allows the remainder of the HTML response to be cached. Works in conjunction with Moovweb's cachify.js (client-side asset JavaScript library). Note: This functionality is experimental.

Parameters

Name Type Description
tag String A string for a CSS selector representing the dynamic elements on the page that need to be fetched.
enc String A string for the encoding, defaulted to "utf-8".

Return Value

`undefined`, provided that the original page is requested; or an array of strings for HTML elements to be re-fetched, if the flagged re-fetching version of the page is requested (indicated by a "_mw_cached_fragments=true" query parameter).

Example

// In index.js ("html" content-type conditional check)
const Cache = require("moov_cache");
let cache = new Cache(fns, $, env);
let cache_array = cache.html(".bing, .google", "utf-8");
let matcher = '_mw_cached_fragments=true';
if (env.path.indexOf(matcher) >= 0) {
  return { body: cache_array, htmlparsed: false };
}
// => HTML input:
//    <body>
//      <img class="google" src="http://www.google.com/img.jpeg" alt="selfie">
//      <img class="bing" src="http://www.bing.com/img.jpeg" alt="selfie">
//    </body>
// => HTML response output (original page):
//    <body>
//      <span data-cache-hold="1"></span>
//      <span data-cache-hold="2"></span>
//    </body>
// => HTML dynamic output (original page):
//    <body>
//      <img class="google" src="http://www.google.com/img.jpeg" alt="selfie">
//      <img class="bing" src="http://www.bing.com/img.jpeg" alt="selfie">
//    </body>
// => Returns (flagged re-fetch response):
//    [
//      "<img class=\"google\" src=\"http://www.google.com/img.jpeg\" alt=\"selfie\">",
//      "<img class=\"bing\" src=\"http://www.bing.com/img.jpeg\" alt=\"selfie\">"
//    ]

Back to top


Last updated Tue Nov 15 2016 22:28:28 GMT+0000 (UTC)