Changeset 20615 for lang

Show
Ignore:
Timestamp:
10/03/08 18:52:44 (5 years ago)
Author:
bonnu
Message:

lang/perl/Method-Cached - content of beta(or alpha) was reviewed

Location:
lang/perl/Method-Cached/trunk
Files:
4 added
3 removed
6 modified
3 moved

Legend:

Unmodified
Added
Removed
  • lang/perl/Method-Cached/trunk/MANIFEST

    r20298 r20615  
    1111inc/Test/More.pm 
    1212lib/Method/Cached.pm 
    13 lib/Method/Cached/ArgsEncoder.pm 
    1413lib/Method/Cached/Domain.pm 
    1514lib/Method/Cached/Domain/Default.pm 
     15lib/Method/Cached/KeyRegularizer.pm 
    1616lib/Method/Cached/Manager.pm 
    17 lib/Method/Cached/StorageInspector.pm 
     17lib/Method/Cached/MethodRegistry.pm 
    1818Makefile.PL 
    1919MANIFEST                        This list of files 
     
    2121README 
    2222t/00-load.t 
    23 t/00_compile.t 
    2423t/01-basic.t 
    2524t/02-manager.t 
     25t/97-boilerplate.t 
     26t/98-perlcritic.t 
     27t/99-pod.t 
     28t/benchmark.pl 
  • lang/perl/Method-Cached/trunk/lib/Method/Cached.pm

    r20218 r20615  
    77use Method::Cached::Manager; 
    88 
    9 our $VERSION = '0.01'; 
     9our $VERSION = '0.0103'; 
    1010 
    1111sub UNIVERSAL::Cached :ATTR(CODE) { 
    1212    my ($pkg, $symbol, $code, $options) = @_[0 .. 2, 4]; 
    1313    $options = [ $options || () ] unless ref $options eq 'ARRAY'; 
    14     my $method_name = $pkg . '::' . *{$symbol}{NAME}; 
     14    my $name = $pkg . '::' . *{$symbol}{NAME}; 
    1515    { 
    1616        no strict 'refs'; 
    1717        no warnings 'redefine'; 
    18         my ($domain_name, $expire, $keygen) = @{ $options }; 
    19         Method::Cached::Manager->register_method($method_name, $domain_name); 
    20         *{$method_name} = sub { 
    21             my $domain = Method::Cached::Manager->get_domain($domain_name); 
    22             my $key    = $keygen && ref $keygen eq 'CODE' 
    23                 ? $keygen->($method_name, @_) 
    24                 : $domain->method_to_keygen->($method_name, @_); 
    25             my $ret    = $domain->storage->get($key); 
     18        my $domain_name = shift @{ $options }; 
     19        Method::Cached::Manager->register_method($name, $domain_name, @{ $options }); 
     20        *{$name} = sub { 
     21            my $domain = Method::Cached::Manager::_get_domain($domain_name); 
     22            my $key = $domain->regularize_key($name, @_); 
     23            my $ret = $domain->get($name, $key); 
    2624            return wantarray ? @{ $ret } : ${ $ret }[0] if $ret; 
    2725            my @ret = ($code->(@_)); 
    28             $domain->storage->set($key, \@ret, defined $expire ? $expire : ()); 
     26            $domain->set($name, $key, \@ret); 
    2927            return wantarray ? @ret : $ret[0]; 
    3028        }; 
     
    4644  use Method::Cached; 
    4745    
    48   sub message :Cached('domain', 180) { 
     46  sub message :Cached('domain', 180, KEY_SERIAL) { 
    4947      my ($class, $param) = @_; 
    5048      sprintf 'Hello world! param(%s) at %s', $param || q{}, time 
  • lang/perl/Method-Cached/trunk/lib/Method/Cached/Domain.pm

    r20196 r20615  
    55use 5.008007; 
    66use base qw/Class::Data::Accessor/; 
    7 use Carp qw/croak/; 
     7use Carp qw/croak confess/; 
    88use Scalar::Util qw/blessed/; 
    9 use Method::Cached::ArgsEncoder; 
    10 use Method::Cached::StorageInspector; 
     9use UNIVERSAL::require; 
     10use Method::Cached::KeyRegularizer; 
     11use Method::Cached::MethodRegistry; 
    1112 
    12 our $VERSION = '0.01'; 
     13our $VERSION = '0.0103'; 
    1314 
    1415__PACKAGE__->mk_classaccessor('storage'); 
    15 __PACKAGE__->mk_classaccessor('method_to_keygen' => \&__method_to_keygen); 
    16  
    17 sub __method_to_keygen { 
    18     my $name = shift; 
    19     my $hash = Method::Cached::ArgsEncoder::encode_args(@_); 
    20     $name . $hash; 
    21 } 
     16__PACKAGE__->mk_classaccessor('key_regularizer' => 'LIST'); 
    2217 
    2318sub new { 
     
    2520    my %args  = (0 < @_ && ref $_[0] eq 'HASH') ? %{ $_[0] } : @_; 
    2621    my $self  = bless {}, $class; 
    27     my $storage_class    = exists $args{storage_class}    ? delete $args{storage_class}    : undef; 
    28     my $storage_args     = exists $args{storage_args}     ? delete $args{storage_args}     : undef; 
    29     my $method_to_keygen = exists $args{method_to_keygen} ? delete $args{method_to_keygen} : undef; 
    30     if ($storage_class) { 
    31         if (blessed $storage_class) { 
    32             $self->storage($storage_class); 
     22    my $storage_class   = $args{storage_class}   || croak 'storage_class is necessary'; 
     23    my $storage_args    = $args{storage_args}    || undef; 
     24    my $key_regularizer = $args{key_regularizer} || undef; 
     25    if (blessed $storage_class) { 
     26        $self->storage($storage_class); 
     27    } 
     28    else { 
     29        unless (__inspect_storage_class($storage_class)) { 
     30            croak "storage_class needs the following methods: new, set, get, delete or remove"; 
    3331        } 
    34         else { 
    35             if ($storage_args && ref $storage_args ne 'ARRAY') { 
    36                 croak 'storage_args should be a array reference'; 
    37             } 
    38             unless (Method::Cached::StorageInspector->inspect($storage_class)) { 
    39                 croak Method::Cached::StorageInspector->message_necessary_methods; 
    40             } 
    41             $self->storage($storage_class->new(@{ $storage_args || [] })); 
     32        if ($storage_args && ref $storage_args ne 'ARRAY') { 
     33            croak 'storage_args should be a array reference'; 
    4234        } 
     35        $self->storage($storage_class->new(@{ $storage_args || [] })); 
    4336    } 
    44     if ($method_to_keygen && ref $method_to_keygen eq 'CODE') { 
    45         $self->method_to_keygen($method_to_keygen); 
    46     } 
     37    $self->key_regularizer($key_regularizer) if $key_regularizer; 
    4738    $self; 
     39} 
     40 
     41sub regularize_key { 
     42    my ($self, $name, @args) = @_; 
     43    my $registry = Method::Cached::MethodRegistry::refer($name); 
     44    Method::Cached::KeyRegularizer->regularize( 
     45        $registry->{key_regularizer} || $self->key_regularizer || undef,  
     46        $name, 
     47        @args, 
     48    ); 
     49} 
     50 
     51sub get { 
     52    my ($self, $name, $key) = @_; 
     53    $self->storage->get($key); 
     54} 
     55 
     56sub set { 
     57    my ($self, $name, $key, $val) = @_; 
     58    my $registry = Method::Cached::MethodRegistry::refer($name); 
     59    $self->storage->set($key, $val, $registry ? $registry->{expires} : ()); 
     60} 
     61 
     62sub __inspect_storage_class { 
     63    my $any_class = shift; 
     64    $any_class->require || confess "Can't load module: $any_class"; 
     65    $any_class->can($_) || return for qw/new set get/; 
     66    $any_class->can('delete') || $any_class->can('remove') || return; 
     67    return 1; 
    4868} 
    4969 
     
    6282  my $domain = Method::Cached::Domain->new( 
    6383      # The supported interface is Cache::Cache and Cache::Memcached.  
    64       storage_class    => 'Cache::Memcached::Fast', 
    65       storage_args     => [ 
     84      storage_class   => 'Cache::Memcached::Fast', 
     85      storage_args    => [ 
    6686          { 
    6787              servers         => [qw/ 127.0.0.1:11211 /], 
     
    7191          }, 
    7292      ], 
    73       method_to_keygen => undef, 
     93      key_regularizer => undef, 
    7494  ); 
    7595 
  • lang/perl/Method-Cached/trunk/lib/Method/Cached/Domain/Default.pm

    r20196 r20615  
    66use base qw/Method::Cached::Domain/; 
    77 
    8 __PACKAGE__->mk_classaccessor('default_setting' => { 
    9     storage_class    => 'Cache::FastMmap', 
    10     storage_args     => [ 
     8my $_setting = { 
     9    storage_class   => 'Cache::FastMmap', 
     10    storage_args    => [ 
    1111        share_file     => '/tmp/fastmmap-sharefile.bin', 
    1212        unlink_on_exit => 0, 
    1313    ], 
    14     method_to_keygen => undef, 
    15 }); 
     14    key_regularizer => 'LIST', 
     15}; 
     16 
     17sub setting { 1 < @_ ? ($_setting = $_[1]) : $_setting } 
    1618 
    1719sub new { 
    1820    my $class = shift; 
    1921    my %args  = (0 < @_ && ref $_[0] eq 'HASH') ? %{ $_[0] } : @_; 
    20     $class->SUPER::new(%{ $class->default_setting }, %args); 
     22    $class->SUPER::new(%{ $class->setting }, %args); 
    2123} 
    2224 
  • lang/perl/Method-Cached/trunk/lib/Method/Cached/Manager.pm

    r20226 r20615  
    44use warnings; 
    55use 5.008007; 
    6 use base qw/Class::Data::Accessor/; 
    76use Carp qw/croak/; 
    87use Scalar::Util qw/blessed/; 
    98use Method::Cached::Domain; 
    109use Method::Cached::Domain::Default; 
     10use Method::Cached::MethodRegistry; 
    1111 
    12 our $VERSION = '0.01'; 
     12our $VERSION = '0.0103'; 
    1313 
    14 __PACKAGE__->mk_classaccessor('domains' => {}); 
    15 __PACKAGE__->mk_classaccessor('map_to_domain' => {}); 
    16 __PACKAGE__->mk_classaccessor('default_domain'); 
     14my %_domains; 
     15my $_default_domain; 
    1716 
    1817sub import { 
     
    2524} 
    2625 
    27 sub get_domain { 
    28     my ($class, $name) = @_; 
    29     my $domain; 
    30     if (defined $name && length $name) { 
    31         $domain = $class->domains->{$name}; 
    32         return $domain if $domain; 
    33     } 
    34     $domain = $class->default_domain; 
    35     unless ($domain) { 
    36         require Method::Cached::Domain::Default; 
    37         $class->default_domain($domain = Method::Cached::Domain::Default->new); 
    38     } 
    39     $domain; 
    40 } 
    41  
    4226sub set_domain { 
    4327    my $class = shift; 
    4428    while (my ($name, $args) = splice @_, 0, 2) { 
    45         $class->domains->{$name} = 
    46             (blessed $args && $args->isa('Method::Cached::Domain')) 
    47                 ? $args 
    48                 : Method::Cached::Domain->new($args); 
     29        $_domains{$name} = (blessed $args && $args->isa('Method::Cached::Domain')) 
     30           ? $args 
     31           : Method::Cached::Domain->new($args); 
    4932    } 
     33} 
     34 
     35sub get_domain { 
     36    my ($class, $name) = @_; 
     37    _get_domain($name); 
     38} 
     39 
     40sub _get_domain { 
     41    defined $_[0] && $_domains{$_[0]} && return $_domains{$_[0]}; 
     42    $_default_domain ||= Method::Cached::Domain::Default->new; 
    5043} 
    5144 
    5245sub default_domain { 
    5346    my $class = shift; 
    54     if (0 < @_) { 
    55         $class->default_domain(Method::Cached::Domain::Default->new(@_)); 
    56     } 
    57     else { 
    58         Method::Cached::Domain->default_setting; 
    59     } 
     47    $_default_domain ||= Method::Cached::Domain::Default->new(@_); 
    6048} 
    6149 
    6250sub register_method { 
    63     my ($class, $method_name, $name) = @_; 
    64     $class->map_to_domain->{$method_name} = $name; 
    65 } 
    66  
    67 sub delete { 
     51    my $class = shift; 
     52    Method::Cached::MethodRegistry::register(@_); 
    6853} 
    6954 
  • lang/perl/Method-Cached/trunk/t/01-basic.t

    r20226 r20615  
    88    package Dummy::Basic; 
    99    use Method::Cached; 
    10     sub message : Cached { 
     10    sub message : Cached(undef, 0, LIST) { 
    1111        my ($args) = @_; 
    1212        sprintf 
    1313            'Nobody understands why to do such a thing. param(%s), rand(%s)', 
    14             $args || q{}, rand 
     14            $args || q{}, time 
    1515    } 
    1616 
     
    1818    use Method::Cached; 
    1919    sub new { bless {}, shift } 
    20     sub message : Cached { 
     20    sub message : Cached(undef, 0, STRUCTURE) { 
    2121        my ($self, $args) = @_; 
    2222        sprintf 
     
    2727} 
    2828 
    29 my $rand1 = Dummy::Basic::message(1); 
    30 my $rand2 = Dummy::Basic::message(1); 
     29my $rand1 = Dummy::Basic::message('abc', 1, {}); 
     30my $rand2 = Dummy::Basic::message('abc', 1, {}); 
    3131 
    3232ok $rand1 eq $rand2, 'message is same'; 
    3333 
    34 my $rand3 = Dummy::Basic::message(0); 
     34my $rand3 = Dummy::Basic::message('def', { word => 99 }); 
    3535 
    3636ok $rand1 ne $rand3, 'message is not same'; 
     
    3838my $obj   = Dummy::Object->new; 
    3939 
    40 my $rand4 = $obj->message('What is the meaning?'); 
    41 my $rand5 = $obj->message('What is the meaning?'); 
     40my $rand4 = $obj->message({ a => 1, b => 2 }); 
     41my $rand5 = $obj->message({ b => 2, a => 1 }); 
    4242 
    4343ok $rand4 eq $rand5, 'message is same';