Quick Tip: How to disabled the Umbraco Nucache local file

Warning: This is not recommended for general use, but a helpful tool when Nucache.db cannot be accessed.
WARNING - Before we get started this tip is not recommended practice but it is useful, especially if you are running your website on Azure.

Background

Since Umbraco v8 came out the old XML cache was replaced with NuCache, a new and, for the most part, a much better cache. However, it has had various issues when sites have been run on Azure Web Apps. Some of which have been resolved but, occasionally one will rear it's ugly head. This repeating issue is

System.IO.IOException: The process cannot access the file 'D:\{{path removed}}\NuCache\NuCache.Content.db' because it is being used by another process

There have been various issues raised about this on the Umbraco issue tracker, most of which have been closed as various fixes have been applied over the last couple of years. However, I personally, do not believe that it is fully resolved, but my knowledge on Azure and debugging Azure doesn't allow me to even begin to point the HQ in the right direction.

The Tip

In the thread of this closed Issue on GitHub, there is some great discussion around the issue and one particular comment contains a tiny snippet of code that is incredibly useful.

The snippet in question is this line here:

composition.Register(factory => 
    new PublishedSnapshotServiceOptions { 
        IgnoreLocalDb = true });

This stops the Umbraco instance from using the NuCache db file on disk and instead loads it directly from the Database.

Yes, you guessed it, this is not great for performance and should not be a long term solution, but in a pinch it can be helpful.

What I've done

I've been working on a large site, it's not even live yet but it's had a vast amount of content, media, and members imported (15k, 30k, and 30k respectively) and I've seriously noticed performance problems. I recently upgrade to 8.15 to try and help by switching to the new compression technique for NuCache, however the site took over 2 hours to start and once it did, the NuCache file was permanently locked by another processes.

So, I added a composer to my project that looked like this:

[RuntimeLevel(MinLevel = RuntimeLevel.Boot)]
public class SetupComposer : IUserComposer
{
    public void Compose(Composition composition)
    {
        if(bool.TryParse(
             ConfigurationManager.AppSettings[Constants.DisableLocalCacheFile], 
             out var disableLocalCacheFile) && disableLocalCacheFile)
                composition.Register(factory => 
                       new PublishedSnapshotServiceOptions { 
                              IgnoreLocalDb = true });
    }
}

I also added an app setting so that I can easily toggle this on and off in my deployments.