现在的位置: 首页 > 综合 > 正文

C# lock 用法和 缓存用法

2012年01月29日 ⁄ 综合 ⁄ 共 1150字 ⁄ 字号 评论关闭

namespace LanVersionSwitch.Common
{
  public class LanSwitch
  {

   private static readonly object obj = new object();

   public static string GetValue(string section, string key, string lan)
    {
      string filePath;
      if(HttpContext.Current.Cache[section + "_" + key + "_" + lan] == null)
        {
          lock (obj)
           {
             if (HttpContext.Current.Cache[section + "_" + key + "_" + lan] == null)
               {
                if (lan.ToUpper() == "EN")
                 {
                   filePath =Environment.CurrentDirectory + "/" + System.Configuration.ConfigurationManager.AppSettings["filePathEn"].ToString();
                 }
                 else
                 {
                   filePath = Environment.CurrentDirectory + "/" + System.Configuration.ConfigurationManager.AppSettings["filePathCn"].ToString();
                 }
                   ManagerConfigIni mi = new ManagerConfigIni(filePath);
                   HttpContext.Current.Cache.Add(section + "_" + key + "_" + lan, mi.GetIniKeyValueForStr(section, key), null, DateTime.Now.AddSeconds(5), TimeSpan.Zero, CacheItemPriority.Normal, null);
               }
           }
        }
       return HttpContext.Current.Cache[section + "_" + key + "_" + lan].ToString();
     }
  }
}

抱歉!评论已关闭.