admin管理员组文章数量:1390812
I'm new to angular and I need to cache data to increase the performance. For this I'm using $cacheFactory by Angular.
When I try,
myApp.factory('myCache', function($cacheFactory) {
return $cacheFactory('myData');
});
With
myApp.controller('MyMain', ['$scope', '$http', 'myCache',
function ($scope, $http, myCache) {
var cache = myCache.get('myData');
if (cache) {
$scope.variable = cache;
}
else {
$http.get('')
.success(function(data) {
$scope.variable = data;
myCache.put('myData', data);
}
);
}
}
This works fine, but it only caches one set of data, as I want to cache multiple set of data I uses
myApp.factory('myCache', function($cacheFactory) {
return {
get : function(cacheKey){
return $cacheFactory(cachekey);
}
}
});
So if I go to another page I can cache another set of data like this,
myApp.controller('MyMain', ['$scope', '$http', 'myCache',
function ($scope, $http, myCache) {
var cache = myCache.get('myData2');
if (cache) {
$scope.variable = cache;
}
else {
$http.get('')
.success(function(data) {
$scope.variable = data;
myCache.put('myData2', data);
}
);
}
}
etc.
However in the latter way although it doesn't give any error no data is returned and no ajax call is made to cache the data.
How can I fix this? And cache multiple sets of data using $cacheFactory?
I'm new to angular and I need to cache data to increase the performance. For this I'm using $cacheFactory by Angular.
When I try,
myApp.factory('myCache', function($cacheFactory) {
return $cacheFactory('myData');
});
With
myApp.controller('MyMain', ['$scope', '$http', 'myCache',
function ($scope, $http, myCache) {
var cache = myCache.get('myData');
if (cache) {
$scope.variable = cache;
}
else {
$http.get('http://www.example./path/to/api/endpoint')
.success(function(data) {
$scope.variable = data;
myCache.put('myData', data);
}
);
}
}
This works fine, but it only caches one set of data, as I want to cache multiple set of data I uses
myApp.factory('myCache', function($cacheFactory) {
return {
get : function(cacheKey){
return $cacheFactory(cachekey);
}
}
});
So if I go to another page I can cache another set of data like this,
myApp.controller('MyMain', ['$scope', '$http', 'myCache',
function ($scope, $http, myCache) {
var cache = myCache.get('myData2');
if (cache) {
$scope.variable = cache;
}
else {
$http.get('http://www.example./path/to/api/endpoint')
.success(function(data) {
$scope.variable = data;
myCache.put('myData2', data);
}
);
}
}
etc.
However in the latter way although it doesn't give any error no data is returned and no ajax call is made to cache the data.
How can I fix this? And cache multiple sets of data using $cacheFactory?
Share Improve this question asked Dec 22, 2014 at 12:43 rkshrksh 4,05010 gold badges52 silver badges72 bronze badges1 Answer
Reset to default 6If your URL is unique for the data that you are querying, then you should use the built in cache inside $http
:
$http.get('http://www.example./path/to/api/endpoint', { cache: true }).success(...
This will cache based on the uniqueness of the URL parameter in cacheProvider
for you.
Here is the documentation for this functionality.
本文标签: javascriptCaching multiple data in Angular cacheFactoryStack Overflow
版权声明:本文标题:javascript - Caching multiple data in Angular $cacheFactory? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744701895a2620610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论