Everybody knows that Azure is a thing.
Some people know you can click some buttons, drag some sliders to get speedier sites and bigger bills.
When doing a Sitecore project recently, using all of Azures’ bells, whistles and PaaS, there were some gotchas. Some we knew we had to deal with, and others we learned the hard way.
This story will show how to solve one of them. Drum roll…
The Media Cache
In our project, we wanted to deploy to Azure daily. We do this by using ARM templates which creates Web App slots, deploys Sitecore baseline and our customizations, but that’s another story.
In Azure Web apps, the disk space is separate for each instance, app and slot. So, by doing blue/green deployments, everything on disk is wiped when the slots are swapped and we can’t have that, right?
Now, we all know we shouldn’t store anything of importance on disk. Storage should be done using….yes, a Storage.
By default, Sitecore pulls the media items from database, stores both the original, resized version and thumbnail in the
~/MediaCache folder usually in
This is fine for an on premise solution, but whenever the MediaCache is wiped, the images need to be fetched and resized again.
Unless you have the Azure slider to the very far right and using lots of CPU cores, you probably don’t want to spend your precious CPU cycles on resizing images.
Building a global solution we naturally use a CDN, but having more than 150+ PoPs world wide, it takes time before everything is available everywhere, new images are published and so on.
So yes, we have to move the cache to a persistent storage. Now, some (other) tools come with pre-built providers supporting S3, Azure Blob Storage etc. But not Sitecore. Good thing though, Sitecore is quite extensible and you can “with ease” build your own.
Enter Azure Blob Media Cache Provider.
For this to work, you need to implement 3 classes. But first…
Disclaimer. This code is provided as is by a non certified developer. Parts of the code is influenced by the excellent ImageProcessor. Parts of the below code has been modified to fit the screen and might not compile.
With that out of the way, first up is the
AzureBlobMediaCacheRecord. This is a representation of a cached media item and is doing all the heavy lifting. In essence, you need to implement the
Persist. They are responsible for reading and writing the media item to Azure Blob Storage.
Next you need to implement your own
MediaCache. In this case you can actually inherit from Sitecores MediaCache which makes it a bit easier.
Stay with me people, we’re almost there.
Now we only need to create a custom
MediaProvider utilizing our cache. Oh, I also threw in an other bug fix there for you.
Cool. That’s it. Only one thing left now, you know we got to do it, patch it in.
Boom. There you have it.
Hopefully you’ll find this post somewhat inspirational. Please comment on the Gists if you find some improvements or just want to say hi.
Happy caching ❤️