From 95f1acf59d10eb63cf4990220a3e1de5615fb4a9 Mon Sep 17 00:00:00 2001 From: Jakub Narebski Date: Mon, 6 Dec 2010 00:01:08 +0100 Subject: [PATCH] gitweb/lib - No need for File::Temp when locking When using locking to ensure that only one process is generating data and updating cache, there is no need to use File::Temp for temporary file. This should improve performance. The _tempfile_to_path subroutine got promoted to _tempfile_to_path method, because we want to choose correct one dynamically, based on the type of object (polymorphism). Idea-inspired-by-code-by: John 'Warthog9' Hawley Signed-off-by: Jakub Narebski --- gitweb/lib/GitwebCache/FileCacheWithLocking.pm | 16 ++++++++++++++++ gitweb/lib/GitwebCache/SimpleFileCache.pm | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/gitweb/lib/GitwebCache/FileCacheWithLocking.pm b/gitweb/lib/GitwebCache/FileCacheWithLocking.pm index 1ea0e60304..4d8114d75f 100644 --- a/gitweb/lib/GitwebCache/FileCacheWithLocking.pm +++ b/gitweb/lib/GitwebCache/FileCacheWithLocking.pm @@ -46,6 +46,22 @@ sub get_lockname { return $lockfile; } +# ---------------------------------------------------------------------- +# "private" utility functions and methods + +# take a file path to cache entry, and its directory +# return filehandle and filename of open temporary file, +# like File::Temp::tempfile +sub _tempfile_to_path { + my ($self, $file, $dir) = @_; + + my $tempname = "$file.tmp"; + open my $temp_fh, '>', $tempname + or die "Couldn't open temporary file '$tempname' for writing: $!"; + + return ($temp_fh, $tempname); +} + # ...................................................................... # interface methods diff --git a/gitweb/lib/GitwebCache/SimpleFileCache.pm b/gitweb/lib/GitwebCache/SimpleFileCache.pm index 12af44f1cd..aeb91d4f64 100644 --- a/gitweb/lib/GitwebCache/SimpleFileCache.pm +++ b/gitweb/lib/GitwebCache/SimpleFileCache.pm @@ -288,7 +288,7 @@ sub write_fh { # return filehandle and filename of open temporary file, # like File::Temp::tempfile sub _tempfile_to_path { - my ($file, $dir) = @_; + my ($self, $file, $dir) = @_; # tempfile will croak() if there is an error return tempfile("${file}_XXXXX", @@ -324,7 +324,7 @@ sub store { } # generate a temporary file - my ($temp_fh, $tempname) = _tempfile_to_path($file, $dir); + 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': $!"; @@ -466,7 +466,7 @@ sub set_coderef_fh { } # generate a temporary file - my ($fh, $tempfile) = _tempfile_to_path($path, $dir); + my ($fh, $tempfile) = $self->_tempfile_to_path($path, $dir); # code writes to filehandle or file $code->($fh, $tempfile); -- 2.11.4.GIT