加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:14:02 所属栏目:百科 来源:网络整理
导读:我正在开发一个由非营利组织共享的博客应用程序.我希望每个组织都能够更改自己的博客设置.我采用了单一模式(来自BlogEngine.net)并对其进行了修改. (我知道它不再是单例模式.)我已经测试了这种方法,它似乎在开发环境中工作正常.这种模式是一种很好的做法吗?
我正在开发一个由非营利组织共享的博客应用程序.我希望每个组织都能够更改自己的博客设置.我采用了单一模式(来自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;
            }
    }
}

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

感谢您的帮助,评论等.

解决方法

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

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

object cacheItem = HttpRuntime.Cache[cacheKey] as UserBlogSettings;

这将删除不需要的演员表

UserBlogSettings cacheItem = HttpRuntime.Cache[cacheKey] as UserBlogSettings;
if (cacheItem == null)
{
    cacheItem = new UserBlogSettings();
    HttpRuntime.Cache.Insert(cacheKey,Cache.NoSlidingExpiration);
}
return cacheItem;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读