Clear cache in SqlDataSource

2019-06-15 08:00发布

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.

3条回答
放我归山
2楼-- · 2019-06-15 08:36

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

查看更多
做自己的国王
3楼-- · 2019-06-15 08:38

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.

查看更多
淡お忘
4楼-- · 2019-06-15 08:51

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;
查看更多
登录 后发表回答