From: Thomas Adam <thomas@xteddy.org>
To: Jakub Narebski <jnareb@gmail.com>
Cc: git@vger.kernel.org,
"John 'Warthog9' Hawley" <warthog9@kernel.org>,
Petr Baudis <pasky@ucw.cz>,
admin@repo.or.cz
Subject: Re: [PATCHv5 03/17] gitweb/lib - Very simple file based cache
Date: Wed, 6 Oct 2010 23:41:11 +0100 [thread overview]
Message-ID: <AANLkTinPCOfknoN4aO_EdPwKorRiM6NU6ep0z_nnNug8@mail.gmail.com> (raw)
In-Reply-To: <1286402526-13143-4-git-send-email-jnareb@gmail.com>
On 6 October 2010 23:01, Jakub Narebski <jnareb@gmail.com> wrote:
> +# creates get_depth() and set_depth($depth) etc. methods
> +foreach my $i (qw(depth root namespace)) {
> + my $field = $i;
> + no strict 'refs';
For each item, you'll set "no strict refs"? This might be better off
outside the loop. It's still scoped appropriately inside the
subroutine.
> + my $file = $self->path_to_key($key);
> + return undef unless (defined $file && -f $file);
PBP (Perl Best Practises) will tell you that explicitly returning
undef is discouraged -- "undef" should be reserved for those errors
you cannot handle, not ones you don't want to.
> +
> + # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed
> + # via CHI::Driver::File (from CHI-0.33)
> + my $buf = '';
> + open my $read_fh, '<', $file
> + or return undef;
Ditto.
> + binmode $read_fh, ':raw';
> +
> + my $size_left = -s $read_fh;
> +
> + while ($size_left > 0) {
> + my $read_cnt = sysread($read_fh, $buf, $size_left, length($buf));
> + return undef unless defined $read_cnt;
Ditto.
> + last if $read_cnt == 0;
> + $size_left -= $read_cnt;
> + #last if $size_left <= 0;
> + }
> +
> + close $read_fh
> + or die "Couldn't close file '$file' opened for reading: $!";
> + return $buf;
> +}
"use Carp;" would be more useful here, and hence croak() and confess().
> +sub store {
> + my ($self, $key, $data) = @_;
> +
> + my $dir;
> + my $file = $self->path_to_key($key, \$dir);
> + return undef unless (defined $file && defined $dir);
See above.
> + # ensure that directory leading to cache file exists
> + if (!-d $dir) {
> + eval { mkpath($dir, 0, 0777); 1 }
> + or die "Couldn't create leading directory '$dir' (mkpath): $!";
> + }
Why is this eval()ed? It will still return false and set $! appropriately.
> + # generate a temporary file
> + my ($temp_fh, $tempname) = $self->_tempfile_to_path($file, $dir);
> + chmod 0666, $tempname
> + or warn "Couldn't change permissions to 0666 / -rw-rw-rw- for '$tempname': $!";
> +
> + # Fast spew, adapted from File::Slurp::write, with unnecessary options removed
> + # via CHI::Driver::File (from CHI-0.33)
> + my $write_fh = $temp_fh;
> + binmode $write_fh, ':raw';
> +
> + my $size_left = length($data);
> + my $offset = 0;
> +
> + while ($size_left > 0) {
> + my $write_cnt = syswrite($write_fh, $data, $size_left, $offset);
> + return undef unless defined $write_cnt;
Again, with the undef.
> + $size_left -= $write_cnt;
> + $offset += $write_cnt; # == length($data);
> + }
> +
> + close $temp_fh
> + or die "Couldn't close temporary file '$tempname' opened for writing: $!";
> + rename($tempname, $file)
> + or die "Couldn't rename temporary file '$tempname' to '$file': $!";
> +}
> +
> +# get size of an element associated with the $key (not the size of whole cache)
> +sub get_size {
> + my ($self, $key) = @_;
> +
> + my $path = $self->path_to_key($key)
> + or return undef;
Again with the undef
> + if (-f $path) {
> + return -s $path;
> + }
> + return 0;
> +}
> +
> +# ......................................................................
> +# interface methods
> +
> +# Removing and expiring
> +
> +# $cache->remove($key)
> +#
> +# Remove the data associated with the $key from the cache.
> +sub remove {
> + my ($self, $key) = @_;
> +
> + my $file = $self->path_to_key($key)
> + or return undef;
> + return undef unless -f $file;
Again with the undef.
> + unlink($file)
> + or die "Couldn't remove file '$file': $!";
> +}
> +
> +# Getting and setting
> +
> +# $cache->set($key, $data);
> +#
> +# Associates $data with $key in the cache, overwriting any existing entry.
> +# Returns $data.
> +sub set {
> + my ($self, $key, $data) = @_;
> +
> + return unless (defined $key && defined $data);
return what?
> + $self->store($key, $data);
> +
> + return $data;
> +}
> +
> +# $data = $cache->get($key);
> +#
> +# Returns the data associated with $key. If $key does not exist
> +# or has expired, returns undef.
> +sub get {
> + my ($self, $key) = @_;
> +
> + my $data = $self->fetch($key)
> + or return undef;
> +
> + return $data;
> +}
> +
> +# $data = $cache->compute($key, $code);
> +#
> +# Combines the get and set operations in a single call. Attempts to
> +# get $key; if successful, returns the value. Otherwise, calls $code
> +# and uses the return value as the new value for $key, which is then
> +# returned.
> +sub compute {
> + my ($self, $key, $code) = @_;
> +
> + my $data = $self->get($key);
> + if (!defined $data) {
> + $data = $code->($self, $key);
> + $self->set($key, $data);
> + }
Can you guarantee $code here?
unless( defined $code and ref $code eq 'CODE' )
{
....
}
Wouldn't it be easier to eval{} this and checj $@?
[...]
-- Thomas Adam
next prev parent reply other threads:[~2010-10-06 22:41 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-06 22:01 [PATCHv5 00/17] gitweb: Simple file based output caching Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 01/17] t/test-lib.sh: Export also GIT_BUILD_DIR in test_external Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 02/17] gitweb: Prepare for splitting gitweb Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 03/17] gitweb/lib - Very simple file based cache Jakub Narebski
2010-10-06 22:41 ` Thomas Adam [this message]
2010-10-06 22:44 ` Ævar Arnfjörð Bjarmason
2010-10-06 22:46 ` Thomas Adam
2010-10-06 22:47 ` Ævar Arnfjörð Bjarmason
2010-10-06 23:00 ` Jakub Narebski
2010-10-06 23:12 ` Thomas Adam
2010-10-06 23:32 ` Jakub Narebski
2010-10-06 22:57 ` Ævar Arnfjörð Bjarmason
2010-10-06 23:46 ` Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 04/17] gitweb/lib - Stat-based cache expiration Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 05/17] gitweb/lib - Regenerate entry if the cache file has size of 0 Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 06/17] gitweb/lib - Simple select(FH) based output capture Jakub Narebski
2010-10-06 22:52 ` Thomas Adam
2010-10-06 23:22 ` Jakub Narebski
2010-10-06 23:03 ` Ævar Arnfjörð Bjarmason
2010-10-06 23:26 ` Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 07/17] gitweb/lib - Cache captured output (using get/set) Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 08/17] gitweb: Add optional output caching Jakub Narebski
2010-10-06 22:46 ` Ævar Arnfjörð Bjarmason
2010-10-06 23:06 ` Jakub Narebski
2010-10-06 23:16 ` Ævar Arnfjörð Bjarmason
2010-10-06 22:01 ` [PATCHv5 09/17] gitweb/lib - Adaptive cache expiration time Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 10/21] gitweb/lib - Use CHI compatibile (compute method) caching interface Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 11/17] gitweb/lib - Use locking to avoid 'cache miss stampede' problem Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 12/17] gitweb/lib - No need for File::Temp when locking Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 13/17] gitweb/lib - Serve stale data when waiting for filling cache Jakub Narebski
2010-10-06 22:01 ` [PATCHv5 14/17] gitweb/lib - Regenerate (refresh) cache in background Jakub Narebski
2010-10-06 22:02 ` [PATCHv5 15/17] gitweb: Introduce %actions_info, gathering information about actions Jakub Narebski
2010-10-06 22:02 ` [PATCHv5/RFC 16/17] gitweb: Show appropriate "Generating..." page when regenerating cache Jakub Narebski
2010-10-06 22:02 ` [PATCHv5/RFC 17/17] gitweb: Add startup delay to activity indicator for cache Jakub Narebski
2010-10-06 22:02 ` [RFC/PATCHv5 18/17] gitweb/lib - Add clear() and size() methods to caching interface Jakub Narebski
2010-10-06 22:56 ` Thomas Adam
2010-10-06 22:02 ` [RFC PATCHv5 19/17] gitweb: Add beginnings of cache administration page Jakub Narebski
2010-10-06 22:02 ` [PoC PATCHv5 20/17] gitweb/lib - Benchmarking GitwebCache::SimpleFileCache (in t/9603/) Jakub Narebski
2010-10-06 22:02 ` [PoC PATCHv5 21/17] gitweb/lib - Alternate ways of capturing output Jakub Narebski
2010-10-10 20:32 ` [RFD] Possible improvements for output caching in gitweb Jakub Narebski
2010-10-24 21:34 ` [PATCHv5 00/17] gitweb: Simple file based output caching J.H.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=AANLkTinPCOfknoN4aO_EdPwKorRiM6NU6ep0z_nnNug8@mail.gmail.com \
--to=thomas@xteddy.org \
--cc=admin@repo.or.cz \
--cc=git@vger.kernel.org \
--cc=jnareb@gmail.com \
--cc=pasky@ucw.cz \
--cc=warthog9@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).