Skip to main content
Inspiring
May 28, 2010
Answered

a data structure to represent a table in ram ?

  • May 28, 2010
  • 3 replies
  • 1291 views

So I've implemented a shoppingCart using a table in the database and it's fine.

BUT, I'm thinking the table approach isn't that efficient - for example clicking Add to Cart does two things:

1) insert the row into the shoppingCart table

2) display the whole shopping cart for that session

I'm thinking, wouldn't it be nice if I could just store the shoppingCart in a data structure ? I'm sure Coldfusion supports such a strucuture with addRow , removeRow commands - I just can't think of it.

Do you think this is an efficient approach ?

This topic has been closed for replies.
Correct answer ilssac

I read the question as not updating the database every request.  But rather keeping the shopping cart information in memory while the user was adding, removing, fiddling with it.  Thus only committing it to permanent database storage at the end.

3 replies

BKBK
Community Expert
Community Expert
May 30, 2010
a data structure to represent a table in ram ?

Yes. In fact, you could literally use a structure to represent the shopping-cart in memory. It would begin like this:

<cfset session.shoppingCart = structnew()>

I'm sure Coldfusion supports such a strucuture with addRow , removeRow commands - I just can't think of it.

You would then be thinking of structInsert() and structDelete.

Do you think this is an efficient approach ?


Yes. However, I have a different suggestion. What you're doing is a learning exercise. That can only be a good thing.

If you're serious about setting up shop, which I assume you are, then you should invest in shopping-cart software. They cost little, relative to the benefit you could derive from them. Mary Jo's CFWebstore seems good value for money.

Inspiring
May 31, 2010

Iain is correct in his assertion that I'm would be only storing the shopping cart in RAM and later writing to tables when the order is completed.

I see the shopping experience broken into two main chunks of code -

1) shoppingCart

2) checkout

ShoppingCart holds the list of user's chosen items - merchandise_ID , qty

Checkout invokes code which dumps the shopping cart into permanent table(s) - customers, orders and ordered_items

I've got most of it working with tables, but in terms of performance, I was thinking of holding the shopping cart in session RAM. The question was mainly, if there's any real benfit of holding the shoppingCart in RAM,, but I guess it's trivial.

BKBK
Community Expert
Community Expert
May 31, 2010
I was thinking of holding the shopping cart in session RAM.

I thought I suggested that.

The question was mainly, if there's any real benfit of holding the shoppingCart in RAM,, but I guess it's trivial.

It is indeed the main question, but it isn't trivial.The answer, correct me if I'm wrong, is to use a struct (or other equivalent map object) in place of a database table, and to hold it in session scope.

You should ensure what you mark as the solution isn't ambiguous or misleading to those who come here looking for answers to similar questions. We all contribute to the quality of the forum.

Inspiring
May 28, 2010

This is not an efficiency consideration.  It's a business consideration.  If there is a requirement to keep the data after the purchase has been made, you store the data in a database.

As long as you are 100% certain that the vendor never asks how many of Item X he sold between Date A and Date B, go with the ram approach.

ilssac
ilssacCorrect answer
Inspiring
May 29, 2010

I read the question as not updating the database every request.  But rather keeping the shopping cart information in memory while the user was adding, removing, fiddling with it.  Thus only committing it to permanent database storage at the end.

ilssac
Inspiring
May 28, 2010

You could build your own record set variable structure with the queryNew(), queryAddRow(), querySetCell(), etc functions.

You could build your own shopping cart structure with that goes beyond a basic record set.

You could build a CFC that encapsolated that structure with methods to add data to it, remove data from it, output the current data and more.

From this point you are steaddly moving towards Object Oriented conectps of Beans, Data Access Objects, Model View Controlers, Obect Relationship Mapping and beyond!

Message was edited by: Ian Skinner P.S. "Represent a table in ram" is the concept behind Object Relationship Mapping (ORM).  And ColdFusion 9 has started including the Hibernate ORM project.  If you want to jump into this, I can only suggest reading the documentation and blogs about ColdFusion 9, Hibernate and ORM.

Inspiring
May 28, 2010

great - I'll take a look Ian