C#缓存公用方法dir

public class DirCache
{
private SortedDictionary<string, object> dic = new SortedDictionary<string, object>();
private static volatile DirCache instance = null;
private static object lockHelper = new object();
private DirCache()
{

}
/// <summary>
/// 新增缓存
/// </summary>
/// <param name="key">关键字</param>
/// <param name="value">缓存数据</param>
public void Add(string key, object value)
{
dic.Add(key, value);
}
/// <summary>
/// 移除缓存
/// </summary>
/// <param name="key">关键字</param>
public void Remove(string key)
{
dic.Remove(key);
}

public object this[string index]
{
get
{
if (dic.ContainsKey(index))
return dic[index];
else
return null;
}
set { dic[index] = value; }
}

public static DirCache Instance
{
get
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance = new DirCache();
}
}
}
return instance;
}
}
}

posted @ 2021-10-21 13:41  龙丶谈笑风声  阅读(61)  评论(0编辑  收藏  举报