2013-06-13 2 views
18

Ho un dashboard specifico dell'utente. La dashboard cambierà solo giornalmente, voglio usare MVC'sOutputCache. C'è un modo per configurare la memorizzazione nella cache per utente e scadere quando la richiesta è un nuovo giorno?Configurare la cache di output per utente mvc

Ho cercato questo e ho scoperto che è possibile estendere l'attributo OutputCache per impostare dinamicamente la durata, ma come posso configurarlo per utente?

Grazie in anticipo

risposta

28

Nel vostro Web.config:

<caching> 
    <outputCacheSettings> 
    <outputCacheProfiles> 
     <add name="Dashboard" duration="86400" varyByParam="*" varyByCustom="User" location="Server" /> 
    </outputCacheProfiles> 
    </outputCacheSettings> 
</caching> 

Nel vostro Controller/Action:

[OutputCache(CacheProfile="Dashboard")] 
public class DashboardController { ...} 

Poi, nel tuo Global.asax:

//string arg filled with the value of "varyByCustom" in your web.config 
public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
    if (arg == "User") 
     { 
     // depends on your authentication mechanism 
     return "User=" + context.User.Identity.Name; 
     //return "User=" + context.Session.SessionID; 
     } 

    return base.GetVaryByCustomString(context, arg); 
} 

In sostanza, GetVaryByCustomString vi permette di scrivere un metodo personalizzato per determinare se ci sarà una cache hit/miss.

+0

questo stesso concetto funziona con DevTrends DonutCaching? – ganders

+0

Non l'ho provato ma dato che forniscono anche un meccanismo 'VaryByCustom'. Presumo che la risposta sia sì. – haim770

2

provare questo nel web.config,

<system.web> 
........... 
........... 
<caching> 
    <outputCacheSettings> 
    <outputCacheProfiles> 
     <add name="UserCache" duration="1440" varyByParam="UserID" enabled="true" location="Client"/> 
    </outputCacheProfiles> 
    </outputCacheSettings> 
</caching>