Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
In our ASP.NET application we have DropDownLists with standard values from reference data(e.g list of Countries).
And we used Cache to avoid re-loading them from the database. The function was like the following:
            private void LoadItems()
            {
                  ListItem[] cachedItems;
                  cachedItems = GetAppCacheItems();
                  if (cachedItems == null)
                  {
                        //Load the reference data from the database omitted for simplicity
                        SetAppCacheItems(cachedItems);
                  }
                  Items.Clear();
                  Items.AddRange(cachedItems);
            }
 And this method worked for a long time. Unfortunately, some users complained that sometimes combo-boxes showed values, different from what they selected. Delelopers tried to reproduce the problem on the development machines without success.
I've wrote Watin test case trying to reproduce the problem, and it helped me to find the cause.
ListItem includes Selected  property and keeping it in a cache effectilely shares the selection between all users. If one user selected AU as country, but other user selected US at the same time, the  first user can see US as his selection.
 
I had to change code to recreate ListItems for each request.
 
It is still a good idea to keep returned from database data as Dataset or other collection in a cache(For example see Peter A. Bromberg's article VB.Net: Database/xml enabled, self-cacheing, TooltipDropDownList ServerControl orMichael Thomas's Custom auto-bound Drop Down List post).
 

I was surprize to find, that Core C# and .NET  by Stephen C. Perry recommends to cache ListItemCollection, which will cause the bug. 

Posted on Friday, September 7, 2007 2:15 PM ASP.NET | Back to top


Comments on this post: Do not cache Web ListItems

# re: Do not cache Web ListItems
Requesting Gravatar...
Hi Michael,

Nice to read that WatiN helped you locate and solve the issue!

Jeroen van Menen
Lead dev WatiN
Left by Jeroen van Menen on Sep 14, 2007 7:24 AM

# re: Do not cache Web ListItems
Requesting Gravatar...
I think you SHOULD cache listitems. Just put them in the cache before the selected is set, and only load the drop downs if not posted back. The viewstate takes care of the contents of the drop down on post back.
Left by lws on Nov 12, 2007 6:59 AM

# re: Do not cache Web ListItems
Requesting Gravatar...
Lws,
The objects in cache are shared between all users. When individual listbox is constructed, it is nesessary to create clones of ListItems or new listItems. But ListItemCollection.AddRange keeps references to the same ListItem objects. And changing the ListItem object by assigning Selected value affects all users, who has references to the item.
Left by Michael Freidgeim on Nov 12, 2007 11:31 PM

# re: Do not cache Web ListItems
Requesting Gravatar...
Cool,

I did not get it is this the code that some user have some problems with ,or is it the correct code!

Thanks
Left by web development company on Aug 18, 2009 12:05 PM

# re: Do not cache Web ListItems
Requesting Gravatar...
William,
The code example is incorrect, you shouldn't cache ListItems, because listItem includes selection,but it ca't be shared between different users.
Left by Michael Freidgeim on Aug 19, 2009 7:16 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net