Application Data Caching

F

Fred Nelson

Hi:

This is probably a newby question however I can't figure it out!

I'm trying to use application data caching in a web application. I have
read an article on 4 guys from rolla that shows the following syntax:

Write:

Cache["key"] = value;

Read:

value = Cache["key"] - or - value = Cache.Get["key"]

I can save the value (or I think I can) with the "write" statement / I
am unable to get the values back with the "read":


I receive the compile message:

Cannot apply indexing with [] to an expression of type 'method group'

Does anyone know how to make this work?

Also: I would very much like to be able to read and update cache values
in a class library. I have added the reference System.Web.Caching
however this doesn't work - is there a way to do this?

Thanks very much!

Fred
 
S

sloan

when you pull items from the cache, they are "object"'s ... you may have to
cast it to the appropriate type.

object o = null;
if (null!=Cache["key"])
{
o = Cache["key"];
}
DataSet ds = o as DataSet;

if(null!=ds)
{
Console.Writeline(ds.GetXml());
}

That of course .. is if you put a dataset into the cache.

There is a "smarter" way to do this:


see
http://spaces.msn.com/sholliday/ 10/24/2005 entry

it would be trivial to change this from a Session to an Application holder.
 
F

Fred Nelson

Hi Sloan:

Thank you very much for the post - I have it working now!

Fred

when you pull items from the cache, they are "object"'s ... you may have to
cast it to the appropriate type.

object o = null;
if (null!=Cache["key"])
{
o = Cache["key"];
}
DataSet ds = o as DataSet;

if(null!=ds)
{
Console.Writeline(ds.GetXml());
}

That of course .. is if you put a dataset into the cache.

There is a "smarter" way to do this:


see
http://spaces.msn.com/sholliday/ 10/24/2005 entry

it would be trivial to change this from a Session to an Application holder.





Fred Nelson said:
Hi:

This is probably a newby question however I can't figure it out!

I'm trying to use application data caching in a web application. I have
read an article on 4 guys from rolla that shows the following syntax:

Write:

Cache["key"] = value;

Read:

value = Cache["key"] - or - value = Cache.Get["key"]

I can save the value (or I think I can) with the "write" statement / I
am unable to get the values back with the "read":


I receive the compile message:

Cannot apply indexing with [] to an expression of type 'method group'

Does anyone know how to make this work?

Also: I would very much like to be able to read and update cache values
in a class library. I have added the reference System.Web.Caching
however this doesn't work - is there a way to do this?

Thanks very much!

Fred
 
S

sloan

You should post what you did... so others can learn and know what you
did....
if they google and find this post.


Fred Nelson said:
Hi Sloan:

Thank you very much for the post - I have it working now!

Fred

when you pull items from the cache, they are "object"'s ... you may have to
cast it to the appropriate type.

object o = null;
if (null!=Cache["key"])
{
o = Cache["key"];
}
DataSet ds = o as DataSet;

if(null!=ds)
{
Console.Writeline(ds.GetXml());
}

That of course .. is if you put a dataset into the cache.

There is a "smarter" way to do this:


see
http://spaces.msn.com/sholliday/ 10/24/2005 entry

it would be trivial to change this from a Session to an Application holder.





Fred Nelson said:
Hi:

This is probably a newby question however I can't figure it out!

I'm trying to use application data caching in a web application. I have
read an article on 4 guys from rolla that shows the following syntax:

Write:

Cache["key"] = value;

Read:

value = Cache["key"] - or - value = Cache.Get["key"]

I can save the value (or I think I can) with the "write" statement / I
am unable to get the values back with the "read":


I receive the compile message:

Cannot apply indexing with [] to an expression of type 'method group'

Does anyone know how to make this work?

Also: I would very much like to be able to read and update cache values
in a class library. I have added the reference System.Web.Caching
however this doesn't work - is there a way to do this?

Thanks very much!

Fred
 
F

Fred Nelson

Hi Again:

Sloan suggested that I post my code - a good idea - so here it is:

The working code - this handels a string:

Write:

Cache["key1"] = "This is a string saved in application cache";


Read:

object ostring = null;
if (null != Cache["key1"])
{ ostring = Cache["key1"]; };

// if not null (otherwise load blank):

string mystring = ostring as string;


Fred


Fred said:
Hi Sloan:

Thank you very much for the post - I have it working now!

Fred

when you pull items from the cache, they are "object"'s ... you may have to
cast it to the appropriate type.

object o = null;
if (null!=Cache["key"])
{
o = Cache["key"];
}
DataSet ds = o as DataSet;

if(null!=ds)
{
Console.Writeline(ds.GetXml());
}

That of course .. is if you put a dataset into the cache.

There is a "smarter" way to do this:


see
http://spaces.msn.com/sholliday/ 10/24/2005 entry

it would be trivial to change this from a Session to an Application holder.





Fred Nelson said:
Hi:

This is probably a newby question however I can't figure it out!

I'm trying to use application data caching in a web application. I have
read an article on 4 guys from rolla that shows the following syntax:

Write:

Cache["key"] = value;

Read:

value = Cache["key"] - or - value = Cache.Get["key"]

I can save the value (or I think I can) with the "write" statement / I
am unable to get the values back with the "read":


I receive the compile message:

Cannot apply indexing with [] to an expression of type 'method group'

Does anyone know how to make this work?

Also: I would very much like to be able to read and update cache values
in a class library. I have added the reference System.Web.Caching
however this doesn't work - is there a way to do this?

Thanks very much!

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top