473,246 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Data Cache Question

Hi,

Some newbie questions.. :-)

First, what is the namespace to use for the Cache class ?

When I use this bit of code I get an error
if (Cache["myUserList"]==null) Cache.Insert("myUserList",userlist);
I don't know which namespace to use.
And, is it possible to create a Data Cache for each user
I have a seach engine page that returns the result to another page.
And I have a detail page for each item (like a simple catalog).
I would like to navigate between each item and go back to the result page
without requerying the database.

Any advice ?

Stan
Oct 25 '06 #1
5 2083
Hi Stan
When I use this bit of code I get an error
if (Cache["myUserList"]==null) Cache.Insert("myUserList",userlist);
I don't know which namespace to use.
In 2.0, its under System.Web.Caching
And, is it possible to create a Data Cache for each user
I have a seach engine page that returns the result to another page.
And I have a detail page for each item (like a simple catalog).
I would like to navigate between each item and go back to the result page
without requerying the database.

Any advice ?
It is possible to do this, you can use the session id as part of the
cache key.

However, it might be a good idea to share all the caches. You can
create a key using all the search parameters so the results remain
unique

Sam

Oct 26 '06 #2
Hi Samuel,

I thank you for your reply.
What is the difference with System.Web.Caching namespace and
HttpContexte.Current.Cache ?
For the second Part, I use a session variable and I store the datatable
using this session.

I don't know if it's a good idea..

Stan

"samuelhon"
Hi Stan
>When I use this bit of code I get an error
if (Cache["myUserList"]==null) Cache.Insert("myUserList",userlist);
I don't know which namespace to use.

In 2.0, its under System.Web.Caching
>And, is it possible to create a Data Cache for each user
I have a seach engine page that returns the result to another page.
And I have a detail page for each item (like a simple catalog).
I would like to navigate between each item and go back to the result page
without requerying the database.

Any advice ?

It is possible to do this, you can use the session id as part of the
cache key.

However, it might be a good idea to share all the caches. You can
create a key using all the search parameters so the results remain
unique

Sam

Oct 26 '06 #3
HttContext.Current.Cache is a property of type System.Web.Caching.Cache...

You are getting an instance of a object and it's declaration mixed up.

The Page object exposes a Cache property, so from a page, user control or
master page, youc an simply do:

Cache.Insert(...)

From a business layer, you can do HttpContext.Current.Cache

HttContext.Current could be null though. So you can also use
HttpRuntime.Cache - which'll never be null, even in a non-web context. They
are all the same instance (it's a singleton as far as I know). So you can
intermix all of them.

There are two main difference between sessions and Cache:
- The cache is global to all users
- The session guarantees (more or less) that the data will be there.

As suggestion by Samuel, it's very easy to circumvent the first difference
by using a smart cachekey (say one that includes a sessionId).

As for the 2nd one, a lot of us prefer the Cache over the session because it
allows .NET more flexibility in managing it's memory. You just need to
program more defensively, because a cache lookup might return null...
If you are only looking to store data from one page to another (like a list
and then a detail page), you could consider using the HttpContext.Items
collection and using a Server.Transfer. From what I understand it's the best
bet because it's the right scope - it's short lived and per-user.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Stan SR" <st**@pasdepam.netsunset.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
Hi Samuel,

I thank you for your reply.
What is the difference with System.Web.Caching namespace and
HttpContexte.Current.Cache ?
For the second Part, I use a session variable and I store the datatable
using this session.

I don't know if it's a good idea..

Stan

"samuelhon"
>Hi Stan
>>When I use this bit of code I get an error
if (Cache["myUserList"]==null) Cache.Insert("myUserList",userlist);
I don't know which namespace to use.

In 2.0, its under System.Web.Caching
>>And, is it possible to create a Data Cache for each user
I have a seach engine page that returns the result to another page.
And I have a detail page for each item (like a simple catalog).
I would like to navigate between each item and go back to the result
page
without requerying the database.

Any advice ?

It is possible to do this, you can use the session id as part of the
cache key.

However, it might be a good idea to share all the caches. You can
create a key using all the search parameters so the results remain
unique

Sam


Oct 26 '06 #4
Karl and Samuel,

Many thanks for your help.
It seems the Cache solution is better for my needs than session.
When you say to use a smart cachekey, does it mean, I could do something
like

if (HttpContext.Current.Cache[mySessionId]==null)
HttpContext.Current.Cache.Insert(mySessionId,myDat atable) ;
return (DataTable) HttpContext.Current.Cache[mySessionId];
I think I try to use this method, but it didn't work. Do I miss something ?

As you ve said, Karl, I use a Business Layer, (that's why I use
HttpContext.Current.Cache).
About the HttpContext.Items, do you think it should be the best approach ?
I have a list of results and a details item page, and I want to navigate to
the previous and next item from the detail page
and of course go back to the result page ?
So what I think, it's to cache the result (coming from a sql query) and use
this cache for the list and details page.
is it a good practice ?

Stan

Karl (or somebody else) if you could tell me how I have to work

HttContext.Current.Cache is a property of type System.Web.Caching.Cache...

You are getting an instance of a object and it's declaration mixed up.

The Page object exposes a Cache property, so from a page, user control or
master page, youc an simply do:

Cache.Insert(...)

From a business layer, you can do HttpContext.Current.Cache

HttContext.Current could be null though. So you can also use
HttpRuntime.Cache - which'll never be null, even in a non-web context.
They are all the same instance (it's a singleton as far as I know). So you
can intermix all of them.

There are two main difference between sessions and Cache:
- The cache is global to all users
- The session guarantees (more or less) that the data will be there.

As suggestion by Samuel, it's very easy to circumvent the first difference
by using a smart cachekey (say one that includes a sessionId).

As for the 2nd one, a lot of us prefer the Cache over the session because
it allows .NET more flexibility in managing it's memory. You just need to
program more defensively, because a cache lookup might return null...
If you are only looking to store data from one page to another (like a
list and then a detail page), you could consider using the
HttpContext.Items collection and using a Server.Transfer. From what I
understand it's the best bet because it's the right scope - it's short
lived and per-user.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Stan SR" <st**@pasdepam.netsunset.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
>Hi Samuel,

I thank you for your reply.
What is the difference with System.Web.Caching namespace and
HttpContexte.Current.Cache ?
For the second Part, I use a session variable and I store the datatable
using this session.

I don't know if it's a good idea..

Stan

"samuelhon"
>>Hi Stan

When I use this bit of code I get an error
if (Cache["myUserList"]==null) Cache.Insert("myUserList",userlist);
I don't know which namespace to use.

In 2.0, its under System.Web.Caching

And, is it possible to create a Data Cache for each user
I have a seach engine page that returns the result to another page.
And I have a detail page for each item (like a simple catalog).
I would like to navigate between each item and go back to the result
page
without requerying the database.

Any advice ?

It is possible to do this, you can use the session id as part of the
cache key.

However, it might be a good idea to share all the caches. You can
create a key using all the search parameters so the results remain
unique

Sam



Oct 27 '06 #5
Dont want to throw a rench at what we have so far, but, as I get more
familiar with what you are trying to do, I get different thoughts about how
to go about doing it.

You might want to consider output cache/partial output caching on the detail
page. This will be the most effective and trouble-free implementation if (a)
the data can stay stale for X amount of time (say 10 minutes) and (b) the
same detail page will get hit more than once within that X minute.

The same applies for the results page. How many search parameters are there?
Is it likely that the same query will happen within X seconds by another
user? If it ISN'T, then caching it globally to all users doesn't make the
most sense.

If you are dealing with a relatively small amount of data, you could hold
all the search results in-memory and query directly against the in-memory
store. This means you only go to the database when the cache is dropped to
get all items back.

If you are dealing with many records, the likely hood of an item being
retrieved from the cache before it expires is small. In this case,all you
are really worried about is caching for the 1 user going back and forth
between details and results.

Your use of mySessionId as the key is fine. If you want to cache multiple
things for the user, say multiple pages of the result, you'll need to stick
it to a made-up key, like "page1: + mySessionId;

To make your business layer truly reusable, ur better off using
HttpRuntime.Cache (although that's really a very minor point).
You should also write it like this:

DataTable myTable = (DataTable) HttpContext.Current.Cache[mySessionId];
if (myTable == null)
{
myTable =GetTableSomehow();
HttpContext.Current.Cache.Insert(mySessionId,myTab le ) ;
}
return myTable;
doing it the way you did, there's a chance for a weird nullreferenceerror...

This is what you have:
if (HttpContext.Current.Cache[mySessionId]==null)
{
HttpContext.Current.Cache.Insert(mySessionId,myDat atable) ;
// *** RIGHT HERE the item could be dumped from the cache!! *///
}
return (DataTable) HttpContext.Current.Cache[mySessionId];
My way, you've established a strong reference to the data (via the myTable)
reference.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Stan SR" <st**@pasdepam.netsunset.comwrote in message
news:eQ****************@TK2MSFTNGP04.phx.gbl...
Karl and Samuel,

Many thanks for your help.
It seems the Cache solution is better for my needs than session.
When you say to use a smart cachekey, does it mean, I could do something
like

if (HttpContext.Current.Cache[mySessionId]==null)
HttpContext.Current.Cache.Insert(mySessionId,myDat atable) ;
return (DataTable) HttpContext.Current.Cache[mySessionId];
I think I try to use this method, but it didn't work. Do I miss something
?

As you ve said, Karl, I use a Business Layer, (that's why I use
HttpContext.Current.Cache).
About the HttpContext.Items, do you think it should be the best approach ?
I have a list of results and a details item page, and I want to navigate
to the previous and next item from the detail page
and of course go back to the result page ?
So what I think, it's to cache the result (coming from a sql query) and
use this cache for the list and details page.
is it a good practice ?

Stan

Karl (or somebody else) if you could tell me how I have to work

>HttContext.Current.Cache is a property of type
System.Web.Caching.Cache...

You are getting an instance of a object and it's declaration mixed up.

The Page object exposes a Cache property, so from a page, user control
or master page, youc an simply do:

Cache.Insert(...)

From a business layer, you can do HttpContext.Current.Cache

HttContext.Current could be null though. So you can also use
HttpRuntime.Cache - which'll never be null, even in a non-web context.
They are all the same instance (it's a singleton as far as I know). So
you can intermix all of them.

There are two main difference between sessions and Cache:
- The cache is global to all users
- The session guarantees (more or less) that the data will be there.

As suggestion by Samuel, it's very easy to circumvent the first
difference by using a smart cachekey (say one that includes a sessionId).

As for the 2nd one, a lot of us prefer the Cache over the session because
it allows .NET more flexibility in managing it's memory. You just need to
program more defensively, because a cache lookup might return null...
If you are only looking to store data from one page to another (like a
list and then a detail page), you could consider using the
HttpContext.Items collection and using a Server.Transfer. From what I
understand it's the best bet because it's the right scope - it's short
lived and per-user.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Stan SR" <st**@pasdepam.netsunset.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
>>Hi Samuel,

I thank you for your reply.
What is the difference with System.Web.Caching namespace and
HttpContexte.Current.Cache ?
For the second Part, I use a session variable and I store the datatable
using this session.

I don't know if it's a good idea..

Stan

"samuelhon"

Hi Stan

When I use this bit of code I get an error
if (Cache["myUserList"]==null) Cache.Insert("myUserList",userlist);
I don't know which namespace to use.

In 2.0, its under System.Web.Caching

And, is it possible to create a Data Cache for each user
I have a seach engine page that returns the result to another page.
And I have a detail page for each item (like a simple catalog).
I would like to navigate between each item and go back to the result
page
without requerying the database.
>
Any advice ?

It is possible to do this, you can use the session id as part of the
cache key.

However, it might be a good idea to share all the caches. You can
create a key using all the search parameters so the results remain
unique

Sam



Oct 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: charlies224 | last post by:
Hi, I am using SQL 2000 and has a table that contains more than 2 million rows of data (and growing). Right now, I have encountered 2 problems: 1) Sometimes, when I try to query against this...
2
by: Wee Bubba | last post by:
2 questions please: 1. At the moment I make a connection to a database whenever a user logs into my website. I run a few SQL's then store some commonly used dataTables in session objects for...
4
by: Guadala Harry | last post by:
Is there any way for one Session to remove and update objects in another Session? I seriously doubt it, but thought I'd ask. Here's why: I have some data that is unique per user (or per session -...
5
by: Scott Reynolds | last post by:
Hello! I developed a web application to display results from the database. Now I need to add search function, to search, sort and filter data. My question is, which way is better... 1)...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
4
by: Fred Nelson | last post by:
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...
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.