{"action":"created","issue":{"url":"https://api.github.com/repos/mgonto/restangular/issues/46","repository_url":"https://api.github.com/repos/mgonto/restangular","labels_url":"https://api.github.com/repos/mgonto/restangular/issues/46/labels{/name}","comments_url":"https://api.github.com/repos/mgonto/restangular/issues/46/comments","events_url":"https://api.github.com/repos/mgonto/restangular/issues/46/events","html_url":"https://github.com/mgonto/restangular/issues/46","id":13978144,"number":46,"title":"Query Caching","user":{"login":"roboncode","id":820723,"avatar_url":"https://avatars.githubusercontent.com/u/820723?v=3","gravatar_id":"","url":"https://api.github.com/users/roboncode","html_url":"https://github.com/roboncode","followers_url":"https://api.github.com/users/roboncode/followers","following_url":"https://api.github.com/users/roboncode/following{/other_user}","gists_url":"https://api.github.com/users/roboncode/gists{/gist_id}","starred_url":"https://api.github.com/users/roboncode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/roboncode/subscriptions","organizations_url":"https://api.github.com/users/roboncode/orgs","repos_url":"https://api.github.com/users/roboncode/repos","events_url":"https://api.github.com/users/roboncode/events{/privacy}","received_events_url":"https://api.github.com/users/roboncode/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2013-05-05T13:35:35Z","updated_at":"2016-08-01T20:37:41Z","closed_at":"2013-05-07T03:12:24Z","body":"More of a question vs issue. In reviewing the code, you are using $resource for the calls. With $http, you can cache the query. Have you thought about query caching? or are you going to leave that outside of the scope of Restangular? Rob."},"comment":{"url":"https://api.github.com/repos/mgonto/restangular/issues/comments/236699967","html_url":"https://github.com/mgonto/restangular/issues/46#issuecomment-236699967","issue_url":"https://api.github.com/repos/mgonto/restangular/issues/46","id":236699967,"user":{"login":"isometriq","id":4261000,"avatar_url":"https://avatars.githubusercontent.com/u/4261000?v=3","gravatar_id":"","url":"https://api.github.com/users/isometriq","html_url":"https://github.com/isometriq","followers_url":"https://api.github.com/users/isometriq/followers","following_url":"https://api.github.com/users/isometriq/following{/other_user}","gists_url":"https://api.github.com/users/isometriq/gists{/gist_id}","starred_url":"https://api.github.com/users/isometriq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/isometriq/subscriptions","organizations_url":"https://api.github.com/users/isometriq/orgs","repos_url":"https://api.github.com/users/isometriq/repos","events_url":"https://api.github.com/users/isometriq/events{/privacy}","received_events_url":"https://api.github.com/users/isometriq/received_events","type":"User","site_admin":false},"created_at":"2016-08-01T20:37:41Z","updated_at":"2016-08-01T20:37:41Z","body":"Here my final version with a wrapper around the cache factory instance\r\n```\r\n(function() {\r\n\r\n    'use strict';\r\n\r\n    angular\r\n        .module('app.services')\r\n        .factory('API', API);\r\n\r\n    function API(Restangular, APICache, $window) {\r\n\r\n        var headers = {\r\n            'Content-Type': 'application/json',\r\n            'Accept': 'application/x.api.v1+json'\r\n        };\r\n\r\n        return Restangular.withConfig(function(RestangularConfigurer) {\r\n\r\n            RestangularConfigurer\r\n                .setBaseUrl('/api')\r\n                .setDefaultHeaders(headers)\r\n                .setDefaultHttpFields({cache:APICache.getCache()})\r\n\r\n                .setErrorInterceptor(function(response, operation) {\r\n\r\n                     if (response.status === 422 || response.status === 401) {\r\n                         if (response.data.errors) {\r\n                             for (var error in response.data.errors) {\r\n                                 ngToast.danger({className: 'danger', content: response.data.errors[error][0]});\r\n                                 return response.data.errors[error][0];\r\n                             }\r\n                         }\r\n                         if (response.message) {\r\n                             ngToast.danger({className: 'danger', content: response.message});\r\n                             return response.message;\r\n                         }\r\n                     }\r\n\r\n                     if (response.status === 500) {\r\n                         if (response.message) {\r\n                             ngToast.danger({className: 'danger', content: response.message});\r\n                             return response.message;\r\n                         }\r\n                     }\r\n                })\r\n\r\n                .setResponseInterceptor(function(response, operation) {\r\n\r\n                    if (APICache.isExpired() || operation === 'put' || operation === 'post' || operation === 'delete') {\r\n                        APICache.reset();\r\n                    }\r\n\r\n                    return response;\r\n                })\r\n\r\n                .addFullRequestInterceptor(function (element, operation, what, url, headers) {\r\n\r\n                    var token = $window.localStorage.satellizer_token;\r\n\r\n                    if (token) {\r\n                        headers.Authorization = 'Bearer ' + token;\r\n                    }\r\n                });\r\n        });\r\n    }\r\n\r\n    angular\r\n        .module('app.services')\r\n        .service('APICache', APICache);\r\n\r\n    function APICache($cacheFactory, $window) {\r\n\r\n        var cache = $cacheFactory('http');\r\n        var cacheTimer = (new Date()).getTime();\r\n        var cacheExpiration = 1000 * 60 * 5; // 5 min\r\n\r\n        this.getCache = function () {\r\n            return cache;\r\n        };\r\n\r\n        this.isExpired = function() {\r\n            var currentTime = (new Date()).getTime();\r\n            var cacheExpired = (currentTime - cacheTimer) > cacheExpiration;\r\n            return cacheExpired;\r\n        };\r\n\r\n        this.reset = function() {\r\n            cacheTimer = (new Date()).getTime();\r\n            cache.removeAll(); // todo could be improved/fine-tuned by passing type or url?\r\n        };\r\n    }\r\n\r\n})();\r\n```"}}
{"actor":{"display_login":"isometriq"}}