YAML::LoadFileCached - A wrapper around YAML::LoadFile with caching


YAML-LoadFileCached documentation Contained in the YAML-LoadFileCached distribution.

Index


Code Index:

NAME

Top

YAML::LoadFileCached - A wrapper around YAML::LoadFile with caching capabilities.

SYNOPSIS

Top

  use YAML::LoadFileCached;
  use Data::Dumper;

  my $data = LoadFileCached('data.yaml');

  print Dumper($data);

DESCRIPTION

Top

This module provides a way to gain speed improvements when you have to repeatedly read a file in YAML format (eg. configuration files) under mod_perl or in a long running process, although at the cost of memory expense.

The by default exported function LoadFileCached caches the results from YAML::LoadFile and simply returns them if called repeatedly.

If the requested file has been changed since the last request, LoadFileCached will reread it.

FUNCTIONS

Top

LoadFileCached (filepath)

see DISCRIPTION.

CacheStatistics ([filepath])

this by default not exported function returns statistics for the cache used by LoadFileCached. If given filepath it returns a Hashref with the keys:

lastchanged

the last modify time in seconds since the epoch (as retourned by stat).

read

number of calls to YAML::LoadFile.

cached

number of cache-served requests.

If called with no argument, CacheStatistics returns a Hash of Hashrefs with filepath as key in the first level.

AUTHOR

Top

Florian Helmberger, <fh@laudatio.com>

SEE ALSO

Top

YAML.

VERSION

Top

$Id: LoadFileCached.pm,v 1.3 2003/02/03 12:10:01 florian Exp $

COPYRIGHT

Top


YAML-LoadFileCached documentation Contained in the YAML-LoadFileCached distribution.

package YAML::LoadFileCached;

#
# $Id: LoadFileCached.pm,v 1.3 2003/02/03 12:10:01 florian Exp $
#

use 5.006;
use strict;
use warnings;

use YAML qw(LoadFile);

require Exporter;

our @ISA	= qw(Exporter);
our @EXPORT_OK	= qw(
	CacheStatistics
	);
our @EXPORT	= qw(
	LoadFileCached
	);
our $VERSION	= '0.21';

my $cache;
my $statistic;

sub LoadFileCached
	{
	my($filepath)	= @_;

	return undef unless defined $filepath;

	if(exists($cache->{$filepath}) && ($statistic->{$filepath}->{'lastchanged'} == (stat($filepath))[9]))
		{
		$statistic->{$filepath}->{'cached'}++;
		}
	else
		{
		$cache->{$filepath} = LoadFile($filepath);
				
		$statistic->{$filepath}->{'lastchanged'} = (stat($filepath))[9];
		$statistic->{$filepath}->{'read'}++;
		}

	return $cache->{$filepath};
	}


sub CacheStatistics
	{
	my ($filepath)	= @_;

	return $statistic unless defined $filepath;

	return exists($statistic->{$filepath}) ? $statistic->{$filepath} : undef;
	}


1;
__END__