2013-06-13 11 views
18

Mam pulpit użytkownika. Deska rozdzielcza zmieni się tylko codziennie, chcę użyć MVC'sOutputCache. Czy istnieje sposób konfigurowania buforowania na użytkownika i wygasania, gdy żądanie jest nowe?Skonfiguruj wyjściową pamięć podręczną dla każdego użytkownika mvc

Sprawdziłem to i stwierdziłem, że można rozszerzyć atrybut OutputCache, aby dynamicznie ustawić czas trwania, jak jednak skonfigurować to dla każdego użytkownika?

góry dzięki

Odpowiedz

28

w twojej Web.config:

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

w twojej Controller/Action:

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

Następnie w 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); 
} 

W istocie, GetVaryByCustomString pozwala napisać niestandardową metodę w celu ustalenia, czy nie będzie Cache hit/miss.

+0

Czy ta sama koncepcja działa z DevTrends DonutCaching? – ganders

+0

Nie próbowałem, ale ponieważ zapewniają mechanizm 'VaryByCustom'. Zakładam, że odpowiedź brzmi "tak". – haim770

2

spróbować tego w pliku web.config,

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