By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

ObjectDataSource caching questions

P: n/a
Hi

I've implemented caching for my ObjectDataSource.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
EnableCaching="True" CacheDuration="10" CacheExpirationPolicy="Sliding"

SelectMethod="GetUsers" TypeName="BizObject"></asp:ObjectDataSource>

This works, however I need to explicitly clear/remove the cache when I
delete record(s). (It's a custom delete, not a ObjectDataSource builtin
one.) I understand that I need to set the CacheKeyDependency property so
that I can Remove it from the cache?

When I add this line to the above the cache stops working, i.e. the
SelectMethod retrieves the data from the database each time.

CacheKeyDependency="GVkey"

Can someone point me in the right direction? How do I see what's in the
cache? If I write this out it is always zero even when the cache is working.

Literal1.Text = "The number of items in the cache:" +
Cache.Count.ToString();

Very confused. Thanks
Andrew
Apr 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Andrew,

Thank you for posting.

Regarding on the ObjectDataSource's caching behavior , here are some of my
understanding and suggestion:

The objectDataSource control's cache is maintained internally so that we
can not manually view or query through the Page.Cache or HttpContext.Cache
collection. And the cache is created and used during the Select method of
the ObjectDataSource, that means, when ObjectDataSource.Select method is
called, it will check the cache to see whether it should use cache or redo
the query from the underlying data access class. This is different from
the behavior GridView (or other databound control) call
DataSourceControl.Select since DataBound control may store data in
ViewState to avoid querying the datasourcecontrol. In other word, if you
want to test the ObjectDatasource control's cache behavior, you can
manually call ObjectDataSource.Select method and also put some trace
code(like Httpcontext.Current.Response.Write...) in the data access
control's select method to check the behavior.

Also, for the "CacheKeyDependency", when it is set, that means the
ObjectDataSource.Select method will use the cached data only when there is
an existing cache entry in the application Cache and the key of it equals
to the "CacheKeyDependency" property wo set on the ObjectDataSource
control. So we need to add such a cache entry so that the ObjectDataSource
will use the cache, then, when we want to explicitly expire the
ObjectDataSource's cache, just remove that entry(we need to readd it if we
want the objectdatasource to begin using cache again). e.g:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
CacheDuration="10"
EnableCaching="True" SelectMethod="GetProducts"
TypeName="TestDataObject" CacheKeyDependency="MYKEY">
.................

//test method to manually call ObjectDatasource.Select method
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<br/>Button1_Click");
ObjectDataSource1.Select();
}

//manually remove the cache entry of the objectdatasource's KeyDependency
protected void btnInvalid_Click(object sender, EventArgs e)
{
Cache.Remove(ObjectDataSource1.CacheKeyDependency) ;
}

// set the key entry
protected void Button2_Click(object sender, EventArgs e)
{

Cache[ObjectDataSource1.CacheKeyDependency] = new object();

}

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Apr 11 '06 #2

P: n/a
Hi,

this is from a post about the ObjectDataSource:

"Sometimes we may need to invalidate the cached data for the
ObjectDataSource. To do so we can use the CacheKeyDependency property.
When the control caches the data returned from the SelectMethod, it
will check the value of CacheKeyDependency, and if it's set the
cached data will have a dependency on that cache key. When we want to
remove the cached data we can use the Cache.Remove method using the key
specified in the CacheKeyDependency".

If you want to check the full post:

http://www.manuelabadia.com/blog/Per...2f72d0c86.aspx

The ObjectDataSource uses the internal cache to store data. To view the
cached data you can use Nikhil's web development helper or Cristian
Civera's ViewState Analyzer.

Hope it helps,

Manu

J055 ha escrito:
Hi

I've implemented caching for my ObjectDataSource.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
EnableCaching="True" CacheDuration="10" CacheExpirationPolicy="Sliding"

SelectMethod="GetUsers" TypeName="BizObject"></asp:ObjectDataSource>

This works, however I need to explicitly clear/remove the cache when I
delete record(s). (It's a custom delete, not a ObjectDataSource builtin
one.) I understand that I need to set the CacheKeyDependency property so
that I can Remove it from the cache?

When I add this line to the above the cache stops working, i.e. the
SelectMethod retrieves the data from the database each time.

CacheKeyDependency="GVkey"

Can someone point me in the right direction? How do I see what's in the
cache? If I write this out it is always zero even when the cache is working.

Literal1.Text = "The number of items in the cache:" +
Cache.Count.ToString();

Very confused. Thanks
Andrew


Apr 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.