- Timestamp:
- 10/07/08 15:48:46 (5 years ago)
- Location:
- lang/perl/Method-Cached/trunk
- Files:
-
- 1 added
- 8 modified
-
MANIFEST (modified) (1 diff)
-
lib/Method/Cached/Domain.pm (modified) (6 diffs)
-
lib/Method/Cached/Domain/Default.pm (modified) (1 diff)
-
lib/Method/Cached/KeyRegularizer.pm (modified) (1 diff)
-
lib/Method/Cached/KeyRule.pm (added)
-
lib/Method/Cached/MethodRegistry.pm (modified) (2 diffs)
-
lib/Method/Cached2.pm (modified) (7 diffs)
-
t/benchmark.pl (modified) (2 diffs)
-
t/benchmark2.pl (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Method-Cached/trunk/MANIFEST
r20848 r20895 14 14 lib/Method/Cached/Domain/Default.pm 15 15 lib/Method/Cached/KeyRegularizer.pm 16 lib/Method/Cached/KeyRule.pm 16 17 lib/Method/Cached/Manager.pm 17 18 lib/Method/Cached/MethodRegistry.pm -
lang/perl/Method-Cached/trunk/lib/Method/Cached/Domain.pm
r20615 r20895 14 14 15 15 __PACKAGE__->mk_classaccessor('storage'); 16 __PACKAGE__->mk_classaccessor('key_r egularizer' => 'LIST');16 __PACKAGE__->mk_classaccessor('key_rule' => 'LIST'); 17 17 18 18 sub new { … … 20 20 my %args = (0 < @_ && ref $_[0] eq 'HASH') ? %{ $_[0] } : @_; 21 21 my $self = bless {}, $class; 22 my $storage_class = $args{storage_class}|| croak 'storage_class is necessary';23 my $storage_args = $args{storage_args}|| undef;24 my $key_r egularizer = $args{key_regularizer}|| undef;22 my $storage_class = $args{storage_class} || croak 'storage_class is necessary'; 23 my $storage_args = $args{storage_args} || undef; 24 my $key_rule = $args{key_rule} || undef; 25 25 if (blessed $storage_class) { 26 26 $self->storage($storage_class); … … 35 35 $self->storage($storage_class->new(@{ $storage_args || [] })); 36 36 } 37 $self->key_r egularizer($key_regularizer) if $key_regularizer;37 $self->key_rule($key_rule) if $key_rule; 38 38 $self; 39 39 } … … 43 43 my $registry = Method::Cached::MethodRegistry::refer($name); 44 44 Method::Cached::KeyRegularizer->regularize( 45 $registry->{key_r egularizer} || $self->key_regularizer|| undef,45 $registry->{key_rule} || $self->key_rule || undef, 46 46 $name, 47 47 @args, … … 82 82 my $domain = Method::Cached::Domain->new( 83 83 # The supported interface is Cache::Cache and Cache::Memcached. 84 storage_class => 'Cache::Memcached::Fast',85 storage_args => [84 storage_class => 'Cache::Memcached::Fast', 85 storage_args => [ 86 86 { 87 87 servers => [qw/ 127.0.0.1:11211 /], … … 91 91 }, 92 92 ], 93 key_r egularizer=> undef,93 key_rule => undef, 94 94 ); 95 95 -
lang/perl/Method-Cached/trunk/lib/Method/Cached/Domain/Default.pm
r20615 r20895 7 7 8 8 my $_setting = { 9 storage_class => 'Cache::FastMmap',10 storage_args => [11 share_file => '/tmp/fastmmap -sharefile.bin',9 storage_class => 'Cache::FastMmap', 10 storage_args => [ 11 share_file => '/tmp/fastmmap.bin', 12 12 unlink_on_exit => 0, 13 13 ], 14 key_r egularizer=> 'LIST',14 key_rule => 'LIST', 15 15 }; 16 16 -
lang/perl/Method-Cached/trunk/lib/Method/Cached/KeyRegularizer.pm
r20638 r20895 12 12 my $_encoder; 13 13 14 # 'This regularizer was not supported or it did not operate normally :' .15 # " $regularizer - $@";16 17 14 sub regularize { 18 my ($class, $ regularizer, $method_name, @args) = @_;19 ref $ regularizer && return $regularizer->(@args);15 my ($class, $key_rule, $method_name, @args) = @_; 16 ref $key_rule && return $key_rule->(@args); 20 17 { 21 18 no strict 'refs'; 22 $ regularizer||= 'LIST';23 return $method_name . &{$ regularizer}(@args);19 $key_rule ||= 'LIST'; 20 return $method_name . &{$key_rule}(@args); 24 21 } 25 22 } -
lang/perl/Method-Cached/trunk/lib/Method/Cached/MethodRegistry.pm
r20848 r20895 4 4 use warnings; 5 5 use 5.008007; 6 use Method::Cached::KeyRegularizer;7 6 8 7 our %_registry; … … 17 16 # Arguments of attribute 18 17 # 19 my $domain_name = ($_[0] && $_[0] =~ /^-?\d+$/) ? q{} : shift;20 my $expires = shift || 0;21 my $key_r egularizer= shift || undef;18 my $domain_name = (defined $_[0] && $_[0] =~ /^-?\d+$/) ? q{} : shift; 19 my $expires = shift || 0; 20 my $key_rule = shift || undef; 22 21 $_registry{$name} = { 23 domain => $domain_name,24 expires => $expires,25 key_r egularizer => $key_regularizer,22 domain => $domain_name, 23 expires => $expires, 24 key_rule => $key_rule, 26 25 }; 27 26 } -
lang/perl/Method-Cached/trunk/lib/Method/Cached2.pm
r20848 r20895 6 6 use Attribute::Handlers; 7 7 use Carp qw/croak confess/; 8 use Digest::SHA qw/sha1_base64/;9 use JSON::XS;10 use Storable qw/freeze/;11 8 use UNIVERSAL::require; 9 use Method::Cached::KeyRule; 12 10 13 11 our $VERSION = '0.0103'; 14 12 15 my %_ domains;16 my $_ default_domain= {17 storage_class => 'Cache::FastMmap',18 storage_args => [19 share_file => '/tmp/fastmmap -sharefile.bin',13 my %_DOMAINS; 14 my $_DEFAULT_DOMAIN = { 15 storage_class => 'Cache::FastMmap', 16 storage_args => [ 17 share_file => '/tmp/fastmmap.bin', 20 18 unlink_on_exit => 0, 21 19 ], 22 key_r egularizer=> 'LIST',20 key_rule => 'LIST', 23 21 }; 24 22 … … 27 25 $options = [ $options || () ] unless ref $options eq 'ARRAY'; 28 26 my $name = $pkg . '::' . *{$symbol}{NAME}; 29 my ($domain_name, $expires, $ regularizer) = _parse_option(@{ $options });27 my ($domain_name, $expires, $key_rule) = _parse_option(@{ $options }); 30 28 no strict 'refs'; 31 29 no warnings 'redefine'; 32 30 *{$name} = sub { 33 my $domain = $_ domains{$domain_name}34 ? $_ domains{$domain_name}35 : $_ default_domain;36 $ regularizer ||= $domain->{key_regularizer};37 my $key = _regularize($regularizer, $name, @_);31 my $domain = $_DOMAINS{$domain_name} 32 ? $_DOMAINS{$domain_name} 33 : $_DEFAULT_DOMAIN; 34 $key_rule ||= $domain->{key_rule}; 35 my $key = Method::Cached::KeyRule::regularize($key_rule, $name, @_); 38 36 my $storage = _storage($domain); 39 37 my $ret = $storage->get($key); … … 53 51 } 54 52 if (exists $args{-default} && defined $args{-default}) { 55 my $d omain= $args{-default};56 ref $d omaineq 'HASH' or croak '-default option should be a hash reference';57 $class->default_domain($d omain);53 my $default = $args{-default}; 54 ref $default eq 'HASH' or croak '-default option should be a hash reference'; 55 $class->default_domain($default); 58 56 } 59 57 } … … 61 59 sub default_domain { 62 60 my $class = shift; 63 $_ default_domain= {64 %{ $_ default_domain},61 $_DEFAULT_DOMAIN = { 62 %{ $_DEFAULT_DOMAIN }, 65 63 %{ +shift }, 66 64 }; … … 70 68 my $class = shift; 71 69 while (my ($name, $args) = splice @_, 0, 2) { 72 $_domains{$name} = $args; 70 if (exists $_DOMAINS{$name}) { 71 warn 'This domain has already been defined: ' . $name; 72 next; 73 } 74 $_DOMAINS{$name} = $args; 73 75 } 74 76 } … … 77 79 my $domain_name = ($_[0] =~ /^-?\d+$/) ? q{} : shift; 78 80 my $expires = shift || 0; 79 my $regularizer = shift || undef; 80 return ($domain_name, $expires, $regularizer); 81 } 82 83 sub _regularize { 84 my ($regularizer, $method_name) = splice @_, 0, 2; 85 ref $regularizer && return $regularizer->($method_name, @_); 86 no strict 'refs'; 87 $regularizer ||= 'LIST'; 88 return $method_name . &{$regularizer}(@_); 81 my $key_rule = shift || undef; 82 return ($domain_name, $expires, $key_rule); 89 83 } 90 84 … … 98 92 } 99 93 100 sub LIST {101 my $method_name = shift;102 local $^W = 0;103 join chr(28), @_;104 }105 106 sub SERIALIZE {107 my $method_name = shift;108 local $^W = 0;109 our $ENCODER ||= JSON::XS->new->convert_blessed(1);110 *UNIVERSAL::TO_JSON = sub { freeze \@_ };111 my $json = $ENCODER->encode(\@_);112 undef *UNIVERSAL::TO_JSON;113 sha1_base64($json);114 }115 116 94 1; 117 95 -
lang/perl/Method-Cached/trunk/t/benchmark.pl
r20848 r20895 7 7 use Method::Cached::Manager 8 8 -default => { 9 storage_class => 'Cache::Memcached::Fast',10 storage_args => [9 storage_class => 'Cache::Memcached::Fast', 10 storage_args => [ 11 11 { servers => [qw/ 127.0.0.1:11211 /] }, 12 12 ], 13 key_r egularizer=> 'SERIALIZE', # SERIALIZE / LIST13 key_rule => 'SERIALIZE', # SERIALIZE / LIST 14 14 }, 15 15 -domains => { 16 16 'memcached-fast' => { 17 storage_class => 'Cache::Memcached::Fast',18 storage_args => [17 storage_class => 'Cache::Memcached::Fast', 18 storage_args => [ 19 19 { servers => [qw/ 127.0.0.1:11211 /] }, 20 20 ], 21 key_r egularizer=> 'SERIALIZE', # SERIALIZE / LIST21 key_rule => 'SERIALIZE', # SERIALIZE / LIST 22 22 }, 23 23 'fastmmap' => { 24 storage_class => 'Cache::FastMmap',25 storage_args => [24 storage_class => 'Cache::FastMmap', 25 storage_args => [ 26 26 share_file => '/tmp/fastmmap.bin', 27 27 unlink_on_exit => 0, 28 28 ], 29 key_r egularizer=> 'SERIALIZE', # SERIALIZE / LIST29 key_rule => 'SERIALIZE', # SERIALIZE / LIST 30 30 }, 31 31 }, … … 83 83 sub m_fib { $m_fib = $num; $m_fib = Dummy::fib_memoize($m_fib) } 84 84 85 cmpthese( 50000, {85 cmpthese(10000, { 86 86 'fib' => \&fib, 87 87 'C(default)' => \&def_fib, -
lang/perl/Method-Cached/trunk/t/benchmark2.pl
r20848 r20895 6 6 7 7 use Method::Cached::Manager -default => { 8 storage_class => 'Cache::Memcached::Fast',9 storage_args => [10 { servers => [qw/ 127.0.0.1:11211 /] },8 storage_class => 'Cache::Memcached::Fast', 9 storage_args => [ 10 { servers => [qw/ 127.0.0.1:11211 /] }, 11 11 ], 12 key_r egularizer => 'SERIALIZE', # SERIALIZE / LIST12 key_rule => 'LIST', # SERIALIZE / LIST 13 13 }; 14 14 15 15 use Method::Cached2 -default => { 16 storage_class => 'Cache::Memcached::Fast',17 storage_args => [18 { servers => [qw/ 127.0.0.1:11211 /] },16 storage_class => 'Cache::Memcached::Fast', 17 storage_args => [ 18 { servers => [qw/ 127.0.0.1:11211 /] }, 19 19 ], 20 key_r egularizer => 'SERIALIZE', # SERIALIZE / LIST20 key_rule => 'LIST', # SERIALIZE / LIST 21 21 }; 22 22 … … 59 59 my $num = 13; 60 60 61 my ($fib, $c_fib, $c2_fib, $ f_fib, $m_fib);61 my ($fib, $c_fib, $c2_fib, $m_fib); 62 62 63 63 sub fib { $fib = $num; $fib = Dummy::fib($fib) } … … 66 66 sub m_fib { $m_fib = $num; $m_fib = Dummy::fib_memoize($m_fib) } 67 67 68 cmpthese(1000 , {68 cmpthese(10000, { 69 69 'fib' => \&fib, 70 70 'Cached' => \&c_fib,
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)