473,388 Members | 1,207 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,388 software developers and data experts.

Caching objects

My website has different products and I've developed a nice little object
oreinted class library for them. I then use the objects for products and
such on many different pages. Especially with some products inheriting from
other objects and many of them needing to access the database to initialize
correctly I've created quite the load on my sql server. I was hoping to
alivate this via caching I was hoping do somthing like this.

public partial class TestCache : System.Web.UI.Page
{
protected Product myProduct;

protected void Page_Load(object sender, EventArgs e)
{
myProduct = new Product("productXYZ",
(string)Application["connString"], Cache)
}
}

public class Product
{
protected string connString;
protected int productID;
...
protected decimal price;

public Product(string productName, string newConnString, Cache
currentCache)
{
connString = newConnString
if (currentCache["prodcut-" +productName] != null)
{
this = (Product)currentCache["prodcut-" +productName];
}
else
{
initProduct(productName);
}
}
}

But alas, the this keyword is read only. Is there a way to accomplish this?
I'd rather not have to go through everypage and put both a cache check and
cache insert statement. And it seems redundant to create a wrapper class to
handle the caching.

I suppose I could just cache the database results in the iniProduct
function, but somehow that seems less elegant. Any help?
Jan 27 '06 #1
2 1199


Check my blog:
10/24/2005
http://spaces.msn.com/sholliday/

Your product class should be standalone, and not have "integrated" db calls.

You need something, I would call a Controller class.
class ProductController

public static GetProduct ( string prodid )
{

WebSessionDataStore wds = WebSessionDataStore.GetInstance();
if (null!=wds[prodid])
{
return (Product)wds[prodid];
}
else
{

// create a class, which reads the db... and populates a Product
//but don't put the code IN the Product class

Product p = Product ReadDataBaseAndCreateAProduct();
wds.Add(prodid , p);
}

private Product ReadDataBaseAndCreateAProduct(string prodid)
{

IDataReader idr = (some method to get a datareader on the database);

Product p = new Product();
p.Name = idr.GetString(0);

return p;

}

}
You got 2 things going on.
First, the "integrated data access" in your Product class....is deficient.
Make Product standalone, and create another class, which reads the
database....and populates a Product.

The Session Storer thing from my blog is a nice wrapper for handling caching
in an asp.net application.
...

"Joel Barsotti" <jo**********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
My website has different products and I've developed a nice little object
oreinted class library for them. I then use the objects for products and
such on many different pages. Especially with some products inheriting from other objects and many of them needing to access the database to initialize correctly I've created quite the load on my sql server. I was hoping to
alivate this via caching I was hoping do somthing like this.

public partial class TestCache : System.Web.UI.Page
{
protected Product myProduct;

protected void Page_Load(object sender, EventArgs e)
{
myProduct = new Product("productXYZ",
(string)Application["connString"], Cache)
}
}

public class Product
{
protected string connString;
protected int productID;
...
protected decimal price;

public Product(string productName, string newConnString, Cache
currentCache)
{
connString = newConnString
if (currentCache["prodcut-" +productName] != null)
{
this = (Product)currentCache["prodcut-" +productName];
}
else
{
initProduct(productName);
}
}
}

But alas, the this keyword is read only. Is there a way to accomplish this? I'd rather not have to go through everypage and put both a cache check and
cache insert statement. And it seems redundant to create a wrapper class to handle the caching.

I suppose I could just cache the database results in the iniProduct
function, but somehow that seems less elegant. Any help?

Jan 27 '06 #2
switch to the factory model.
public partial class TestCache : System.Web.UI.Page
{
protected Product myProduct;

protected void Page_Load(object sender, EventArgs e)
{
myProduct = Product.Create("productXYZ");
}
}

where Create is a static method that creates a class object. i would move
the cache control and connect string to the factory, i'd also use the
factory model for the settings, and call setup in application start.
-- bruce (sqlwork.com)
"Joel Barsotti" <jo**********@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
My website has different products and I've developed a nice little object
oreinted class library for them. I then use the objects for products and
such on many different pages. Especially with some products inheriting
from other objects and many of them needing to access the database to
initialize correctly I've created quite the load on my sql server. I was
hoping to alivate this via caching I was hoping do somthing like this.

public partial class TestCache : System.Web.UI.Page
{
protected Product myProduct;

protected void Page_Load(object sender, EventArgs e)
{
myProduct = new Product("productXYZ",
(string)Application["connString"], Cache)
}
}

public class Product
{
protected string connString;
protected int productID;
...
protected decimal price;

public Product(string productName, string newConnString, Cache
currentCache)
{
connString = newConnString
if (currentCache["prodcut-" +productName] != null)
{
this = (Product)currentCache["prodcut-" +productName];
}
else
{
initProduct(productName);
}
}
}

But alas, the this keyword is read only. Is there a way to accomplish
this? I'd rather not have to go through everypage and put both a cache
check and cache insert statement. And it seems redundant to create a
wrapper class to handle the caching.

I suppose I could just cache the database results in the iniProduct
function, but somehow that seems less elegant. Any help?

Jan 28 '06 #3

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

Similar topics

2
by: Marwin | last post by:
Hi all, I have a database with 40GB of binary objects stored in image columns in two tables. Our database server is also used for another 15 databases. SQL Server caches the image-column,...
15
by: olle | last post by:
Hi folks. I learning asp.net and compare it with traditional asp and Access-developing. The issue is this one: 1/I have this Ms Acceess adp-project application that works fine on my Ms Sql...
10
by: Jon Maz | last post by:
Hi, My goal is to take the entire html/javascript stream spat out by .aspx pages and save them as simple strings in a database (for caching purposes). I'm not sure how I can get hold of 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...
1
by: Gavin Pollock | last post by:
Is anyone using Caching (HttpRuntime.Cache) in Whidbey? Not sure if there's another newsgroup for this though since it's still beta.... I'm having issues running a system built on 1.1 in a 2.0...
4
by: DylanM | last post by:
I've seen a few examples on how to cache data in a WinForms GUI, just after some thought on the best solution. The data I'm trying to cache will be generally be small collections of Business...
2
by: thedotnetarchitect | last post by:
To keep the background simple here is my problem, I have two web servers in a web farm I would like to have a centralized caching system on another server, where I can add and remove items from...
3
by: Gary W. Smith | last post by:
I had a couple questions about data caching. We have a site that gets a huge amount of traffic to a few specific pages that have a lot of data on them (300k hits/hour during peak, about 6-10 data...
4
by: Girish | last post by:
i have to implement caching in my application, can any one tell me about the good techniques to implement caching, or provide some architectural help , so i can use it to my application. i want...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.