2011-04-08 22 views
5

मैं अपने आवेदन में कैशिंग का उपयोग करना चाहता हूं लेकिन जो डेटा मैं लौटा रहा हूं वह लॉग इन उपयोगकर्ता के लिए विशिष्ट है। जब मुझे उपयोगकर्ता द्वारा अलग-अलग करने की आवश्यकता होती है तो मैं बॉक्स कैशिंग नियमों में से किसी एक का उपयोग नहीं कर सकता।एमवीसी 3 कस्टम आउटपुट कैश

क्या कोई कस्टम कैशिंग विशेषता बनाने पर मुझे सही दिशा में इंगित कर सकता है। नियंत्रक से मैं उपयोगकर्ता को Thread.CurrentPrincipal.Identity; या एक निजी नियंत्रक सदस्य से एक्सेस कर सकता हूं जिसे मैं नियंत्रक कन्स्ट्रक्टर _user

में प्रारंभ करता हूं।

उत्तर

9

आप VaryByCustom इस्तेमाल कर सकते हैं में विधि ओवरराइड करना चाहिए। Global.asax में GetVaryByCustomString विधि ओवरराइड:

public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
    if (arg == "IsLoggedIn") 
    { 
     if (context.Request.Cookies["anon"] != null) 
     { 
       if (context.Request.Cookies["anon"].Value == "false") 
       { 
        return "auth"; 
       } 
       else 
       { 
        return "anon"; 
       } 
      } 
      else 
      { 
      return "anon"; 
      } 
    } 
    else 
    { 
     return base.GetVaryByCustomString(context, arg); 
    } 
} 

और फिर OutputCache विशेषता का उपयोग:

[OutputCache(CacheProfile = "MyProfile")] 
public ActionResult Index() 
{ 
    return View(); 
} 

और web.config में:

<caching> 
    <outputcachesettings>    
     <outputcacheprofiles> 
      <clear /> 
      <add varybycustom="IsLoggedIn" varybyparam="*" duration="86400" name="MyProfile" /> 
     </outputcacheprofiles> 
    </outputcachesettings> 
</caching> 
+8

[लेखक] (http://visitmix.com/writings/using-varybycustom-with-outputcache-in-asp-net देने के लिए अच्छा होगा -एमवीसी-टू-सपोर्ट-कैशिंग-इन-लॉग-इन-यूजर्स) क्रेडिट;) ​​ – balexandre

+0

http://visitmix.com/writings/using-varybycustom-with-outputcache-in-asp-net-mvc-to-support -caching के लिए लॉग-इन-उन – ulty4life

0

कस्टम भिन्न स्ट्रिंग निर्दिष्ट करने के लिए आपको OutputCache.VaryByCustom Property का उपयोग करना चाहिए। और यह उपयोग करने के लिए, आप अपने Global.asax

public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
    if(arg.ToLower() == "currentuser") 
    { 
    //return UserName; 
    } 
    return base.GetVaryByCustomString(context, arg); 
} 
1

अधिकृत गुण कुछ दिलचस्प बातें है अधिकृत बनाम अनधिकृत उपयोगकर्ताओं के लिए कैशिंग के संबंध में जा रहा है। आप इसके तर्क को निकालने में सक्षम हो सकते हैं और प्रति उपयोगकर्ता के बजाय प्रति अधिकृत उपयोगकर्ता को कैश करने के लिए संशोधित कर सकते हैं, "उपयोगकर्ता अधिकृत है"। इस पोस्ट के बाहर

की जांच:

Can someone explain this block of ASP.NET MVC code to me, please?

संबंधित मुद्दे