views:

39

answers:

1

I currently have a control that called MyGridview that inherits Gridview. It has a paging template within it for customized paging options, and I'm at the point where I want to cache the initial datasource for better performance.

I haven't done this in a long time, so perhaps there is a different solution these days with the newer frameworks. Before, I simply used a Cache object that was named whatever the gridview was named. I couldn't use the same gridview name through the application though.

Is there a best way to have a cache object, or some other object like a session within the control to store those unique datasets for paging and sorting?

A: 

Since you are talking a custom control, I would recommend abstracting it so whoever is using the application can define their own strategy. Define an interface:

public interface IGridCaching {
  object LoadCache();
  void SaveCache(object data);
}

Create a default implementation DefaultGridCaching and implement the solution you want, and expose it as a property of the Grid:

public IGridCaching Caching
{
    get { return this._caching ?? new DefaultGridCaching(); }
    set { this._caching = value; }
}

And then it becomes customizable. There haven't been a huge amount of changes; caching is still a good option. Using the Enterprise Library Caching block is another caching solution. But this way, caching is customizable.

HTH.

Brian
Let me ask this... suppose I do this and the cache key ends up being the same for two datasources. Data will be overwritten in the cache, and that's what I want to avoid. Is there any solutions out there than ensure unique keys that link back to the current instance of the control?
jlrolin
Appending the user ID or the current session ID, in addition to the grid view ID, to the cached key would resolve this issue. That's typically what is done for caching.
Brian