Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jkondo (7642)

  (email not shown publicly)

Journal of jkondo (7642)

Tuesday January 30, 2007
05:33 PM


[ #32283 ]

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.

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);



    # Second, create a base class for all models.
    package Blog::TableObject;
    use base qw 'Class::Moco'; # Inherit Class::Moco



    # Third, create your models.
    package Blog::User;
    use base qw 'Blog::TableObject';

            entries => 'Blog::Entry',
            { key => 'user_id' }
            bookmarks => 'Blog::Bookmark',
            { key => 'user_id' }


    package Blog::Entry;
    use base qw 'Blog::TableObject';

            user => 'Blog::User',
            { key => 'user_id' }
            bookmarks => 'Blog::Bookmark',
            { key => 'entry_id' }


    package Blog::Bookmark;
    use base qw 'Blog::TableObject';

            user => 'Blog::User',
            { key => 'user_id' }
            entry => 'Blog::Entry',
            { key => 'entry_id' }


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

        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!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Hey, sounds interesting. Could you add documentation on the caching behavior (expiration, max size)? Is it possible to use other or mappers than Class::DBI?
    • You can use MoCo singularly because MoCo doesn't use Class::DBI.
      But APIs are similar to Class::DBI.

      MoCo::Cache is simple cache object but doesn't have complicated features such as max size etc..

      You can use Cache::* modules instead of MoCo::Cache.
      please try like this.

      # in your base model class.
      package MyClass;
      base qw(MoCo);
      use Cache::Memory;