Dominique Dumont: Using custom cache object with AngularJS $http
Hello
At work, I ve been bitten by the way AngularJS handles cache by default when using $https service. This post will show a simple way to improve cache handling with $http service.
The service I m working on must perform the followings tasks:
- retrieve data from a remote server.
- save data to the same server.
- retrieve the saved data and some extra information generated by the server to update a UI
- $http.get(url, cache: true )
- $http.post(url)
- $http.get(url, cache: false )
- $http.get(url, cache: true )
- $http.get(url, cache: true ) returns foo
- $http.post(url) stores bar
- $http.get(url, cache: false ) returns bar
- Completely flush the cache using removeAll() function.
- Clear the cache for the specific URL using remove(key) function. The only hitch is that the key used by $http is not documented.
angular.module('app').factory('myService', function ($http, $cacheFactory)
var myFooUrl = '/foo-rest-service';
// create cache object. The cache id must be unique
var fooCache = $cacheFactory('myService.foo');
function getFooData ()
return $http.get( myFooUrl, cache: fooCache );
;
function saveFooData(data)
return $http.post( myFooUrl, cache: fooCache ).then(function()
myCache.removeAll() ;
);
);
The code above ensures that:
- cached data for foo service is always consistent
- http get requests are not sent more than necessary
- cache is not refreshed if the data on the server are updated by another client
- cache is flushed when only the browser page is reloaded
In short succession a new firmware from Kobo, this time 3.18.0. And here is my mega-update. On request from a reader I have now prepared updates for all three hardwares, Mark4 (Glo), Mark5 (Aura), and Mark6 (GloHD).

First I tried to find a sensible Debian package to record the
battery status, assuming that this must be a problem already handled
by someone else. I found
In a few weeks, I will have the opportunity to offer a weekend workshop to selected and motivated high school students
When I wrote about
[As You Like It, Act III, Scene V]
Hey Beautiful
I thought of you on the Fourth of July; which does not, in and of itself,
distinguish the day from any other since I met you. It was remarkable
only in that it was justifiable, given our conversation about fireworks
displays. Of course, I'm happy to take the flimsiest of displays as an
excuse to mark you.
I'm home alone right now, after watching The Bad Seed. Truth be told,
the shadows keep spooking me. I can't seem to stop myself from imagining
precocious blonde murderers in them. It's a manageable silliness, but
made a little less so by the fact that I forgot to lock the door. Less
troublesome than the night I spent after Ringu (the original Japanese
version of The Ring). I finished watching it in the wee hours of the
morning, and I wanted to go to sleep. I was in half a stupor, but the
incessant inner critic in me kept imagining all the changes that could
have been made to make the movie more truly unsettling until visions of
Obake were swimming around me.
Ordinarily I doubt I'd be bothered particularly by a 50's classic, but
went back up to Boston this afternoon. 's at a conference in
Finland, so I invited him down to visit while I have the apartment all
to myself. It was strange to have a visitor actually in my home for
whom I didn't have to play at being contented. At any rate, being
around for three days essentially meant carrying on a three-day-long
conversaiton, and the abrupt drop of sociability makes me feel my
isolation a little more acutely.
We watched The Way We Were together, and agreed that it should be
remade with casting that actually works. I hadn't seen it before, and
was surprised to find it unusually nuanced and substantial, yet still
not good. It was nice to have someone around who would dissect it with
me afterward. It's been a while since I actually discussed a film with
someone. Partly out of my own fault; I don't always enjoy verbalizing
my opinions of movies immediately after watching them if I've found them
in the least bit moving. I guess I consider the aftertaste part of the
experience. In this case, we both felt the film had missed its
emotional mark so it wasn't so much of an issue. On the other hand, I
don't find most movies moving. I find them frustratingly flawed, and by
the time they end I'm raring to rant about their petty contradictions
and failures of logic. I think it might give people the impression that
I don't actually make any effort to tease out the messages filmmakers
weave into their work. Or maybe I'm just making excuses for having
uninteresting friends. Either way, it was pleasant to be in the company
of someone eager to tolerate the convolutions of my thought process.
On Wednesday night, I have a date to meet up with some former
co-workers/friends that I've been passively avoiding for several years
now. Every time I fail to carefully manage my visibility, people seem
to come flooding back into my life. This time the culprit was a day
spent logged into instant messaging without stringent privacy settings.
I should feel lucky for that, I suppose. I'm not sure how I actually
feel. One of the formers is a woman I was very close to, as far as
most of the world her included could discern. The other is a Boston
boy I admired for the touch of golden child in the air that hung about
him. The main theme of his life was (and I suspect still is) getting
to drinks with friends at one of his regular bars at the end of every
evening. Which did not at all stop him from being productive,
interested in the world, and bright. If he had been a girl I would
have been hateful with envy. Instead he's always stood out in my
memory as the only person I've had a bit of a crush on despite not
finding him particularly intellectually stimulating. A month or so
ago he sold his company to google. Now he spends a lot of time out of
town giving lectures. I suspect I may be generally happy for him,
and I'm not quite sure what to do with that.
Thursday I'm leaving for a few days in Denver. I wish I hadn't
scheduled it for a time when I could have been the sole occupant of
my domicile, but other than that I'm looking forward to it. I've
no idea what I'll do there, but at least that means I really am
going someplace that wouldn't occur to me outside of a peculiar set
of constraints. I think it would be advisable to work out the
transportation system before I depart, though.
I hope this letter finds you relatively satisfied, at a minimum.
I don't actually need to tell you how much I miss talking to you, do I?
You're wonderful.
Affectionally, as always,
