- Timestamp:
- 10/03/08 18:52:44 (5 years ago)
- Location:
- lang/perl/Method-Cached/trunk
- Files:
-
- 4 added
- 3 removed
- 6 modified
- 3 moved
-
MANIFEST (modified) (2 diffs)
-
lib/Method/Cached.pm (modified) (2 diffs)
-
lib/Method/Cached/ArgsEncoder.pm (deleted)
-
lib/Method/Cached/Domain.pm (modified) (4 diffs)
-
lib/Method/Cached/Domain/Default.pm (modified) (1 diff)
-
lib/Method/Cached/KeyRegularizer.pm (added)
-
lib/Method/Cached/Manager.pm (modified) (2 diffs)
-
lib/Method/Cached/MethodRegistry.pm (added)
-
lib/Method/Cached/StorageInspector.pm (deleted)
-
t/00_compile.t (deleted)
-
t/01-basic.t (modified) (4 diffs)
-
t/03-key_regularizer (added)
-
t/97-boilerplate.t (moved) (moved from lang/perl/Method-Cached/trunk/t/97_boilerplate.t)
-
t/98-perlcritic.t (moved) (moved from lang/perl/Method-Cached/trunk/t/98_perlcritic.t)
-
t/99-pod.t (moved) (moved from lang/perl/Method-Cached/trunk/t/99_pod.t)
-
t/benchmark.pl (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Method-Cached/trunk/MANIFEST
r20298 r20615 11 11 inc/Test/More.pm 12 12 lib/Method/Cached.pm 13 lib/Method/Cached/ArgsEncoder.pm14 13 lib/Method/Cached/Domain.pm 15 14 lib/Method/Cached/Domain/Default.pm 15 lib/Method/Cached/KeyRegularizer.pm 16 16 lib/Method/Cached/Manager.pm 17 lib/Method/Cached/ StorageInspector.pm17 lib/Method/Cached/MethodRegistry.pm 18 18 Makefile.PL 19 19 MANIFEST This list of files … … 21 21 README 22 22 t/00-load.t 23 t/00_compile.t24 23 t/01-basic.t 25 24 t/02-manager.t 25 t/97-boilerplate.t 26 t/98-perlcritic.t 27 t/99-pod.t 28 t/benchmark.pl -
lang/perl/Method-Cached/trunk/lib/Method/Cached.pm
r20218 r20615 7 7 use Method::Cached::Manager; 8 8 9 our $VERSION = '0.01 ';9 our $VERSION = '0.0103'; 10 10 11 11 sub UNIVERSAL::Cached :ATTR(CODE) { 12 12 my ($pkg, $symbol, $code, $options) = @_[0 .. 2, 4]; 13 13 $options = [ $options || () ] unless ref $options eq 'ARRAY'; 14 my $ method_name = $pkg . '::' . *{$symbol}{NAME};14 my $name = $pkg . '::' . *{$symbol}{NAME}; 15 15 { 16 16 no strict 'refs'; 17 17 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); 26 24 return wantarray ? @{ $ret } : ${ $ret }[0] if $ret; 27 25 my @ret = ($code->(@_)); 28 $domain->s torage->set($key, \@ret, defined $expire ? $expire : ());26 $domain->set($name, $key, \@ret); 29 27 return wantarray ? @ret : $ret[0]; 30 28 }; … … 46 44 use Method::Cached; 47 45 48 sub message :Cached('domain', 180 ) {46 sub message :Cached('domain', 180, KEY_SERIAL) { 49 47 my ($class, $param) = @_; 50 48 sprintf 'Hello world! param(%s) at %s', $param || q{}, time -
lang/perl/Method-Cached/trunk/lib/Method/Cached/Domain.pm
r20196 r20615 5 5 use 5.008007; 6 6 use base qw/Class::Data::Accessor/; 7 use Carp qw/croak /;7 use Carp qw/croak confess/; 8 8 use Scalar::Util qw/blessed/; 9 use Method::Cached::ArgsEncoder; 10 use Method::Cached::StorageInspector; 9 use UNIVERSAL::require; 10 use Method::Cached::KeyRegularizer; 11 use Method::Cached::MethodRegistry; 11 12 12 our $VERSION = '0.01 ';13 our $VERSION = '0.0103'; 13 14 14 15 __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'); 22 17 23 18 sub new { … … 25 20 my %args = (0 < @_ && ref $_[0] eq 'HASH') ? %{ $_[0] } : @_; 26 21 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"; 33 31 } 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'; 42 34 } 35 $self->storage($storage_class->new(@{ $storage_args || [] })); 43 36 } 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; 47 38 $self; 39 } 40 41 sub 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 51 sub get { 52 my ($self, $name, $key) = @_; 53 $self->storage->get($key); 54 } 55 56 sub 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 62 sub __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; 48 68 } 49 69 … … 62 82 my $domain = Method::Cached::Domain->new( 63 83 # 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 => [ 66 86 { 67 87 servers => [qw/ 127.0.0.1:11211 /], … … 71 91 }, 72 92 ], 73 method_to_keygen=> undef,93 key_regularizer => undef, 74 94 ); 75 95 -
lang/perl/Method-Cached/trunk/lib/Method/Cached/Domain/Default.pm
r20196 r20615 6 6 use base qw/Method::Cached::Domain/; 7 7 8 __PACKAGE__->mk_classaccessor('default_setting' =>{9 storage_class => 'Cache::FastMmap',10 storage_args => [8 my $_setting = { 9 storage_class => 'Cache::FastMmap', 10 storage_args => [ 11 11 share_file => '/tmp/fastmmap-sharefile.bin', 12 12 unlink_on_exit => 0, 13 13 ], 14 method_to_keygen => undef, 15 }); 14 key_regularizer => 'LIST', 15 }; 16 17 sub setting { 1 < @_ ? ($_setting = $_[1]) : $_setting } 16 18 17 19 sub new { 18 20 my $class = shift; 19 21 my %args = (0 < @_ && ref $_[0] eq 'HASH') ? %{ $_[0] } : @_; 20 $class->SUPER::new(%{ $class-> default_setting }, %args);22 $class->SUPER::new(%{ $class->setting }, %args); 21 23 } 22 24 -
lang/perl/Method-Cached/trunk/lib/Method/Cached/Manager.pm
r20226 r20615 4 4 use warnings; 5 5 use 5.008007; 6 use base qw/Class::Data::Accessor/;7 6 use Carp qw/croak/; 8 7 use Scalar::Util qw/blessed/; 9 8 use Method::Cached::Domain; 10 9 use Method::Cached::Domain::Default; 10 use Method::Cached::MethodRegistry; 11 11 12 our $VERSION = '0.01 ';12 our $VERSION = '0.0103'; 13 13 14 __PACKAGE__->mk_classaccessor('domains' => {}); 15 __PACKAGE__->mk_classaccessor('map_to_domain' => {}); 16 __PACKAGE__->mk_classaccessor('default_domain'); 14 my %_domains; 15 my $_default_domain; 17 16 18 17 sub import { … … 25 24 } 26 25 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 42 26 sub set_domain { 43 27 my $class = shift; 44 28 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); 49 32 } 33 } 34 35 sub get_domain { 36 my ($class, $name) = @_; 37 _get_domain($name); 38 } 39 40 sub _get_domain { 41 defined $_[0] && $_domains{$_[0]} && return $_domains{$_[0]}; 42 $_default_domain ||= Method::Cached::Domain::Default->new; 50 43 } 51 44 52 45 sub default_domain { 53 46 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(@_); 60 48 } 61 49 62 50 sub 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(@_); 68 53 } 69 54 -
lang/perl/Method-Cached/trunk/t/01-basic.t
r20226 r20615 8 8 package Dummy::Basic; 9 9 use Method::Cached; 10 sub message : Cached {10 sub message : Cached(undef, 0, LIST) { 11 11 my ($args) = @_; 12 12 sprintf 13 13 'Nobody understands why to do such a thing. param(%s), rand(%s)', 14 $args || q{}, rand14 $args || q{}, time 15 15 } 16 16 … … 18 18 use Method::Cached; 19 19 sub new { bless {}, shift } 20 sub message : Cached {20 sub message : Cached(undef, 0, STRUCTURE) { 21 21 my ($self, $args) = @_; 22 22 sprintf … … 27 27 } 28 28 29 my $rand1 = Dummy::Basic::message( 1);30 my $rand2 = Dummy::Basic::message( 1);29 my $rand1 = Dummy::Basic::message('abc', 1, {}); 30 my $rand2 = Dummy::Basic::message('abc', 1, {}); 31 31 32 32 ok $rand1 eq $rand2, 'message is same'; 33 33 34 my $rand3 = Dummy::Basic::message( 0);34 my $rand3 = Dummy::Basic::message('def', { word => 99 }); 35 35 36 36 ok $rand1 ne $rand3, 'message is not same'; … … 38 38 my $obj = Dummy::Object->new; 39 39 40 my $rand4 = $obj->message( 'What is the meaning?');41 my $rand5 = $obj->message( 'What is the meaning?');40 my $rand4 = $obj->message({ a => 1, b => 2 }); 41 my $rand5 = $obj->message({ b => 2, a => 1 }); 42 42 43 43 ok $rand4 eq $rand5, 'message is same';
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)