3

How can i cache my items and values for dropdown list in MVC?

Is there a way to do so?

I am doing that in controller.

Sample code is.......

    public ActionResult Index()
    {
        RegionTasks regionTasks = new RegionTasks();
        ViewBag.Region = GetRegions();}

My controller has function as below.

 [OutputCache(Duration = 10, Location = System.Web.UI.OutputCacheLocation.Server)]
    private IEnumerable<SelectListItem> GetRegions()
    {
        RegionTasks regionTasks = new RegionTasks();
       return regionTasks.GetRegions();
    }

I have tested and it not caches the item for region.

How can i do that?

amit patel
  • 2,287
  • 8
  • 31
  • 45

2 Answers2

8

The OutputCache attribute is used on controller actions to cache the resulting output. It has strictly no effect on other methods.

If you want to cache custom objects you could use the HttpContext.Cache:

private IEnumerable<SelectListItem> GetRegions()
{
    var regionTasks = HttpContext.Cache["regions"] as IEnumerable<SelectListItem>;
    if (regionTasks == null)
    {
        // nothing in the cache => we perform some expensive query to
        // fetch the result
        regionTasks = new RegionTasks().GetRegions();

        // and we cache it so that the next time we don't need to perform
        // the query
        HttpContext.Cache["regions"] = regionTasks;
    }

    return regionTasks;
}

The regionTasks are now cached under the regions key and accessible from anywhere in your ASP.NET application which has access to the HttpContext.Cache.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

Darin is also right, how ever i have done following code to store on server for X minutes.

 private IEnumerable<SelectListItem> GetGlobalUrlRegion(string profileName)
    {
        string cacheKey = "cacheUrlRegion";
        RegionTasks regionTasks = RegionTasks.CreateRegionTasks();
        IEnumerable<SelectListItem> regionUrlList = HttpContext.Cache[cacheKey] as IEnumerable<SelectListItem>;
        if (regionUrlList == null)
        {
            var regionObject = regionTasks.GetRegions(profileName);
            var cTime = DateTime.Now.AddMinutes(int.Parse(System.Configuration.ConfigurationSettings.AppSettings["GlobalCacheDurationInMin"].ToString()));
            var cExp = System.Web.Caching.Cache.NoSlidingExpiration;
            var cPri = System.Web.Caching.CacheItemPriority.Normal;
            regionUrlList = regionObject;
            HttpContext.Cache.Insert(cacheKey, regionObject, null, cTime, cExp, cPri, null);
        }

        return regionUrlList;
    }
amit patel
  • 2,287
  • 8
  • 31
  • 45
  • Cheers, vote up. One problem I had (with a similar implementation) was that my equivalent of `regionUrlList` was being set, but I couldn't seem to read the value on the first run, resulting in an exception. It did skip over the `if` block on the second run however, and the correct value was returned. My approach was to initialise (my equivalent of) `regionObject` as `null` before the `if` block, then use something like this for the return - `return (regionObject != null) ? regionObject : regionUrlList`. That way a value should be returned, even if it is not yet in the cache for some reason. – Aaron Newton Jan 06 '13 at 02:53