在 iis 网站中缓存全局数据

发布于 2024-10-30 16:42:04 字数 1659 浏览 0 评论 0 原文

我有只读对象图,它被缓存一分钟,以便所有线程都可以使用它(下面的代码)。

public class ObjectGraphCache
{
    static readonly Object storeLock = new object();

    public ObjectGraph AllForElection(int ElectionId, System.Web.Caching.Cache cache)
    {
        string key=string.Format("AllForElection{0}",
            ElectionId);
        int timout = int.Parse(ConfigurationManager.AppSettings["dbCacheTimeInSeconds"]);

        if (timout == 0)
        {
            ObjectGraph graph = new ObjectGraph();
            graph.AllForElection(ElectionId);
            return graph;
        }
        else
        {
            Object obj = cache[key];
            if (obj == null)
            {
                lock (storeLock)
                {
                    obj = cache[key]; // In case another page got the lock first and your are queued (Ensures only one get per cycle)
                    if (obj == null)
                    {
                        // Not in cache
                        ObjectGraph graph = new ObjectGraph();
                        graph.AllForElection(ElectionId);

                        cache.Insert(
                            key,
                            graph,
                            null,
                            DateTime.Now.Add(new TimeSpan(0, 0, timout)),
                            System.Web.Caching.Cache.NoSlidingExpiration);
                        return graph;
                    }
                }
            }
            return (ObjectGraph)obj;
        }

但是我想知道:当我更轻松地将 READONLY 对象存储为静态时,为什么要使用 Cache 对象。这存储了一个指向堆的指针,因此当它被更新时,仍在处理前一个指针对象的线程将对旧对象进行罚款,而且我也不必在返回对象之前从缓存中转换该对象。有人看到任何陷阱吗?

I have READONLY object graph that is cached for one minute so that it can be used by all threads (Code below).

public class ObjectGraphCache
{
    static readonly Object storeLock = new object();

    public ObjectGraph AllForElection(int ElectionId, System.Web.Caching.Cache cache)
    {
        string key=string.Format("AllForElection{0}",
            ElectionId);
        int timout = int.Parse(ConfigurationManager.AppSettings["dbCacheTimeInSeconds"]);

        if (timout == 0)
        {
            ObjectGraph graph = new ObjectGraph();
            graph.AllForElection(ElectionId);
            return graph;
        }
        else
        {
            Object obj = cache[key];
            if (obj == null)
            {
                lock (storeLock)
                {
                    obj = cache[key]; // In case another page got the lock first and your are queued (Ensures only one get per cycle)
                    if (obj == null)
                    {
                        // Not in cache
                        ObjectGraph graph = new ObjectGraph();
                        graph.AllForElection(ElectionId);

                        cache.Insert(
                            key,
                            graph,
                            null,
                            DateTime.Now.Add(new TimeSpan(0, 0, timout)),
                            System.Web.Caching.Cache.NoSlidingExpiration);
                        return graph;
                    }
                }
            }
            return (ObjectGraph)obj;
        }

However I want to know : Why use the Cache object, when I more easily store the READONLY object as a static. This stores a pointer to the heap, so when it is updated, threads still processing the previous pointers object will carry one fine with the old object, AND also I would not have to cast the object from the cache before returning it. Anyone see any gotchas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花海 2024-11-06 16:42:04

storeLock 放入应用程序变量中

put storeLockin an application variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文