|
Posted by Henk Verhoeven on 02/20/07 00:58
Hi,
Of course it depends on what you need to do and how much processing
power you have at hand. For some purposes using a relational database
may itself be a bad idea. But for most apps data objects can work fine,
if you implement caching. When i developed the first version of
phpPeanuts i wanted it to be the simpelest thing that could possebly
work so i did not include caching. But when i tested my first serious
app with it, caching was the first thing to be added. Without it it
simply did not perform. With caching the app only fired 1/4th to 1/10th
of the queries, this solved the performance problem. I implemented
caching in the framework and i have not yet had to develop an app for
wchich it was not sufficient.
To implement caching each object type needs to have a key. For each
object that is retrieved from the database a reference* is added to the
cache, using its key as key in an associative array. Associative arrays
are great for caching because they use hashed lookup to quickly retrieve
objects. With hashed lookup a there is very little searching, and the
searching does not increase substantially with large arrays.
When navigating over a '1-n' relationship in the '1' direction you have
the (now foreign) key, so you first look in the cache to see if the
object has already been loaded. If it has not, you load it and cache it
and return a reference. This mechanism is so fast that it is not needed
to store the reference to a related object in a member variable of the
relating object. This saves you a big headache with circular references
that tend to make php unstable.
When navigating over an 1-n relationship in the n direction the chache
is of little help**. Therefore it is generally a good idea to store each
array of objects you retrieve in a member variable of the relating object.
BTW, you do not have to implement insert/delete/update methods for each
object type, with metadata you can implement generic insert, delete and
update methods so you only need three methods however many types you
make. Or you could download a framework that does all this ;-).
Greetings,
Henk Verhoeven,
www.phpPeanuts.org.
BTW phpPeanuts does not work if you need arbitrary keys. It allways
needs 'id' to be the key. Simpelest thing...
* in php5 it is not necessary to think about references, but in php4 you
may get a subtantial performance gain by using variable references here.
** However, if an object is stored in multiple tables but you do not
know in advance in which ones (like with polymorphism) the cache may
save you an extra query to retrieve additional data.
[Back to original message]
|