|
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace BizTalkCaching
{
public static class CacheSaravana
{
private static Random _random = new Random(Environment.TickCount);
private static Dictionary<string, string> _authorIds = null;
public static string GetAuthorName(string authorId)
{
if (_authorIds == null)
{
Debug.WriteLine("Cache is empty. So populating the cache...");
_authorIds = new Dictionary<string, string>();
lock (_authorIds)
{
// sandwitching in some delay
_authorIds.Add("2FC0CF1D-E107", "Matthew Johnstone");
Thread.Sleep(_random.Next(0, 1));
_authorIds.Add("71F5C860-80FA", "Carl Reynolds");
Thread.Sleep(_random.Next(0, 1));
_authorIds.Add("158FF294-1A89", "Robert Perkins");
Thread.Sleep(_random.Next(0, 1));
_authorIds.Add("A71AE681-9C39", "Michael Killey");
Thread.Sleep(_random.Next(0, 1));
_authorIds.Add("58794661-A9A3", "Saravana Kumar");
Thread.Sleep(_random.Next(0, 1));
}
}
else
{
Debug.WriteLine("Cache list is pre-populated. Value is going to be taken from the cache.");
}
string authName = _authorIds[authorId];
if (authName != string.Empty)
return authName;
else
throw new Exception("Author cannot be found with id :" + authorId);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.