I've moved this journal to VOX.com. See you at
http://jkondo.vox.com/
I uploaded MoCo ver 0.05 today.
I fixed a bug that cached objects are not flushed correctly when they're stored without primary keys.
I added keys accessor to MoCo and changed flush mechanism.
If you define Entry.pm like this,
package Entry;
__PACKAGE__->primary_keys(['entry_id']);
__PACKAGE__->keys(['uri']);
and, retrieve an entry from uri,
my $e = Entry->retrieve_by_uri('http://123');
and, create a new entry.
my $e2 = Entry->create(uri => 'http://123');
The first cache, $e is flushed correctly.
(I'm still considering about the name.)
I changed the name of my model class to MoCo.pm
It's light & fast Model Component.
http://search.cpan.org/dist/MoCo/ (it's processing now)
Also, I implemented session features today.
You can delay your update/create operation using MoCo's session.
If you call MoCo->start_session once, a session will be started and update/create queries will be delayed until the session will be ended or save method will be called expressly.
# in your web server etc..
MoCo->start_session;
# in your scripts
my $user = Blog::User->retrieve(123);
$user->name('jkondo'); # not saved now. changed in cache.
print $user->name; # 'jkondo'
$user->save; # update db
print Blog::User->retrieve(123)->name; # 'jkondo'
# Or, update queries will be thrown automatically after ending session.
$user->name('jkontan');
Moco->end_session;
print Blog::User->retrieve(123)->name; # 'jkontan'
I uploaded Class::Moco 0.03 today.
Class::Moco is easy to cache model component.
It provides the way to create models like Class::DBI and store objects in cache easily.
http://search.cpan.org/dist/Class-Moco/
I'd like to get feedbacks.
Here is the SYNOPSIS of Class::Moco.
# First, set up your db.
package Blog::DataBase;
use base qw(Class::Moco::DataBase);
__PACKAGE__->dsn('dbi:mysql:dbname=blog');
__PACKAGE__->username('test');
__PACKAGE__->password('test');
1;
# Second, create a base class for all models.
package Blog::TableObject;
use base qw 'Class::Moco'; # Inherit Class::Moco
__PACKAGE__->db_object('Blog::DataBase');
1;
# Third, create your models.
package Blog::User;
use base qw 'Blog::TableObject';
__PACKAGE__->table('user');
__PACKAGE__->primary_keys(['user_id']);
__PACKAGE__->has_many(
entries => 'Blog::Entry',
{ key => 'user_id' }
);
__PACKAGE__->has_many(
bookmarks => 'Blog::Bookmark',
{ key => 'user_id' }
);
1;
package Blog::Entry;
use base qw 'Blog::TableObject';
__PACKAGE__->table('entry');
__PACKAGE__->primary_keys(['entry_id']);
__PACKAGE__->has_a(
user => 'Blog::User',
{ key => 'user_id' }
);
__PACKAGE__->has_many(
bookmarks => 'Blog::Bookmark',
{ key => 'entry_id' }
);
1;
package Blog::Bookmark;
use base qw 'Blog::TableObject';
__PACKAGE__->table('bookmark');
__PACKAGE__->primary_keys(['user_id','entry_id']);
__PACKAGE__->has_a(
user => 'Blog::User',
{ key => 'user_id' }
);
__PACKAGE__->has_a(
entry => 'Blog::Entry',
{ key => 'entry_id' }
);
1;
# Now, You can use some methods same as in Class::DBI.
# And, all objects are stored in cache automatically.
my $user = Blog::User->retrieve(user_id => 123);
print $user->name;
$user->name('jkontan'); # update db immediately
print $user->name; # jkontan
my $user2 = Blog::User->retrieve(user_id => 123);
# $user is same as $user2!
# You can easily get has_many objects array.
my $entries = $user->entries;
my $entries2 = $user->entries;
# $entries is same reference as $entries2!
my $entry = $entries->first; # isa Blog::Entry
print $entry->title; # you can use methods in Entry class.
Blog::Entry->create(
user_id => 123,
title => 'new entry!',
);
# $user->entries will be flushed automatically.
my $entries3 = $user->entries;
# $entries3 isnt $entries!
print ($posts1->[-1] eq $posts2->[-1]); # 1
print ($posts1->[-1] eq $posts3->[-1]); # 1
# It's same instance!