caching an array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to cache an array.
1st - can I do this and
2nd -what is the syntax please?

I have added the namespace System.Web.Caching.

I tried cache.add("myArray") = myArray; from here
[http://www.dotnetspider.com/qa/Question23244.aspx] and a few other
derivatives but my web searches have offered little solution. Your help is
most welcome pls.
 
P Ocor,

The simplest way is to use the Cache property on your ASPX page, like
so:

// Get the array from the cache:
MyType[] array = (MyType[]) Cache["key"];

The thing is, it might not always be there, as the Cache will be purged
if neessary (or if the condition keeping the item in the cache expires).
Because of that, you should wrap the functionality like so:

private MyType[] GetArray()
{
// Try to get the return value.
MyType[] retVal = (MyType[]) Cache["key"];

// If the item does not exist, then
// add it here.
if (retVal == null)
{
// Add the array to the cache here.
retVal = <create array here>;

// Add the item to the cache.
Cache["key"] = retVal;
}

// Return the array.
return retVal;
}
 
P Ocor,

The simplest way is to use the Cache property on your ASPX page, like
so:

// Get the array from the cache:
MyType[] array = (MyType[]) Cache["key"];

The thing is, it might not always be there, as the Cache will be purged
if neessary (or if the condition keeping the item in the cache expires).
Because of that, you should wrap the functionality like so:

private MyType[] GetArray()
{
// Try to get the return value.
MyType[] retVal = (MyType[]) Cache["key"];

// If the item does not exist, then
// add it here.
if (retVal == null)
{
// Add the array to the cache here.
retVal = <create array here>;

// Add the item to the cache.
Cache["key"] = retVal;
}

// Return the array.
}
 
Back
Top