简体   繁体   中英

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.

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

Have a look at the Enterprise Library Caching Application Block. This has the exact functionality you are looking for

The Caching Application Block

    cache.Add(listid.ToString(), list, CacheItemPriority.Normal, null, 
new SlidingTime(TimeSpan.FromMinutes(60)));

I don't understand you restriction on where you can actually perform caching, but I assume you'll have access to HttpRuntime.Cache ? If that's the case, I have written a series of utilities for caching service responses in a blog post ( Caching Services - The Lazy Way ).

The basics of this utility is you can do:

   string cacheKey = GenerateCacheKey(myParam); //most likely a derivative of myParam

   if (Cache.IsInCache<MyResultType>(cacheKey))
   {
      return Cache.GetFromCache<MyResultType>(cacheKey);
   }

   var result = GetMyRequestedResult(myParam);
   if (result != null) //or whatever makes sense
   {
      Cache.InsertIntoCacheAbsoluteExpiration(cacheKey, result, DateTime.Now.AddMinutes(0));
   }

   return result;

If you have any services in between, the post shows a cute class for interacting/caching with those services.

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.

You can also use System.Runtime.Caching.ObjectCache starting from .Net Framework 4 not only in web applications. Here is an example:

List<EmailData> result = null;

ObjectCache cache = MemoryCache.Default;
var key = string.Concat(title, ":", language);
var item = cache.GetCacheItem(key);

if (item != null)
  return item.Value as List<EmailData>;

using (var connection = _connectionFactory.OpenConnection())
{
  result = connection.Query<EmailData>(sql, new { title, language }).ToList();
}

var cachingPolicy = new CacheItemPolicy
{
  AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(_cacheExpirationIntervalInMinutes)
};

cache.Set(new CacheItem(key, result), cachingPolicy);

return result;

You can read more: https://msdn.microsoft.com/en-us/library/system.runtime.caching.objectcache(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM