- [Instructor] As mentioned, we don't cache images right now
because our caching system does not work with streams.
Let's try that out real quick.
So I'm in my speaker service and there,
I disable the image handling middleware
and I restart it.
And back on the page,
we see that we don't get any images now.
Let's change it back real quick and restart,
and it works again.
This is because we are using the response type stream here.
So we have to add functionality now
that let's us also cache images
and we will do that in the file system.
For that I create in the root of the conference app
a new folder and I call it _imagecache.
So this is something you would not commit
to version control system,
it's just for the locally running app.
Now, on our speakers chest class,
I will add const.
Fs because we want to utilize file system functions
require fs.
I am also bringing in util const util equals require util
and in I want to create a promisified version of fs existed.
Tests if a file exist and they can do that
by adding const fsexists equals util.promisify fs.exists.
Now I go back into the call service function
to extend it to also support images.
And there after I create the cache key,
I am creating a new variable
using let cacheFile equals null.
And after that I want to find out if this is a image
that we are requesting.
So I add if requestOptions.responseType.
So if this is set and requestOptions responseType
and copy pasting that equals stream,
I will set a possible cache file
which will then be cacheFile equals
and I'm now creating the path to this image cache
and I trust you that you know now how
to also use a config variable for that if needed.
But now I'm hard coding that to dirname one level up,
another level up _imagecache.
And this file should be found under the cache key.
Now you'll also know why we used a hash here
because for filenames a hash is a better option
than concatenated string.
Then everything stays the same until you're down
at the result.
So after we evaluate the regular to cachekey,
we will now say if cache file,
this means okay that is a request for file.
I will find out if this file exists
by adding const exists equals await fs exists,
now the promisified version of that cacheFile
and if it exists,
if exists we will return fs createReadStream.
So now we're again returning a stream
with the cacheFile.
Now we also have to differentiate the storing
of the cache result, so if not cacheFile
then we obviously are on a regular request and do a else
branch here as well.
So in this case we will simply store that
in our cache property and otherwise,
I can now add const vs and I'm using fs createWriteStream
to the cache file.
This means I want to write to this file and I will
then pipe this result into this writeStream.
And in the end I simply return the result.
Now let's try that out.
I'm restarting the application
and I'm accessing the website.
And I'm reloading it once.
And if we now look into our image cache directory,
we see that we created a lot of entries
for those different images that the page was showing.
And now if I, as we did it before,
disable the image serving middleware and restart the app.
And look at the website.
I can restart and we see that those images are now
provided by the cache.
Our micros habit infrastructure
is now really resilient
and it covers pretty much everything.
Of course each fault tolerance it has its limits
and its also a matter of effort and cost how far you want
to take it.
In our case, one single point of failure is
to service registry.
If the registry fails the application won't work.
This could be solved by providing several instances
of the registry that maintain their entries in a database.
The main application could then go through
all possible service registry instances until
it finds one that's online.
Also right now, the page will fail
if there was no matching service found.
But we basically solved that problem
because there can be unlimited number of instances
for one service.
And this provides a fair amount of fault tolerance.
Even when services get updated in place.
One more common technique for adding resiliency
is missing dow.
And we will cover that in the next chapter.
But before we do that let's real quick fix back
our speaker service.
So that the image route works again.