开发者

.NET reference to cached item (DataTable)

I'm seeing what seems like strange behaviour when dealing with a DataTable I'm storing in the cache. Although it may be by design.

The scenario is simple. I'm storing the results of a DB query in a DataTable and dropping that into that cache. After doing the DB query or grabbing the results from the cache I need to do some post manipulation on it (such as adding a column or ordering). It seems like the manipulation is happening to the item in the cache. So when I grab the item from the cache it must be grabbing a reference to it?

The code

string CacheKey = "Search开发者_高级运维Data";
DataTable CachedResults = null;
object CachedObject = Cache[CacheKey];
if (CachedObject is DataTable) { CachedResults = (DataTable)CachedObject; }
if (CachedResults == null)
{
  CachedResults = new DataTable();
  // Query the database and fill CachedResults

  if (Cache[CacheKey] == null)
  {
    Cache.Add(CacheKey, CachedResults, null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration, CacheItemPriority.Low, null);
  }

}

CachedResults.Columns.Add("NewColumn");

I guess my question is, is CachedResults simply a reference to the cached item? And if I want to avoid that I'd need to do a copy on the DataTable?

Thanks so much, Maleks


This actually depends on what the underlying cache provider is. If it is the in-memory provider, then yes: you are manipulating a single reference to an object. This could be very bad, since DataTable is not thread-safe - plus you will confuse other code using it.

If, however, you are using something like sql-server as a backend for cache, it will serialize and deserialize.

Personally, my advice is therefore: only cache immutable data, or, if you must cache mutable data, don't mutate it when getting it from cache.

In your specific case, yes: copying the DataTable after fetch would avoid the issue.


Yes, it is a reference to the cached item. The way I've always managed it is to copy as soon as I retrieve it from the cache so the changes aren't automatically reflected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜