Invalidate a request

95 views
Skip to first unread message

Bsr S

unread,
Oct 18, 2013, 10:05:41 PM10/18/13
to angula...@googlegroups.com
Hello Json,

I am yet to try the library, but couldn't find an API to remove a stored cache value. Is there an easy way to invalidate a specific request (say GET /user/123 ).

thanks for your hard work,
bsr.

Jason Dobry

unread,
Oct 18, 2013, 10:48:13 PM10/18/13
to angula...@googlegroups.com
If you're just using Angular's $cacheFactory, then 

$cacheFactory.get('$http').remove('http://myapiserver.com/user/123'); 

should work.

Bsr S

unread,
Oct 19, 2013, 7:57:16 AM10/19/13
to angula...@googlegroups.com
Thanks for the response. So do you mean, I cannot use angular-cache, but angular's $cacheFactory.

Jason Dobry

unread,
Oct 19, 2013, 12:52:10 PM10/19/13
to angula...@googlegroups.com
No, you can use angular-cache to do anything $cacheFactory can do. You said you hadn't tried angular-cache, that's why I gave an example that doesn't use it.

Here's an example that uses angular-cache:

var postsCache = $angularCacheFactory('postsCache', { maxAge: 3600000, deleteOnExpire: 'aggressive' );

then, somewhere else:

$http.get('/posts/13', { cache: postsCache });
$http.get('/posts/76', { cache: postsCache });

then, somewhere else / later:

postsCache.get('http://myapiserver.com/posts/13'); // { id: 13, title: 'How to use angular-cache', ... }
postsCache.get('http://myapiserver.com/posts/76'); // { id: 76, title: 'Why angular-cache is better', ... }

postsCache.get('http://myapiserver.com/posts/13'); // undefined

wait an hour, because postsCache defined above deletes items that are more than an hour old: 

postsCache.get('http://myapiserver.com/posts/76'); // undefined

For more reading on $cacheFactory and $http, see this blog post: Power up Angular's $http service with caching.

Bsr S

unread,
Oct 19, 2013, 8:58:35 PM10/19/13
to angula...@googlegroups.com
Thanks Jason. Greatly appreciate your detailed response.

Jason Dobry

unread,
Oct 19, 2013, 9:00:46 PM10/19/13
to angula...@googlegroups.com
Happy to help!
Reply all
Reply to author
Forward
0 new messages