2

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

marcind
  • 52,944
  • 13
  • 125
  • 111
JGilmartin
  • 8,683
  • 14
  • 66
  • 85
  • velocity -> http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=b24c3708-eeff-4055-a867-19b5851e7cd2 – JGilmartin Apr 21 '11 at 15:58

2 Answers2

2

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
marcind
  • 52,944
  • 13
  • 125
  • 111
  • this is useful -> http://stephenwalther.com/blog/archive/2008/08/28/asp-net-mvc-tip-39-use-the-velocity-distributed-cache.aspx – JGilmartin Apr 21 '11 at 15:57
0

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.

Tomas McGuinness
  • 7,651
  • 3
  • 28
  • 40