如何将对象添加到高速缓存和缓存MVC4访问数据? [英] How to add an object to a Cache and Access the data from cache in MVC4?

查看:98
本文介绍了如何将对象添加到高速缓存和缓存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"
                            }
                        ]
                    }
                ]
            }
        ]

现在我想是我想要保留在缓存这个数据,我这个数据绑定到我的看法,现在我想的是当过我让从我的要求,现在的我应该能够从访问数据上述目的whihc在高速缓存中......惨啊纽约一个可以帮助我,我怎么能做到这一点我如何该对象从那里添加到高速缓存和访问数据

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

推荐答案

您可以把这个应用程序缓存,这样就可以从任何地方访问它。

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"

有是缓存的OutputCache的另一种类型的可在控制器操作如下被应用。为300秒的持续时间,这将提供缓存输出到客户端。

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);
 }

看起来你需要上述2之一还有其他类型的缓存也喜欢甜甜圈缓存和甜甜圈洞的缓存,如果你只想要查看的某些部分会被缓存,均适用。在甜甜圈洞缓存可以包括上述缓存动作转换成另一种观点作为行动。

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.

这篇关于如何将对象添加到高速缓存和缓存MVC4访问数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆