473,396 Members | 1,971 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,396 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 2095
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.