在我的 ASP.NET Web 应用程序中,所有对页面的请求都访问这个公共静态缓存类。(所以它必须是线程安全的)
通过调用 Cache 类的 Refresh 方法来刷新此缓存是否安全,如下所示。或者它会导致同步问题?
static class Cache
{
static Dictionary<string, string> cachedCodeNames;
static readonly object sync = new object();
public static string GetName(string code)
{
return CodeNames[code];
}
public static void Refresh()
{
cachedCodeNames = null;
}
static Dictionary<string, string> CodeNames
{
get
{
if (cachedCodeNames == null)
{
lock (sync)
{
if (cachedCodeNames == null)
{
cachedCodeNames = WebServiceManager.GetCodeNameCache();
}
}
}
return cachedCodeNames;
}
}
}
static class WebServiceManager
{
internal static Dictionary<string, string> GetCodeNameCache()
{
throw new NotImplementedException();
}
}