Question

I know I can create a custom outputcache attribute for ASP.NET MVC controller action. I would like to change the duration of the output cache depending on if an event has passed or not. If it has passed the current date then the duration would be alot longer. However I need to check a database to do this, however I am making the same call within the controller action itself. Is there anyway to set the duration at the end of the action result so it's dependent on the date of the event that was queried inside the controller action? I dont want to make two calls, one in the output cache attribute and controller action.

Was it helpful?

Solution

You should utilize the SqlCacheDependency class for that, for example:

VIEW:

<%@ OutputCache Duration="3600" SqlDependency="Northwind:Employees" VaryByParam="none" %>

WEB.CONFIG:

 <caching>
      <sqlCacheDependency enabled = "true" pollTime = "60000" >
        <databases>
          <add name="northwind" 
            connectionStringName="Northwind"
            pollTime="9000000"
            />
        </databases>
      </sqlCacheDependency>
    </caching>

The above example shows an ASP.NET Web.config file that enables table-based dependencies on a SQL Server database table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top