【发布时间】:2021-10-04 10:20:29
【问题描述】:
我正在编写文件缓存之类的东西,我正在争论使用锁还是 ConcurrentDictionary。如果多个线程要求一个键,那么如果两个线程尝试写入普通字典就会出现问题,所以我尝试了 ConcurrentDictionary。现在有一个次要问题,当每个线程试图获取文件时,如何防止文件被读取两次(或更多次)。我添加了示例代码来解释我的意思。
这是一个使用锁和字典的版本
class Program
{
private static object locking = new object();
private static Dictionary<string, byte[]> cache;
static void Main(string[] args)
{
cache = new Dictionary<string, byte[]>();
Task.Run(() =>
{
AddToCache("largefile", "largefile.bin");
});
Task.Run(() =>
{
AddToCache("largefile", "largefile.bin");
});
}
static byte[] AddToCache(string key, string filename)
{
lock(locking)
{
if (cache.TryGetValue(key, out byte[] data))
{
Console.WriteLine("Found in cache");
return data;
}
Console.WriteLine("Reading file into cache");
data = File.ReadAllBytes(filename);
cache[key] = data;
return data;
}
}
}
这个版本符合预期,它可以保护字典免受多线程的影响,并且只读取一次大文件。
这是使用 ConcurrentDictionary 的第二个版本:
class Program
{
private static ConcurrentDictionary<string, byte[]> cache;
static void Main(string[] args)
{
cache = new ConcurrentDictionary<string, byte[]>();
Task.Run(() =>
{
AddToCache("largefile", "largefile.bin");
});
Task.Run(() =>
{
AddToCache("largefile", "largefile.bin");
});
}
static byte[] AddToCache(string key, string filename)
{
return cache.GetOrAdd(key, (s) =>
{
Console.WriteLine("Reading file into cache");
return File.ReadAllBytes(filename);
});
}
}
此版本保护字典,但它两次读取大文件,这不是必需的。我认为我在这里做错了,但不熟悉 GetOrAdd 我不确定是什么。
第一个版本看起来不错,但它是真实代码的缩减版本,并且锁会锁定很多代码。第二个版本看起来更简单,但不会阻止多次读取文件。有没有办法做到这一点,而锁不会阻塞大量代码,或者这是唯一的答案?
【问题讨论】:
标签: .net locking concurrentdictionary