Re: [RFC/PATCHv5 18/17] gitweb/lib - Add clear() and size() methods to caching interface

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 6 October 2010 23:02, Jakub Narebski <jnareb@xxxxxxxxx> wrote:
> Add ->size() method, which following Cache::Cache interface returns
> estimated total size of all entries in whole cache (in the namsepace
> assiciated with give cache instance). ÂNote that ->get_size($key)
> returns size of a single entry!
>
> Add ->clear() method, which removes all entries from the namespace
> associated with given cache instance. ÂFor safety it requires
> namespace to be set to true value, which means that it cannot be
> empty; therefore default namespace is changed to 'gitweb'.
>
> The ->clear() method should be fairly safe, because it first renames
> directory (which should be atomic), and only then removes it
> (following code from CGI::Driver::File).
>
> Signed-off-by: Jakub Narebski <jnareb@xxxxxxxxx>
> ---
> The only difference from previous version is that a comment about some
> method was moved to earlier commit that introduced it, where it
> belongs.
>
> This patch (implementing "backend") together with next patch
> (implementing "interface") are meant to provide nice web interface to
> safely cleaning (pruning) cache, as described in comments and
> documentation (how to do this manually).
>
> Âgitweb/lib/GitwebCache/SimpleFileCache.pm | Â 48 ++++++++++++++++++++++++++--
> Ât/t9503/test_cache_interface.pl      |  Â1 -
> Â2 files changed, 44 insertions(+), 5 deletions(-)
>
> diff --git a/gitweb/lib/GitwebCache/SimpleFileCache.pm b/gitweb/lib/GitwebCache/SimpleFileCache.pm
> index 2a3d9cf..8f172cb 100644
> --- a/gitweb/lib/GitwebCache/SimpleFileCache.pm
> +++ b/gitweb/lib/GitwebCache/SimpleFileCache.pm
> @@ -20,8 +20,9 @@ package GitwebCache::SimpleFileCache;
> Âuse strict;
> Âuse warnings;
>
> -use File::Path qw(mkpath);
> -use File::Temp qw(tempfile);
> +use File::Path qw(mkpath rmtree);
> +use File::Temp qw(tempfile mktemp);
> +use File::Find qw(find);
> Âuse Digest::MD5 qw(md5_hex);
>
> Â# by default, the cache nests all entries on the filesystem single
> @@ -37,7 +38,7 @@ our $DEFAULT_CACHE_ROOT = "cache";
> Â# by default we don't use cache namespace (empty namespace);
> Â# empty namespace does not allow for simple implementation of clear() method.
> Â#
> -our $DEFAULT_NAMESPACE = '';
> +our $DEFAULT_NAMESPACE = "gitweb";
>
> Â# ......................................................................
> Â# constructor
> @@ -334,7 +335,7 @@ sub get_size {
> Â}
>
> Â# ......................................................................
> -# interface methods
> +# interface methods dealing with single item
>
> Â# Removing and expiring
>
> @@ -427,6 +428,45 @@ sub compute {
> Â Â Â Âreturn $data;
> Â}
>
> +# ......................................................................
> +# interface methods dealing with whole namespace
> +
> +# $cache->clear();
> +#
> +# Remove all entries from the namespace.
> +# Namespace must be defined and not empty.
> +sub clear {
> + Â Â Â my $self = shift;
> +
> + Â Â Â return unless $self->get_namespace();
> +
> + Â Â Â my $namespace_dir = $self->path_to_namespace();
> + Â Â Â return if !-d $namespace_dir;
> +
> + Â Â Â my $renamed_dir = mktemp($namespace_dir . '.XXXX');
> + Â Â Â rename($namespace_dir, $renamed_dir);
> + Â Â Â rmtree($renamed_dir);
> + Â Â Â die "Couldn't remove '$renamed_dir' directory"
> + Â Â Â Â Â Â Â if -d $renamed_dir;
> +}
> +
> +# $size = $cache->size();
> +#
> +# Size of whole names (or whole cache if namespace empty)
> +sub size {
> + Â Â Â my $self = shift;
> +
> + Â Â Â my $namespace_dir = $self->path_to_namespace();
> + Â Â Â return if !-d $namespace_dir;
> +
> + Â Â Â my $total_size = 0;
> + Â Â Â my $add_size = sub { $total_size += -s $_ };
> +
> + Â Â Â File::Find::find({ wanted => $add_size, no_chdir => 1 }, $namespace_dir);
> +
> + Â Â Â return $total_size;
> +}
> +
> Â1;

This looks good to me.

Acked-by:  Thomas Adam <thomas@xxxxxxxxxx>

-- Thomas Adam
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]