Настройка кэша вывода для каждого пользователя mvc

У меня есть пользовательская панель инструментов. Панель инструментов будет меняться ежедневно, я хочу использовать MVC's OutputCache. Есть ли способ настроить кеширование для каждого пользователя и истечь, когда запрос является новым днем?

Я исследовал это и обнаружил, что вы можете расширить атрибут OutputCache, чтобы динамически установить свою продолжительность, но как я могу настроить это для каждого пользователя?

Заранее спасибо

Ответы

Ответ 1

В Web.config:

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

В Controller/Action:

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

Затем в 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);
}

По существу, GetVaryByCustomString позволяет вам написать собственный метод, чтобы определить, будет ли кэш hit/miss.

Ответ 2

Попробуйте это в web.config,

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