diff --git a/bower.json b/bower.json index 4a7bc1f2..6d3c6dc4 100644 --- a/bower.json +++ b/bower.json @@ -11,7 +11,8 @@ "angular-bootstrap": "0.11.2", "angular-local-storage": "0.0.7", "angular-elastic": "2.4.0", - "angular-moment": "0.8.2" + "angular-moment": "0.8.2", + "angular-cache": "3.2.2" }, "devDependencies": { "angular-mocks": "1.2.25", diff --git a/src/app/services/http/http_cache_handler.js b/src/app/services/http/http_cache_handler.js new file mode 100644 index 00000000..42bac0f4 --- /dev/null +++ b/src/app/services/http/http_cache_handler.js @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * An HTTP request interceptor that pays attention to POST/PUT/DELETE operations + * on specific resources, and replaces the local cached data with the resulting + * value. + */ +angular.module('sb.services').factory('httpCacheHandler', + function ($q, $cacheFactory) { + 'use strict'; + + var $httpDefaultCache = $cacheFactory.get('$http'); + + return { + /** + * Handle a success response. + */ + response: function (response) { + var method = response.config.method; + var url = response.config.url; + var obj = response.data; + + // Ignore GET methods. + switch (method) { + case 'POST': + if (obj.hasOwnProperty('id')) { + $httpDefaultCache.put(url + '/' + obj.id, obj); + } + break; + case 'PUT': + $httpDefaultCache.put(url, obj); + break; + case 'DELETE': + $httpDefaultCache.remove(url); + break; + default: + break; + } + + return response; + } + }; + }) + // Attach the HTTP interceptor. + .config(function ($httpProvider) { + 'use strict'; + $httpProvider.interceptors.push('httpCacheHandler'); + }); \ No newline at end of file diff --git a/src/app/services/service/resource_factory.js b/src/app/services/service/resource_factory.js index fe8740f1..a4ca72be 100644 --- a/src/app/services/service/resource_factory.js +++ b/src/app/services/service/resource_factory.js @@ -50,7 +50,7 @@ angular.module('sb.services') }, 'get': { method: 'GET', - cache: false + cache: true }, 'update': { method: 'PUT' @@ -62,6 +62,7 @@ angular.module('sb.services') method: 'GET', isArray: true, responseType: 'json', + cache: false, params: { limit: getLimit } @@ -71,6 +72,7 @@ angular.module('sb.services') url: searchUrl, isArray: true, responseType: 'json', + cache: false, params: { limit: getLimit } diff --git a/src/app/storyboard/module.js b/src/app/storyboard/module.js index 9f9122b7..732af788 100644 --- a/src/app/storyboard/module.js +++ b/src/app/storyboard/module.js @@ -26,7 +26,8 @@ angular.module('storyboard', [ 'sb.services', 'sb.templates', 'sb.dashboard', 'sb.pages', 'sb.projects', 'sb.auth', 'sb.story', 'sb.profile', 'sb.notification', 'sb.search', 'sb.admin', 'sb.subscription', 'sb.project_group', 'ui.router', - 'ui.bootstrap', 'monospaced.elastic', 'angularMoment']) + 'ui.bootstrap', 'monospaced.elastic', 'angularMoment', + 'angular-data.DSCacheFactory']) .constant('angularMomentConfig', { preprocess: 'utc', timezone: 'UTC' @@ -57,4 +58,16 @@ angular.module('storyboard', function () { $state.go('index'); }); - }); + }) + .run(function ($http, DSCacheFactory) { + 'use strict'; + + DSCacheFactory.createCache('defaultCache', { + // Items added to this cache expire after 1 minute. + maxAge: 60000, + // Items will be deleted from this cache only on check. + deleteOnExpire: 'passive' + }); + + $http.defaults.cache = DSCacheFactory.get('defaultCache'); + }); \ No newline at end of file diff --git a/src/index.html b/src/index.html index a47bc25c..8c83bf27 100644 --- a/src/index.html +++ b/src/index.html @@ -35,6 +35,7 @@ +