0

I want to add visit count per person request action. My action is :

public async Task<ActionResult> Index()
{
    // add visit count
    VisitCount();
    return View(await db.Departments.ToListAsync());
}

When I use OutputCache to cache it, VisitCount() doesn't run! How can I do OutputCache with VisitCount ?

use it .but not run again (only run every output cache Duration)

[VisitCoutFilter]
[OutputCache(Duration = 30,Order = 100)]
public async Task<ActionResult> Index()
{
    return View(await db.Departments.ToListAsync());
}

and visit cont :

public class VisitCoutFilter: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //do visit 
        // break ponit 
        base.OnActionExecuting(filterContext);
    }
}
teo van kot
  • 12,350
  • 10
  • 38
  • 70
shahroz
  • 51
  • 1
  • 8

2 Answers2

2

The problem is that OutputCache will not run the action method because it caches the whole view. It'll retrieve the view from the cache. So, you have to ask yourself what exactly you're trying to achieve. Performance? How? By minimizing round trips to the database?

Minimizing database round trips

  1. You can cache the data. If you're using Entity Framework with EnableObjectTracking on, then by default the data will be cached

  2. You can use a partial view to render the data and cache the partial view ONLY. This will leverage data caching

Minizing requests to the server

1.You can cache the view on the client if you want to eliminate round trips to the web server. However, you will need to use javascript to handle the analitics part of it

 [OutputCache(Duration=100, VaryByParam="none", Location=OutputCacheLocation.Client, NoStore=true)]
    public ActionResult Index(){}
Community
  • 1
  • 1
Leo
  • 14,625
  • 2
  • 37
  • 55
1

Ok i get your problem and solution is here:

The OutputCacheAttribute has limitations by the way and there is a custom attribute named DonutOutputCache developed by Paul Hiles helps to overcome the limitations.

Community
  • 1
  • 1
teo van kot
  • 12,350
  • 10
  • 38
  • 70