同じ秒で複数の Web 要求を介して呼び出すことができる次のコードがあります。そのため、2 番目以降のリクエストがデータベースにヒットするのは望ましくありませんが、最初のリクエストがヒットするまで待ちます。
Lazy<T> 代わりにキーワードクラスを使用するようにこれをリファクタリングする必要がありますか? 1 つのコードに対して 10 回の呼び出しがLazy<T>同時に発生した場合、それらの呼び出しのうち 9 回は最初の呼び出しが完了するまで待機しますか?
public class ThemeService : IThemeService
{
private static readonly object SyncLock = new object();
private static IList<Theme> _themes;
private readonly IRepository<Theme> _themeRepository;
<snip snip snip>
#region Implementation of IThemeService
public IList<Theme> Find()
{
if (_themes == null)
{
lock (SyncLock)
{
if (_themes == null)
{
// Load all the themes from the Db.
_themes = _themeRepository.Find().ToList();
}
}
}
return _themes;
}
<sip snip snip>
#endregion
}