开发者

Keep my in cached memory objects when recompiling in Visual Studio

开发者 https://www.devze.com 2023-02-27 09:53 出处:网络
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.

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....

    开发者_StackOverflow中文版/// <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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消