ASP.NET - how to implement cache to dropdown list

Asked By nida on 11-May-11 07:08 AM
hi all!

im a fairly new user, i have created a class in BLL file for cache items code as below.

public class TestCache

{

static Cache _myCache;

public TestCache(Cache cache)

{

_myCache = cache;

}

public static void AddCache(string key, string val)

{

_myCache[key] = val;

}

public static object GetCacheByKey(string key)

{

return _myCache[key];

}


public static void AddCache(string Key, object Value, CacheDependency Dependencies, int Minutes)

{

_myCache.Insert(Key, Value, null, DateTime.Now.AddMinutes(Minutes), Cache.NoSlidingExpiration);

}


public int Count

{

get { return _myCache.Count; }

}


public long EffectivePercentagePhysicalMemoryLimit

{

get { return _myCache.EffectivePercentagePhysicalMemoryLimit; }

}


public IDictionaryEnumerator GetEnumerator()

{

return _myCache.GetEnumerator();

}

public static object Remove(string key)

{

return _myCache.Remove(key);

}


i want to create a combo box in presentation to store all values of cache. how would i go about coding this in .cs file?

any help would be much apreciated.

:)

















Ravi S replied to nida on 11-May-11 07:11 AM
HI

you cache the datasource of the dropdownlist, not the dropdown itself.

refer these links...you will find examples

http://weblogs.asp.net/pleloup/archive/2003/09/05/26431.aspx
http://wiki.asp.net/page.aspx/655/caching-in-aspnet/
http://forums.asp.net/t/1629547.aspx?How+to+implement+caching+to+a+dropdown+list%3F
nida replied to Ravi S on 11-May-11 07:13 AM
hey thanks for the quick responce! im on  a dealine of finshing this so any help is much appreciated! shall have a read at suggested links!
Venkat K replied to nida on 11-May-11 07:16 AM
What is the type of _myChache i see it's an object.
You can convert it to datatable and then bind to dropdownlist as follow:

myDDL.Datasource= (DataTable)cache("_myCache")
myDDL.Databind()

or you can treat the elements inside the cache as array and you can loop through the array.

foreach(...loop as array)
{
  myDDL.Datasource= cache(_myCache[key]) 
  myDDL.DataTextField="YourFieldName" 
  myDDL.Databind()
}

Thanks
Asked By nida on 11-May-11 07:24 AM
hello, in mu .aspx file i was using coding to create a combo as below

<asp:Label ID="Label4" runat="server" CssClass="CommonLabels" EnableViewState="False"

Text="Cache Items" Width="90px"></asp:Label>

<telerik:RadComboBox ID="CacheItems" runat="server"

Width="200px" TabIndex="21" DataSourceID="ObjectDataSource1">


</telerik:RadComboBox>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetCacheByKey" EnableCaching="true"

TypeName="SystemPctLibDll.BLL.NebulaCache">

<SelectParameters>

<asp:Parameter Name="Key" Type="String" />

</SelectParameters>

</asp:ObjectDataSource>

 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Delete Cache Item" /><br />


<br />

i had the idea that in my .cs file i would be able to link this to button althought this is where i got stuck! the class i created previously i wanted to be able to use the GetEnumerator Method., is this possible? any help in code woild be great

protected void Button1_Click(object sender, EventArgs e)

{


RadAjaxManager1.Alert("please select cache item from list to be removed");

}



Asked By nida on 11-May-11 07:40 AM
hi,

im now using in bll as suggested.

public static void AddCache(string Key, object Value, CacheDependency Dependencies, int Minutes)

{

_myCache.Insert(Key, Value, null, DateTime.Now.AddMinutes(Minutes), Cache.NoSlidingExpiration);

}


what is the correct method to create a combobox and load all cache items for this?
Asked By nida on 11-May-11 07:54 AM
could you please provide me with some code exaple if what you mean? im fairly new so unsure  :(
Riley K replied to nida on 11-May-11 08:01 AM
To cache the list I suggest using an ObjectDataSource with Cache set and then force a load of the ObjectDataSource by calling the Select Method, then using the IEnumerable type, iterate over the ObjectDataSource’s assigned type.

<asp:ObjectDataSource ID=”ObjectDataSource1″ runat=”server”

  CacheDuration=”30″ EnableCaching=”True”

  SelectMethod=”GetData”

  TypeName=”DataSetLevelsTableAdapters.SessionLevelsTableAdapter”>

</asp:ObjectDataSource>


The DataSource TypeName is simply a typed dataset which retrieves a resultset with a primary key and a description.
Use the Cached List on an ASPX Page

To use the cache list on the page, I first store what is in the DropDownList into a Dictionary which I can later reference on the page. Here is what I do in the Page_Load event.

public partial  class Sessions : System.Web.UI.Page

{

   protected Dictionary<int, string> SessionLevelsDictionary;

   protected void Page_Load(object sender, EventArgs e)

   {

    SessionLevelsDictionary =

     new Dictionary<int, string>();

     IEnumerable someData = ObjectDataSource1.Select();

     foreach (StuffData stuffData in someData)

     {

        SessionLevelsDictionary.Add

        (stuffData.Id,stuffData.Name);

     }


Now, I have in the page class a reference to all the values I may want to use on the page. Say, for example I have a Repeater control that is going to display 200 rows of data that includes a foreign key to one of my small list tables. I want to show the data in a Label control but don’t want to have any codebehind to set the data. Here is how I can access my dictionary to perform this trick.

<asp:Label ID=”Label4″ runat=”server” Width=”90″

 Text=’<%# (string) SessionLevelsDictionary[(int) Eval("SessionLevel_id")] %>‘>

</asp:Label>