git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCHv5 03/17] gitweb/lib - Very simple file based cache

From
Jakub Narebski <jnareb@gmail.com>
Date
Oct 6, 2010, 23:00 UTC
Message-ID
<201010070100.33478.jnareb@gmail.com>
In-Reply-To
<AANLkTinPCOfknoN4aO_EdPwKorRiM6NU6ep0z_nnNug8@mail.gmail.com>
Thank you very much for those comments on code.
On Thu, 7 Oct 2010, Thomas Adam wrote:
> On 6 October 2010 23:01, Jakub Narebski <jnareb@gmail.com> wrote:
Show 8 quoted lines
> > +# 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.

On the other hand this way scope where "no strict 'refs';" is active is limited... but I guess having "no strict 'refs';" outside loop would be better.

Show 7 quoted lines
> 
> > +       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.

Well, Perl Best Practices are practices; sometimes there is good reason to not take them into account (though probably not in this case).

Explicitly returning undef is discouraged because in list context the returned undef (or rather 1-element list with 'undef' as sole element) is true-ish.

On the other hand
Show 10 quoted lines
> > +# $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?
 
as you can see 'return' statement with no value looks rather cryptic
with statement modifier (conditional).

I should really have run gitweb, caching modules and tests through perlcritic...

Show 11 quoted lines
> > +               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().
For a web application we usually do not want to have too detailed error
message present to client (to web browser) to avoid leaking of sensitive
information.
 
Show 7 quoted lines
> > +       # 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.
IIRC mkpath *dies on error*, rather than returning false.  For better
error handling we would need to use make_path, but File::Path 2.0+
is in [stable] core only since Perl 5.10.
 
[...]
Show 16 quoted lines
> > +# $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?

I don't want to code too defensively, but perhaps check for this is in order... though what we should do if $code is not code reference?

> 
> unless( defined $code and ref $code eq 'CODE' )

"ref($code) eq 'CODE'" would be enough; 'undef' is not reference, and ref(undef) returns "".

Show 5 quoted lines
> {
>         ....
> }
> 
> Wouldn't it be easier to eval{} this and check $@?
So the answer is no.
Thanks again for your comments.
-- 
Jakub Narebski
Poland
Previous: Ævar Arnfjörð BjarmasonNext: Thomas Adam
Message 9 of 41 in “[PATCHv5 00/17] gitweb: Simple file based output caching”
  1. Jakub NarebskiOct 6, 2010
  2. 01/17 t/test-lib.sh: Export also GIT_BUILD_DIR in test_externalJakub Narebski, Oct 6, 2010
  3. 02/17 gitweb: Prepare for splitting gitwebJakub Narebski, Oct 6, 2010
  4. 03/17 gitweb/lib - Very simple file based cacheJakub Narebski, Oct 6, 2010
  5. Thomas AdamOct 6, 2010
  6. Ævar Arnfjörð BjarmasonOct 6, 2010
  7. Thomas AdamOct 6, 2010
  8. Ævar Arnfjörð BjarmasonOct 6, 2010
  9. Jakub NarebskiOct 6, 2010
  10. Thomas AdamOct 6, 2010
  11. Jakub NarebskiOct 6, 2010
  12. Ævar Arnfjörð BjarmasonOct 6, 2010
  13. Jakub NarebskiOct 6, 2010
  14. 04/17 gitweb/lib - Stat-based cache expirationJakub Narebski, Oct 6, 2010
  15. 05/17 gitweb/lib - Regenerate entry if the cache file has size of 0Jakub Narebski, Oct 6, 2010
  16. 06/17 gitweb/lib - Simple select(FH) based output captureJakub Narebski, Oct 6, 2010
  17. Thomas AdamOct 6, 2010
  18. Jakub NarebskiOct 6, 2010
  19. Ævar Arnfjörð BjarmasonOct 6, 2010
  20. Jakub NarebskiOct 6, 2010
  21. 07/17 gitweb/lib - Cache captured output (using get/set)Jakub Narebski, Oct 6, 2010
  22. 08/17 gitweb: Add optional output cachingJakub Narebski, Oct 6, 2010
  23. Ævar Arnfjörð BjarmasonOct 6, 2010
  24. Jakub NarebskiOct 6, 2010
  25. Ævar Arnfjörð BjarmasonOct 6, 2010
  26. 09/17 gitweb/lib - Adaptive cache expiration timeJakub Narebski, Oct 6, 2010
  27. 10/21 gitweb/lib - Use CHI compatibile (compute method) caching interfaceJakub Narebski, Oct 6, 2010
  28. 11/17 gitweb/lib - Use locking to avoid 'cache miss stampede' problemJakub Narebski, Oct 6, 2010
  29. 12/17 gitweb/lib - No need for File::Temp when lockingJakub Narebski, Oct 6, 2010
  30. 13/17 gitweb/lib - Serve stale data when waiting for filling cacheJakub Narebski, Oct 6, 2010
  31. 14/17 gitweb/lib - Regenerate (refresh) cache in backgroundJakub Narebski, Oct 6, 2010
  32. 15/17 gitweb: Introduce %actions_info, gathering information about actionsJakub Narebski, Oct 6, 2010
  33. 16/17 gitweb: Show appropriate "Generating..." page when regenerating cacheJakub Narebski, Oct 6, 2010
  34. 17/17 gitweb: Add startup delay to activity indicator for cacheJakub Narebski, Oct 6, 2010
  35. 18/17 gitweb/lib - Add clear() and size() methods to caching interfaceJakub Narebski, Oct 6, 2010
  36. Thomas AdamOct 6, 2010
  37. 19/17 gitweb: Add beginnings of cache administration pageJakub Narebski, Oct 6, 2010
  38. 20/17 gitweb/lib - Benchmarking GitwebCache::SimpleFileCache (in t/9603/)Jakub Narebski, Oct 6, 2010
  39. 21/17 gitweb/lib - Alternate ways of capturing outputJakub Narebski, Oct 6, 2010
  40. [RFD] Possible improvements for output caching in gitwebJakub Narebski, Oct 10, 2010
  41. J.H.Oct 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.