Created
April 18, 2013 10:11
-
-
Save antonellopasella/5411649 to your computer and use it in GitHub Desktop.
Implementing your own cache in AngularJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍