I have a problem. Cannot find how to make OutputCache SQLDependency dependent on one row from database table. E.g. I have a Controller with one parameter.
ActionResult Index(int? id)
And for each request with same id I need to check database table table1(id int, last_updated datetime). If row with id = id and last_updated hasn't changed.
I use sql server 2005 or higher.
Which strategy should I use ?
I tried to use:
[OutputCache(Duration = int.MaxValue, VaryByParam = "id",
SqlDependency = "DatabaseName:table1")]
but it works for whole table changes.
Using SQL cache dependency, you could cache your product information and create a dependency on a database table or row change. When the data changes—and only then—the cache items based on that data are invalidated and removed from the cache.
The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.
To avoid hitting the database for each webpage request (generally an expensive operation), Object Caching (introduced in .NET 4.0) can be used. This would result in fast serving of webpages because everything would be handled directly from memory. Database operations would occur only if the data actually changed, or if the cache was evicted from memory due to resource constraints or your CacheItemPolicy settings.
The practical strategy used in this case would be as follows.
Step-1. In your Model method where data for the "id" row is being modified/added/deleted, complete that database operation and then:
Step-2. Retrieve the object from cache whenever possible, refreshing from the db only if necessary:
(The MemoryCache class is the concrete implementation of the ObjectCache class).
With this approach, the OutputCache on the Controller method would not need to be used at all, and the data caching decisions would all be fully encapsulated within the Model. We would get a cleaner separation of concerns; much higher efficiency, better response times, and improved scalability; and a reduced dependency on costly database operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With