Okay, I’ll admit, I felt I knew the answer to this – but after my work with the CustomCache object, I felt that this was an interesting experiment.
If I just need to read string, is it faster to read from the configuration file with Sitecore.Configuration.Settings.GetSetting() or using the cache?
Well, the short answer is that the file is roughly twice as fast. Good, no cache needed!
I found that for the below code, on my machine, the ‘reading file’ loop took about 1800ms, but the ‘reading cache’ loop took about 3600ms. There was some variation in the results – of the order of about 15% – but reading from the file was always faster. Good!
My test code:
string exampleValue = Sitecore.Configuration.Settings.GetSetting("example");
string key = "test";
int iterations = 10000000;
CacheManager.SetSettingsCache(key, exampleValue);
Stopwatch sw1 = new Stopwatch();
sw1.Start();
for (int i = 0; i < iterations; i++)
{
string x = Sitecore.Configuration.Settings.GetSetting("example");
}
sw1.Stop();
Stopwatch sw2 = new Stopwatch();
sw2.Start();
for (int j = 0; j < iterations; j++)
{
string y = CacheManager.GetSettingsCache(key);
}
sw2.Stop();