开发者

Disabling server-side outputcaching on ChildActions in ASP.NET MVC 3 OR vary by client

开发者 https://www.devze.com 2023-03-12 14:36 出处:网络
I am developing an intranet where I have a PartialView refreshing every x seconds (3 for debugging purposes) through jQuery. At the same time, I use the [OutputCaching(Duration = 3)] directive on the

I am developing an intranet where I have a PartialView refreshing every x seconds (3 for debugging purposes) through jQuery. At the same time, I use the [OutputCaching(Duration = 3)] directive on the PartialView action.

The javascript used for refreshing is the following:

<script type="text/javascript">
    $(document).ready(function () {
        setInterval(function () {
            $("#partial_1").load('@Url.Action("_News", "Home", new { id = 1 })');
        }, 3000);
    });
</script>

Where the _News action currently is as follows:

    [OutputCache(Duration=3)]
    public ActionResult _News(int id)
    {
        /* random stuff to get news AND the reason why it should be on per-client-basis
           is that this also contains logic for authentification
        */
        return PartialView();
    }

This all works fine as long as there is only one user.

The problem arises when several users access the page, because all users see the same version.

Completely disabling outputcaching for this action would work, but simply removing the [OutputCaching] directive does not work. If I do, a cached version of the partialview is loaded, every time the jQuery refresh happens. The only way I can change which version of the partialview is being cached is by loading the partialview directly using /_News/Home/ The NoStore attribute is not allowed for ChildActions, so this is not the solution either.

I considered using the VaryByParam attribute, but I'm unsure how how to implement this to vary by us开发者_JAVA百科er, where User.Identity would suffice in this case.


You could try to force the cache location to the client, so that every user (client) has its own cached version.

[OutputCache(Duration=3, Location=OutputCacheLocation.Client)]


You'll want to utilize the VaryByCustom attribute of Output Caching. This will allow you to have a unique cached version for every user (or some other unique cache key that you determine).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号