Babble::Cache - Caching infrastructure for Babble


Babble documentation Contained in the Babble distribution.

Index


Code Index:

NAME

Top

Babble::Cache - Caching infrastructure for Babble

DESCRIPTION

Top

This module implements the base Babble::Cache class, one that all other cache modules are based upon. It provides all the methods every module must support. Some of these will only carp - these need to be implemented in subclasses -, some won't have to be touched at all.

METHODS

Top

Babble::Cache provides the following methods:

Constructor

new (%params)

Creates a new Babble::Cache object. All parameters passed to it, will be saved for future use. However, the following parameters are recognised by Babble::Cache itself, and are used by the module itself:

-cache_fn

The filename of the cache. This is required for proper operation, provided one is using a cache that is loaded from file.

Methods that need subclass implementation

The following methods need to be implemented in subclasses, as they are not generic:

load ()

This just carps, as loading is not supported by the base class.

dump ()

This carps, as saving is not supported by the base class.

get ($category, $id[, $key])

This carps, therefore must be implemented by subclasses.

set ($category, $id, $key, $value)

This carps, therefore must be implemented by subclasses.

Generic methods

frob ($category, $id, $data [, $keys])

Frobnicate stuff in the cache. This is a quite complex method, which does a few interesting things. First, it looks up if an entry named $id exists under the $category in the cache. If it does, all the keys listed in the $keys arrayref will be copied over from the cache. If the cache does not have the key yet, it will be updated. If the entry is not found in the cache, the keys listed in $keys will be stored in it. If $keys is not defined, all keys of $data will be used.

update ($category, $id, $data, $key)

Update the cache with the values of $data when its $key key is defined. Otherwise, return the contents of the appropriate entry of the cache.

cache_fn ([$fn])

Get or set the cache file name.

AUTHOR

Top

Gergely Nagy, algernon@bonehunter.rulez.org

Bugs should be reported at http://bugs.bonehunter.rulez.org/babble.

SEE ALSO

Top

Babble, Babble::Cache::Class::Hash, Babble::Cache::Dumper, Babble::Cache::Storable


Babble documentation Contained in the Babble distribution.
## Babble/Cache.pm
## Copyright (C) 2004 Gergely Nagy <algernon@bonehunter.rulez.org>
##
## This file is part of Babble.
##
## Babble is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; version 2 dated June, 1991.
##
## Babble is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
## for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package Babble::Cache;

use strict;
use Carp;

sub new {
	my $type = shift;
	my %params = @_;
	my $self = {
		%params,

		cachedb => {},
	};

	bless $self, $type;
}

sub load () {
	my $self = shift;

	carp "$self does not support load";
}

sub dump () {
	my $self = shift;

	carp "$self does not support dump";
}

sub get ($$;$) {
	my $self = shift;

	carp "$self does not support get";
}

sub set ($$$$) {
	my $self = shift;

	carp "$self does not support set";
}

sub frob ($$$;$) {
	my ($self, $cat, $id, $data, $keys) = @_;
	@$keys = keys %$data unless $keys;

	if ($self->get ($cat, $id)) {
		foreach my $key (@$keys) {
			if (defined $self->get ($cat, $id, $key)) {
				$$data->{$key} = $self->get ($cat, $id, $key);
			} else {
				$self->set ($cat, $id, $key, $$data->{$key});
			}
		}
	} else {
		foreach my $key (@$keys) {
			$self->set ($cat, $id, $key, $$data->{$key});
		}
	}
}

sub update ($$$$) {
	my ($self, $cat, $id, $data, $key) = @_;

	if ($data->{$key}) {
		foreach my $dkey (keys %$data) {
			$self->set ($cat, $id, $dkey, $data->{$dkey});
		}
		return $data->{$key}
	} else {
		return $self->get ($cat, $id, $key);
	}
}

sub cache_fn (;$) {
	my ($self, $new_fn) = @_;

	$self->{-cache_fn} = $new_fn if $new_fn;
	return $self->{-cache_fn};
}

1;

# arch-tag: 0398d7e3-5725-4de8-ae74-fc3a277fb97d