edruid/BasicObject — pull request #25 by torandi (2013-06-05 22:34:34+00:00)
Title: Caching and Tests

Caching
======
Adds two kinds of caching, which speeds up performance a lot.

All caching are off by until turned on by BasicObject::enable_cache() or BasicObject::enable_structure_cache($mc)

Structure Cache
--------------------
Caches all BO structure queries (Foreign key, primary key, table column names etc) in memcached.

This cache gives the highest performance boost, but BasicObject::clear_structure_cache($mc) must manually be called changes to the database structure have been made. I recommend using a migration-like system (for example torandi/php-migrations with a post_migration-hook where one can trigger the cache clean.

Query Caching
-----------------
Caches query result, but only inside a single request. This still gives some performance boost, since one often execute the same query multiple times in a page view (ex:
forum, look up user from user id, multiple posts from same user, so the user is looked up multiple times).

This cache is only per request, but the query cache for ALL BasicObjects are reset when ANY BasicObject in commited, this is due to the relations and dependencies between models can be quite complex.

A warning: If your code have some manually updates of the database you will have to manually trigger BasicObject::invalidate_cache().

Example:

$foo = Foo::from_id(1);

$db->query("UPDATE FOO set baz='bar' where id=1;")

// Foo::from_id(1) still returns old value of baz, since BO doesn't know the value have changed

Tests
==========
Add a structure for tests, and some tests (not remotely close to full coverage).

Tests are run with ./tests.sh
Tests depend on PHPUnit, which can be installed with ./install_phpunit.sh

tests folder includes Blueprint.php, which is a class written for this that allows you to create blueprints for test objects, see tests/blueprints/README.md

---------------------------------------------------------

Also: Maybe I should put the text about caching in the README...