在 Visual Studio 中重新编译时将我的对象保留在缓存内存中

发布于 2024-11-02 20:58:48 字数 1523 浏览 6 评论 0原文

我有一个 MVC 3 站点,它使用内存中缓存的对象。

当网站第一次受到攻击时,需要大约一分钟的时间来构建缓存,一旦构建后,每个人的速度都会非常快。

当我开发时,我必须减少缓存对象的数量,因为每次我重新完成我的项目时,它都会删除缓存并必须重建它。

有没有一种方法可以设置 Visual Studio,以便在我重新完成时保留内存缓存?

这是我用于缓存的一些代码....

    /// <summary>
    /// Method to get all the prices
    /// </summary>
    public static List<DB2011_PriceRange> AllPrices
    {
        get
        {
            lock(_PriceLock)
            {
                if (HttpRuntime.Cache["Prices"] != null)
                {
                    return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
                }
                else
                {
                    PopulatePrices();
                    return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
                }
            }
        }
    }

    /// <summary>
    /// Poplate the Cache containing the prices
    /// </summary>
    private static void PopulatePrices()
    {
        // clear the cache and the list object
        ClearCacheAndList("Trims", ref ListAllPrices);

        using(var DB = DataContext.Get_DataContext)
        {
            ListAllPrices = (from p in DB.DB2011_PriceRange
                          select p).ToList();
        }

        // add the list object to the Cache
        HttpRuntime.Cache.Add("Prices", ListAllPrices, null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
    }

任何帮助总是appricaied

Truegilly

I have an MVC 3 site that uses cached in memory objects.

when the site first gets hit, it takes around a minute to build the cache, once built its very fast for everyone then on.

when im developing, ive had to reduce the number of cached objects as everytime i recomple my project it drops the cache and has to rebuild it.

Is there a way i can set Visual studio so it keeps the in memory cache when i recomple ?

here is some of my code i use for caching....

    /// <summary>
    /// Method to get all the prices
    /// </summary>
    public static List<DB2011_PriceRange> AllPrices
    {
        get
        {
            lock(_PriceLock)
            {
                if (HttpRuntime.Cache["Prices"] != null)
                {
                    return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
                }
                else
                {
                    PopulatePrices();
                    return (List<DB2011_PriceRange>)HttpRuntime.Cache["Prices"];
                }
            }
        }
    }

    /// <summary>
    /// Poplate the Cache containing the prices
    /// </summary>
    private static void PopulatePrices()
    {
        // clear the cache and the list object
        ClearCacheAndList("Trims", ref ListAllPrices);

        using(var DB = DataContext.Get_DataContext)
        {
            ListAllPrices = (from p in DB.DB2011_PriceRange
                          select p).ToList();
        }

        // add the list object to the Cache
        HttpRuntime.Cache.Add("Prices", ListAllPrices, null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
    }

any help is always appricaiated

Truegilly

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

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

发布评论

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

评论(2

樱花细雨 2024-11-09 20:58:48

重新编译您的应用程序会导致托管该应用程序的 AppDomain 重新启动,这会释放您的缓存。您可以:

  1. 尝试将缓存保存到磁盘并在应用程序启动时从那里读取它。它可能会更快。
  2. 使用进程外缓存,例如 Velocity

Recompiling your application causes the AppDomain that is hosting it to be restarted which is what is disposing of your cache. You could:

  1. Try to save your cache to disk and read it from there when the app starts. It might be faster.
  2. Use an out-of-process cache such as Velocity
救赎№ 2024-11-09 20:58:48

我不相信是这样。当您发布新的 DLL 时,将创建运行应用程序的新进程。由于您使用的是内存缓存,因此所有对象都将被删除。

您可以使用一种特殊的方法“预热缓存”,该方法会在您发布新代码时预先填充所有缓存。

I don't believe so. When you publish new DLLs the a new process that runs the application is created. Since you're using an in-memory cache, all objects would be removed.

You could "warm the caches" with a special method that would pre-populate them all when you publish new code.

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