这种修改过的C#单例模式是一种很好的做法吗?

Ger*_*ois 6 c# asp.net design-patterns

我正在开发一个由非营利组织共享的博客应用程序.我希望每个组织都能够更改自己的博客设置.我采用了单一模式(来自BlogEngine.net)并对其进行了修改.(我知道它不再是单例模式.)我已经测试了这种方法,它似乎在开发环境中工作正常.这种模式是一种很好的做法吗?是否存在可能在生产环境中出现的问题?

public class UserBlogSettings
    {
    private UserBlogSettings()
    {
        Load();
    }

    public static UserBlogSettings Instance
    {
            get
            {
                string cacheKey = "UserBlogSettings-" + HttpContext.Current.Session["userOrgName"].ToString();
                object cacheItem = HttpRuntime.Cache[cacheKey] as UserBlogSettings;
                if (cacheItem == null)
                {
                    cacheItem = new UserBlogSettings();
                    HttpRuntime.Cache.Insert(cacheKey, cacheItem, null, DateTime.Now.AddMinutes(1),
                                             Cache.NoSlidingExpiration);
                }
                return (UserBlogSettings) cacheItem;
            }
    }
}    
Run Code Online (Sandbox Code Playgroud)

(为简洁起见,省略了部分代码.)

感谢您的帮助,评论等.

小智 5

如果是每个会话,则将其存储在会话中而不是存储在缓存中.

此外,你在这里无缘无故地向上转播和向下转播:

object cacheItem = HttpRuntime.Cache[cacheKey] as UserBlogSettings;
Run Code Online (Sandbox Code Playgroud)

这将删除不需要的演员表

UserBlogSettings cacheItem = HttpRuntime.Cache[cacheKey] as UserBlogSettings;
if (cacheItem == null)
{
    cacheItem = new UserBlogSettings();
    HttpRuntime.Cache.Insert(cacheKey, cacheItem, null, 
                         DateTime.Now.AddMinutes(1),
                         Cache.NoSlidingExpiration);
}
return cacheItem;
Run Code Online (Sandbox Code Playgroud)