I have a service that gets data related to the current logged in user from the server, which internally caches the returned user object. The user service:
var appCache = $cacheFactory.get('appCache');
var userInfoFromCache = appCache.get('userInfo');
if (userInfoFromCache) {
$log.debug("User info cache hit");
deferred.resolve(userInfoFromCache);
} else{
$log.debug("User info cache miss");
//do work and get data from server
appCache.put('userInfo', userInfo);
}
The service works great. I am calling the user service from multiple places on load such as top navigation and inside the home view. I also call the service in run section of app.js:
.run(function(userService) {
userService.getUser();
})
Since I am calling the service from multiple places at once, i am ending up with multiple server calls. Is there a way I can setup a lock on the code so that subsequent calls to the userService is served by cache?