Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching and Tests #25

Open
wants to merge 80 commits into
base: master
Choose a base branch
from
Open

Caching and Tests #25

wants to merge 80 commits into from

Conversation

torandi
Copy link
Contributor

@torandi torandi commented Jun 5, 2013

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...

p-blomberg and others added 30 commits May 7, 2012 14:29
static in base class does not by itself use late static binding, only if the derived class declared the same variable. Thus the from_field cache would collide if two tables had the same column name.

For instance:

    $a = Event::from_id(1);
    $b = Row::from_id(1);

    var_dump($a);
    var_dump($b);

Since both tables use "id" the results would collide and show up as the same instance.
torandi and others added 24 commits March 11, 2013 19:05
The correct order is expected, real
Eg. $foo->Bar(array('baz' => 'bar'))
Undo of commit e8c7537
"Invalid" names after : in column names should be accepted,
so that one can construct several where clauses on the same column.
After this patch, it only deletes the keys used for structure cache.
This is important, because I have other caches in memcache that I
want to keep, even when I run migrations that alter db structure.
And a script to install phpunit
@torandi
Copy link
Contributor Author

torandi commented Jun 5, 2013

Sorry about the big pull request, but it's hard to separate (for example: a lot of the tests test caching behaviour, and that the normal behaviour works with and without caching).

@torandi
Copy link
Contributor Author

torandi commented Sep 8, 2013

Some more commits where added; nothing big. Most important is the addition of prefix to memcache structure cache, allowing multiple BO's to run in parallell on the same memcache instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants