SoftwareNerd
SoftwareNerd

Reputation: 1895

How to add an object to a Cache and Access the data from cache in MVC4?

Hi all i have an object which returns data like this

         var productdetial=  {
                               "productId": "1",
                               "productname": "Casualshirts",
                               "productSkus": [
                                                   {
                                                     "Skuimage": "URL",
                                                     "SKU": [
                                                               {
                                                                  "ProducSKU": "Shoe1001",
                                                                  "Options": [
                                                                                {
                                                                                  "productOptions": [
                            {
                                "OptionID": "1",
                                "optionname": "Color",
                                "value": "Black"
                            },
                            {
                                "OptionID": "2",
                                "optionname": "Size",
                                "value": "S"
                            },
                            {
                                "OptionID": "3",
                                "optionname": "Fit",
                                "value": "Regular"
                            }
                        ]
                    }
                ]
            },
            {
                "ProducSKU": "Shoe1002",
                "Options": [
                    {
                        "productOptions": [
                            {
                                "OptionID": "1",
                                "optionname": "Color",
                                "value": "Red"
                            },
                            {
                                "OptionID": "2",
                                "optionname": "Size",
                                "value": "S"
                            },
                            {
                                "OptionID": "3",
                                "optionname": "Fit",
                                "value": "Regular"
                            }
                        ]
                    }
                ]
            },
            {
                "ProducSKU": "Shoe1003",
                "Options": [
                    {
                        "productOptions": [
                            {
                                "OptionID": "1",
                                "optionname": "Color",
                                "value": "Orange"
                            },
                            {
                                "OptionID": "2",
                                "optionname": "Size",
                                "value": "S"
                            },
                            {
                                "OptionID": "3",
                                "optionname": "Fit",
                                "value": "Regular"
                            }
                        ]
                    }
                ]
            }
        ]

now what i want is i want to keep this data in cache, and i am binding this data to my view ,now what i want is when ever i make a request from my now on i should be able to access data from the above object whihc is in cache...cana ny one help me how can i do this how do i add this object to a cache and access data from there

Upvotes: 2

Views: 5727

Answers (2)

testCoder
testCoder

Reputation: 7385

You can use httpcontext cache:

if(HttpContext.Cache.Get("productdetial") == null)
{
  HttpContext.Cache.Add("productdetial", productdetial, null, Cache.NoAbsoluteExpiration,
                                          new TimeSpan(0, 1, 0), CacheItemPriority.Normal, null);
}else
{
  cached = (ProductDetails) HttpContext.Cache.Get("productdetial");
}

Upvotes: 4

Abhijit-K
Abhijit-K

Reputation: 3679

You can put this in the Application cache so that you can access it from anywhere.

HttpContext.Current.Application["productdetial"] = productdetial;

Then you can access it from anywhere like this

ProductDetails object = (ProductDetails) HttpContext.Current.Application["productdetial"]

if you want to store and retrieve this a string than also this is possible. Note that if you need caching specific to user session then use "Session" table instead of "Application"

There is another type of cache OutputCache that can be applied on controller action as shown below. For duration of 300 seconds this will provide the cached output to the client.

[OutputCache(Duration=300, VaryByParam="none")]
 public ActionResult GetProductDetails()
 {
      //Fetch the data once and it will be cached for 300 seconds       
            return View(data);
 }

Looks like you need one of the above 2. There are other types of caching also like donut caching and donut hole caching which are applicable if you want only some portion of the view to be cached. In donut hole caching you can include the above cached action into another view as a action.

Upvotes: 4

Related Questions