Cache Using ObjectDataSource

by rbellantoni 8/26/2010 7:02:00 AM

I needed to bind a large dataset to a gridview, it also contained numerous calculations that slowed down the database call, so I wanted to implement caching for the gridview I was using. 

So I used the default Caching on the ObjectDataSource. Which works great and well except for when you want to programatically refresh the data (say a new filter to be added). Turns out you can actually do it.

 

First make sure that in Page Load you set the CacheKeyDependency if it isn't set (you can also manually set it on the aspx page as a property of the ObjectDataSource):

        if (!IsPostBack)
        {
            //Create CacheKeyDependency if it doesn'tnot exists
            if (Cache[ObjectDataSource1.CacheKeyDependency] == null)
            {
                Cache[ObjectDataSource1.CacheKeyDependency] = new object();
            }
        }

In your code whenever you want the Select() of your ObjectDataSource to fire or just rebind from the database make this call:

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

This allows you to programatically renew/refresh your datasource at will while still retaining the Cache abilities for paging/sorting/grouping etc.

Tags:

.NET | .NET WCF | BlogEngine.NET | SQL SERVER

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  January 2018  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2018

Sign in