Is there a way to cache the results of a MySQL query, specifically a data-reader?
I have some mundane queries that really only need to be performed once when the application is loaded, then each form can use the cache values instead of querying the remote server.
At present my method is to use MySqlDataReader to retrieve the data and store it in another MySqlDataReader so I can retrieve it at a later time (example below)
if (parentfrm.prioritylist != null)
{
if (parentfrm.prioritylist.HasRows)
{
using (var rows = parentfrm.prioritylist)
{
while (rows.Read())
{
cboxitem cbi = new cboxitem(int.Parse(rows["priorityid"].ToString()), rows["label"].ToString());
cb.Items.Add(cbi);
}
}
}
else
{
query = @"SELECT priorityid, label FROM prioritylist WHERE active = 'Y' ORDER BY theorder ASC";
parentfrm.prioritylist = db.localfetchrows(query);
if (cb != null)
{
using (var rows = db.localfetchrows(query))
{
while (rows.Read())
{
cboxitem cbi = new cboxitem(int.Parse(rows["priorityid"].ToString()), rows["label"].ToString());
cb.Items.Add(cbi);
}
}
}
}
}
else
{
query = @"SELECT priorityid, label FROM prioritylist WHERE active = 'Y' ORDER BY theorder ASC";
parentfrm.prioritylist = db.localfetchrows(query);
if (cb != null)
{
using (var rows = db.localfetchrows(query))
{
while (rows.Read())
{
cboxitem cbi = new cboxitem(int.Parse(rows["priorityid"].ToString()), rows["label"].ToString());
cb.Items.Add(cbi);
}
}
}
}
Then anywhere in the code, just use
CachedItems.GetPriorityList()
to access the cached list.