Skip to content

Registering FileStore Cache

Jon P Smith edited this page Aug 23, 2022 · 9 revisions

The FileStore cache library contains an extension method called AddDistributedFileStoreCache which will register the FileStore cache version you want to use as a service. It also sets up / find the FileStore cache file name and location using your environment information, which is simpler (and cheaper) compared to using a distributed cache that uses a database.

Registering FileStore Cache

Registering FileStore Cache in ASP.NET Core

The code below shows how to register the FileStore cache String version in an ASP.NET Core’s Program startup.

builder.Services.AddDistributedFileStoreCache (options =>
    {
        options.WhichVersion = FileStoreCacheVersions.String;
    }, builder.Environment);

The register above will:

  • Register the String version to the interface IDistributedFileStoreCacheString
  • It will create a json cache name by using your IHostEnvironment.EnvironmentName for the second part of the filename, e.g. it would be "FileStoreCacheFile.Development.json" if your IHostEnvironment.EnvironmentName was "Development"
  • It will set the options's PathToCacheFileDirectory property to the value in the IHostEnvironment.ContentRootPath.
  • It will look for an existing json cache file that matches the name / directory. If file isn't found it will create the file (within a file lock) with an empty cache. NOTE: The FileStore will throw an exception if a json cache file is missing.

Registering FileStore Cache with applications without a IHostEnvironment

If you want to register the FileStore cache in a non-ASP.NET Core application (superficially, a application that does not have a IHostEnvironment) then you need to to set the SecondPartOfCacheFileName to a suitable environment and the PathToCacheFileDirectory to the file directory where all instances can access. The code below shows the registering on a non-ASP.NET Core application which produces the same result as the ASP.NET Core application registration above

Services.AddDistributedFileStoreCache (options =>
    {
        options.WhichVersion = FileStoreCacheVersions.String;
        options.SecondPartOfCacheFileName = "Development"
        options.PathToCacheFileDirectory = AppDomain.CurrentDomain.BaseDirectory
    });

Registering FileStore Cache where the applications are run on different servers

If you run your applications on different servers, say when using Microservices or a App farm, then you would need to set the PathToCacheFileDirectory option to a directory that all the applications can access.

Services.AddDistributedFileStoreCache (options =>
    {
        options.WhichVersion = FileStoreCacheVersions.String;
        options.PathToCacheFileDirectory = //Some directory that all the apps can access
    }, builder.Environment);

What happens to the FileStore cache content on startup

By default, the FileStore cache won't be changed when the application starts / restarts. This is because I want the content of the FileStore cache to survive an application start / restart. BUT read this section of the Deployment and testing "gocha's" documentation about making sure you don't replace an existing Production json FileStore file when you deploy your application.

But if you want to wipe the FileStore cache on startup you can use the cache's ClearAll method. This is a sync method so you can cal this in the Program class after the app variable has been created - see code below.

//... other builder code left out
var app = builder.Build();
app.Services.GetRequiredService<chosen cache interface>().ClearAll();
//... other app code left out

Option's WhichVersion: default String

This defined what type of FileStore cache service you want to register, as shown in the code at the start of the page. There are four versions and each register an interface.

NOTE: See the Home page for a description of each version.

Version name Value type Registered interface
String string IDistributedFileStoreCacheString
Class string + class IDistributedFileStoreCacheClass
Bytes byte[] IDistributedFileStoreCacheBytes
IDistributedCache byte[] IDistributedCache

Possible extra options

The DistributedFileStoreCacheOptions has many options, most of which you don't need to change but might be handy in certain situations. All the DistributedFileStoreCacheOptions properties have comments so I only detail the options that you might need. But do have a look at Tips on making your cache fast document which tells you about how to set the maximum size of the cache.