我有一个包含数千个整数的查找表 (LUT),我在大量请求中使用它来根据从数据库中获取的内容来计算内容。
如果我只是创建一个标准单例来保存 LUT,它是在请求之间自动持久化还是我特别需要将其推送到应用程序状态?
如果它们是自动持久化的,那么将它们与应用程序状态一起存储有什么区别?
正确的单例实现会是什么样子?它不需要延迟初始化,但它需要是线程安全的(每个服务器实例有数千个理论用户)并具有良好的性能。
编辑:Jon Skeet 的第 4 版看起来很有希望http://csharpindepth.com/Articles/General/Singleton.aspx
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
// randomguy's specific stuff. Does this look good to you?
private int[] lut = new int[5000];
public int Compute(Product p) {
return lut[p.Goo];
}
}