ObjectDataSource caching questions

J

J055

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
 
S

Steven Cheng[MSFT]

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.)
 
M

Manu

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/PermaLink,guid,eaa3eed8-f997-43c4-8c30-78c2f72d0c86.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:
 
Joined
Aug 14, 2007
Messages
1
Reaction score
0
Hi,

I had a lot of problems with CacheKeyDependency.

Here is the code, that works:

http://ikosoftware.blogspot.com/2007/08/aspnet-20-cachekeydependency.html


Basically,
you need to create CacheKeyDependency (Page_load) if it does not exists:

Code:
protected void Page_Load(object sender, EventArgs e)    
{      
  if (!IsPostBack)
        {
            //create CacheKeyDependency if it does not exists
            if (Cache[SqlDataSource1.CacheKeyDependency] == null)
           {
                Cache[SqlDataSource1.CacheKeyDependency] = new object();
            }
        }
    }

Remove cache:
Code:
 Cache[SqlDataSource1.CacheKeyDependency] = new object();

or

Code:
 Cache["MyTestKey"] = new object();

That's it!


Full article: http://ikosoftware.blogspot.com/2007/08/aspnet-20-cachekeydependency.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top