From 9edec8ef3f53b5c1b9c3c2d2c8e55a1d5f74a520 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 27 Apr 2015 16:31:47 +0200 Subject: [PATCH] Adds Fastly cache backend --- lib/cartodb/cartodb_windshaft.js | 37 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/cartodb/cartodb_windshaft.js b/lib/cartodb/cartodb_windshaft.js index d43f748f..4828fe2f 100644 --- a/lib/cartodb/cartodb_windshaft.js +++ b/lib/cartodb/cartodb_windshaft.js @@ -4,6 +4,11 @@ var Windshaft = require('windshaft'); var os = require('os'); var HealthCheck = require('./monitoring/health_check'); +var SurrogateKeysCache = require('./cache/surrogate_keys_cache'); +var NamedMapsCacheEntry = require('./cache/model/named_maps_entry'); +var VarnishHttpCacheBackend = require('./cache/backend/varnish_http'); +var FastlyCacheBackend = require('./cache/backend/fastly'); + if ( ! process.env.PGAPPNAME ) process.env.PGAPPNAME='cartodb_tiler'; @@ -30,14 +35,21 @@ var CartodbWindshaft = function(serverOptions) { // var template_baseurl = global.environment.base_url_templated || '(?:/maps/named|/tiles/template)'; - var SurrogateKeysCache = require('./cache/surrogate_keys_cache'), - NamedMapsCacheEntry = require('./cache/model/named_maps_entry'), - VarnishHttpCacheBackend = require('./cache/backend/varnish_http'), - varnishHttpCacheBackend = new VarnishHttpCacheBackend( - serverOptions.varnish_host, - serverOptions.varnish_http_port - ), - surrogateKeysCache = new SurrogateKeysCache(varnishHttpCacheBackend); + var surrogateKeysCacheBackends = []; + + if (serverOptions.varnish_purge_enabled) { + surrogateKeysCacheBackends.push( + new VarnishHttpCacheBackend(serverOptions.varnish_host, serverOptions.varnish_http_port) + ); + } + + if (!!serverOptions.fastly.enabled && !!serverOptions.fastly.apiKey && !!serverOptions.fastly.serviceId) { + surrogateKeysCacheBackends.push( + new FastlyCacheBackend(serverOptions.fastly.apiKey, serverOptions.fastly.serviceId) + ); + } + + var surrogateKeysCache = new SurrogateKeysCache(surrogateKeysCacheBackends); function invalidateNamedMap (owner, templateName) { surrogateKeysCache.invalidate(new NamedMapsCacheEntry(owner, templateName), function(err) { @@ -46,12 +58,9 @@ var CartodbWindshaft = function(serverOptions) { } }); } - - if (serverOptions.varnish_purge_enabled) { - ['update', 'delete'].forEach(function(eventType) { - templateMaps.on(eventType, invalidateNamedMap); - }); - } + ['update', 'delete'].forEach(function(eventType) { + templateMaps.on(eventType, invalidateNamedMap); + }); // boot var ws = new Windshaft.Server(serverOptions);