Skip to content

Instantly share code, notes, and snippets.

@antonellopasella
Created April 18, 2013 10:11
Show Gist options
  • Save antonellopasella/5411649 to your computer and use it in GitHub Desktop.
Save antonellopasella/5411649 to your computer and use it in GitHub Desktop.
Implementing your own cache in AngularJs
Implementing your own cache in AngularJs is quite easy.
Just use $cacheFactory:
app.factory('myService', function($resource, $cacheFactory) {
var cache = $cacheFactory('myService');
var User = $resource('/user/:userId', {userId:'@id'});
return {
getResource: function(userId) {
var user = cache.get(userId);
if (!user) {
user = User.get({userId:userId});
cache.put(userId, user);
}
return user;
}
};
});
@thehungrycoder
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment