这些中最好的还是最差的方法之一?
利用范围:
my $cache = CHI->new( driver => 'File',expires_in => 3600 ); sub one { if ( my $data = $cache->get( 'key_one' ) ) { # ... } sub two { if ( my $data = $cache->get( 'key_two' ) ) { # ... }
传递对象作为参数:
my $cache = CHI->new( driver => 'File',expires_in => 3600 ); sub one { my ( $cache ) = @_; if ( my $data = $cache->get( 'key_one' ) ) { # ... } sub two { my ( $argument1,$cache ) = @_; if ( my $data = $cache->get( 'key_two' ) ) { # ... }
或在子程序中创建一个新实例:
sub one { my $cache = CHI->new( driver => 'File',expires_in => 3600 ); if ( my $data = $cache->get( 'key_one' ) ) { # ... } sub two { my $cache = CHI->new( driver => 'File',expires_in => 3600 ); if ( my $data = $cache->get( 'key_two' ) ) { # ... }