Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is cacheTime in the sitemap node.js module?

The documentation of the sitemap node.js module does not explain what cacheTime is. Why is it needed to generate a sitemap? What is its purpose?

like image 575
Jérôme Verstrynge Avatar asked Feb 23 '14 16:02

Jérôme Verstrynge


1 Answers

The cacheTime is how long the sitemap.js module will wait before regenerating the sitemap.xml file from the list of urls given to it.

ie. on the first request, a sitemap.xml file is generated and placed in the cache. Subsequent requests read the sitemap from the cache, until it expires and is regenerated.

I agree it could be clearer, but the source code makes it pretty clear.

According to the source code at sitemap.js, line 136:

// sitemap cache
  this.cacheEnable = false;
  this.cache = '';
  if (cacheTime > 0) {
    this.cacheEnable = true;
    this.cacheCleanerId = setInterval(function (self) {
      self.clearCache();
    }, cacheTime, this);
  }

and line 187:

Sitemap.prototype.toString = function () {
  var self = this
    , xml = [ '<?xml version="1.0" encoding="UTF-8"?>',
              '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'];

  if (this.cacheEnable && this.cache) {
    return this.cache;
  }

  // TODO: if size > limit: create sitemapindex

  this.urls.forEach( function (elem, index) {
    // SitemapItem
    var smi = elem;

Specifically:

if (this.cacheEnable && this.cache) {
    return this.cache;
}

And the clear cache operation has a setInterval on it equal to the cacheTime parameter given.

Note the implication that your sitemap could become out of date if your urls change and your cacheTime has not triggered a clearing of the sitemap cache.

like image 87
Ryan Weir Avatar answered Nov 10 '22 17:11

Ryan Weir