Caching lookup values in an e-commerce website I am working on an e-commerce website. I have a CarMake dropdown which contains car make names and their corresponding Id, for example: { 1:Alfa romeo}, { 2:Audi }, { 3:BMW }, etc

I want to keep this lookup in cache, so I won't have to make a trip to DB every time I want to get the corresponding Name or Id...

So in my infrastructure layer, I read the data from DB and keep it in cache, I am using a static CarMakeLookup class:

public static class CarMakeLookup
{
    private static readonly Dictionary&lt;short, string&gt; _carMakeIdLookup;
    private static readonly Dictionary&lt;string, short&gt; _carMakeNameLookup;

    static CarMakeLookup()
    {
        _carMakeIdLookup = new Dictionary&lt;short, string&gt;();
        _carMakeNameLookup = new Dictionary&lt;string, short&gt;();

        using (var context = ApplicationDbContext.Create())
        {
            DropdownRepository dropdownRepository = new DropdownRepository(context);

            var carMakes = dropdownRepository.GetCarMakes();
            foreach (var carMake in carMakes)
            {
                _carMakeIdLookup[carMake.CarMakeId] = carMake.CarMakeName;
                _carMakeNameLookup[carMake.CarMakeName.ToLower()] = carMake.CarMakeId;
            }
        }
    }

    public static string GetCarMakeName(short carMakeId)
    {
        if (carMakeId &lt;= 0)
        {
            return string.Empty;
        }

        if (_carMakeIdLookup.ContainsKey(carMakeId))
        {
            return _carMakeIdLookup[carMakeId];
        }

        return string.Empty;
    }

    public static short GetCarMakeId(string carMakeName)
    {
        if (!string.IsNullOrEmpty(carMakeName))
        {
            string lowerCaseCarMakeName = carMakeName.ToLower();
            if (_carMakeNameLookup.ContainsKey(lowerCaseCarMakeName))
            {
                return _carMakeNameLookup[lowerCaseCarMakeName];
            }
        }

        return -1;
    }

    public static Dictionary&lt;short, string&gt; GetCarMakeLookup()
    {
        return _carMakeIdLookup;
    }
}


I also need to display car make in a dropdown in my View... so I need a List&lt;SelectListItem&gt; to be displayed as a dropdown. So in my Application layer I have another static class which holds this list:

public static class CarCache
{
    static CarCache()
    {
        var selectCarMake = new SelectListItem { Value = "-1", Text = "Select a car", Disabled = true, Selected = true };
        CarMakeItemsIncludingSelect = new List&lt;SelectListItem&gt;(); // Dropdown's first option should be "select a car"
        CarMakeItemsIncludingSelect.Insert(0, selectCarMake);

        foreach (KeyValuePair&lt;short, string&gt; carMakeKeyPair in CarMakeLookup.GetCarMakeLookup())
        {
            var selectListItem = new SelectListItem { Value = carMakeKeyPair.Key.ToString(), Text = carMakeKeyPair.Value };
            CarMakeItemsIncludingSelect.Add(selectListItem);
        }
    }

    public static List&lt;SelectListItem&gt; CarMakeItemsIncludingSelect { get; private set; }

    public static string GetCarMakeName(short carMakeId)
    {
        return CarMakeLookup.GetCarMakeName(carMakeId);
    }

    public static short GetCarMakeId(string carMakeName)
    {
        return CarMakeLookup.GetCarMakeId(carMakeName);
    }
}


I have a few more dropdowns like this in my application, for example HouseType, JobType, etc

Everything works fine, the only problem is that my application startup time is slow... I assume one problem is that when I start debugging the application, the application starts building all these static cache classes, and since these classes are static I cannot inject the DbContext into them... so each dropdown should build its own DdContext instance, get corresponding data from DB and Dispose of DbContext. There is no performance problem with the production server... 

Any recommendation on how to improve this cache?
Global static state makes your code untestable...

I would define your car make lookup as:

public class CarMakeLookup
{
    public CarMakeLookup(IEnumerable&lt;(short Id, string Make)&gt; data)
    {
        Makes = data.ToDictionary(d =&gt; d.Id, d =&gt; d.Make);
        Ids = data.ToDictionary(d =&gt; d.Make, d =&gt; d.Id);
    }

    public IReadOnlyDictionary&lt;short, string&gt; Makes { get; }
    public IReadOnlyDictionary&lt;string, short&gt; Ids { get; }
}


And read it with:

public class CarMakeReader : IReader&lt;CarMakeLookup&gt;
{
    public CarMakeReader(Func&lt;ApplicationDbContext&gt; context) =&gt; 
        Context = context;

    Func&lt;DbContext&gt; Context { get; }

    public Task&lt;CarMakeLookup&gt; ReadAsync()
    {
        using (var context = Context())
            return new CarMakeLookup(
                from cm in context.CarMakes
                select (cm.CarMakeId, cm.CarMakeName));            
    }
}


Where IReader&lt;&gt; is simply:

public interface IReader&lt;TSet&gt;
{
    Task&lt;TSet&gt; ReadAsync();
}


There should be also an extension method Cache defined nearby:

public static class Reader
{
    public static IReader&lt;TSet&gt; Cache&lt;TSet&gt;(this IReader&lt;TSet&gt; source) =&gt;
        new CachingReader&lt;TSet&gt;(source);
}


Where:

class CachingReader&lt;TSet&gt; : IReader&lt;TSet&gt;
{
    public CachingReader(IReader&lt;TSet&gt; source) =&gt; 
        Lookup = new Lazy&lt;Task&lt;TSet&gt;&gt;(() =&gt; source.ReadAsync());

    Lazy&lt;Task&lt;TSet&gt;&gt; Lookup { get; }
    public Task&lt;TSet&gt; ReadAsync() =&gt; Lookup.Value;
}


Now register you reader in the IoC container as a decorated singleton:

containerBuilder.RegisterInstance(ctx =&gt; new CarMakeLookup(
    ctx.Resolve&lt;Func&lt;ApplicationDbContext&gt;&gt;())
    .Cache()).AsImplementedInterfaces();