Sten Sten - 1 year ago 93
C# Question

Best way to cache stored procedure result in C#

I am running a web site in ASP.NET/C#/SQL Server 2012 that needs to cache the result of some stored procedure queries. The result should have an absolute expiration. What options are there to do this?

Preferably setting command.ExpirationDateTime = DateTime.Now.AddMinutes(10) would be great, but as far as I know nothing like that is possible.

The data will be returned from an API, so caching using pages or user controls is not possible.

Answer Source

I ended up creating a hash from the SqlCommand by merging the command text with the parameter names and values. That hash I used as a cache key when putting/getting stuff in/from the HttpContext.Current.Cache object. Works fine. Probably not super fast, but since some queries are somewhat much slower it is all ok.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download