Clear cache in SqlDataSource
Asked Answered
S

4

12

I need to manually clear the cache on a SqlDataSource with caching enabled. I've tried setting EnableChaching = false, and CacheDuration = 0 (as well as = 1) and none seem to expire the content already in the cache - although they do seem to prevent new SELECTs from being cached.

How do I manually expire that cache?

Thanks.

Sociology answered 1/6, 2009 at 20:13 Comment(0)
J
14

I just started researching this today and came across this post, this look likes the best solution:

Simple way to invalidate SqlDataSource cache programmatically

<asp:SqlDataSource ID="x" EnableCaching="True" CacheKeyDependency="MyCacheDependency"/>

protected void Page_Load(object sender, EventArgs e)
{ // Or somewhere else before the DataBind() takes place
  if (!IsPostBack)
  {
      //prime initial cache key value if never initialized
      if (Cache["MyCacheDependency"] == null)
      {
        Cache["MyCacheDependency"] = DateTime.Now;
      }
  }
}


// Evict cache items with an update in dependent cache:
Cache["MyCacheDependency"] = DateTime.Now;
Jolynjolynn answered 9/11, 2009 at 20:23 Comment(1)
FYI this solution was used by me for my project that's in production now and has worked great.Jolynjolynn
S
0

Disabling caching (EnableCaching = false) and then forcing a new select (SqlDataSourceInstance.Select(new DataSourceSelectArguments())) before re-enabling caching clears the particular combination of CommandText and CommandParameters being selected, so it's a start.

It still retains the cache of the other combinations of queries executed by the SqlDataSource, though, so this doesn't solve all of the problem.

Sociology answered 1/6, 2009 at 20:42 Comment(0)
P
0

Make sure your CacheExpirationPolicy is Absolute. Use a non-zero CacheDuration (0 means infinite cache duration).

Poock answered 1/6, 2009 at 20:42 Comment(1)
Once I've set those to be the case, and I execute another Select(), it clears the cache of the query I'm executing, but retains all other permutations of CommandText,CommandParameters, etc. How do I clear those as well?Sociology
J
0

I had the same problem, although the "EnableCaching=false".
Under heavy load, I saw some caching happening. This caching caused me to miss much data.
I found it might be related to caching.
So I use Response.Cache.SetCacheability(HttpCacheability.NoCache); on my code behind. It seems to work and after 12 hours I don't have the least missing data.

Jeramyjerba answered 7/12, 2020 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.