I want to cache the resource object which gets returned from service.js so that again and again API is not hit and response time is fast. How can I use it? Tried few samples but couldnt made it work.
My controller.js
app.controller('MyCtrl2', ['$scope', '$rootScope', '$location', 'UserFactory', 'ProductFactory', '$q', 'Reddit', function ($scope, $rootScope, $location, UserFactory, ProductFactory, $q, Reddit) {
$scope.casualShirts = function () {
console.log("casualshirts");
$rootScope.home = ProductFactory.home.query({productcategory: 'Men Casual Shirts'});
var pagesShown = 1;
var pageSize = 21;
$scope.paginationLimit = function(data) {
//alert("34");
return pageSize * pagesShown;
};
$scope.hasMoreItemsToShow = function() {
return pagesShown < ($rootScope.home.length / pageSize);
};
$scope.showMoreItems = function() {
pagesShown = pagesShown + 1;
};
$location.path('/view2');
}
}]);
my service.js
services.factory('ProductFactory', ['$resource', '$rootScope', '$cacheFactory', function ($resource, $rootScope, $cacheFactory) {
/*alert("I am here service");*/
console.log("casualshirts service");
return {
home: $resource('/rest/products/:productcategory', {}, {
query: {method: 'GET', isArray: true,cache: $cacheFactory, },
create: {method: 'POST'}
})
}]);
Also, how can I set the expiry wherein after some time, cache gets refreshed.
A jsfiddle will be a great help. Thanks a to all brilliants out here.