MOBY
view release on metacpan or search on metacpan
lib/MOBY/RDF/Ontologies/Cache/CacheUtils.pm view on Meta::CPAN
#-----------------------------------------------------------------
# MOBY::RDF::Ontologies::Cache::Cache
# Author: Edward Kawas <edward.kawas@gmail.com>,
#
# For copyright and disclaimer see below.
#
# $Id: CacheUtils.pm,v 1.3 2008/09/02 13:12:33 kawas Exp $
#-----------------------------------------------------------------
package MOBY::RDF::Ontologies::Cache::CacheUtils;
#imports
use XML::LibXML;
use File::Spec;
use strict;
use DirHandle;
# names of cache directories/files/locks
use constant LIST_FILE => '__L__I__S__T__';
use constant RDF_FILE => '__R__D__F__';
use constant UPDATE_FILE => '__U__P__D__A__T__E__F__I__L__E__';
use constant DATATYPES_CACHE => 'dataTypes';
use constant SERVICES_CACHE => 'services';
use constant NAMESPACES_CACHE => 'namespaces';
use constant SERVICETYPES_CACHE => 'serviceTypes';
use vars qw /$VERSION/;
$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
=head1 NAME
MOBY::RDF::Ontologies::Cache::CacheUtils - Utility module that aids in caching
=head1 SYNOPSIS
use MOBY::RDF::Ontologies::Cache::CacheUtils;
my $cachedir = "C:/tmp/";
my $url = "http://moby.ucalgary.ca/moby/MOBY-Central.pl";
my $uri = "http://moby.ucalgary.ca/MOBY/Central";
my $x = MOBY::RDF::Ontologies::Cache::CacheUtils->new(
endpoint => $url,
namespace => $uri,
cache => $cachedir,
);
# create the cache directory
$x->create_cache_dirs;
# check if the cache exists
print "Cache exists!\n" if $x->cache_exists();
print "Cache doesnt exist!\n" unless $x->cache_exists();
# get the cache dir
print "The cache dir is: " . $x->cachedir . "\n";
# get the exact location of all cache dirs
my $dirs = $x->get_cache_dirs();
while ( ( $key, $value ) = each( %{$dirs} ) ) {
print "$key is stored in $value\n";
}
=head1 DESCRIPTION
This module aids in the creation and maintainence of cache directories
=cut
=head1 AUTHORS
Edward Kawas (edward.kawas [at] gmail [dot] com)
=cut
#-----------------------------------------------------------------
=head1 SUBROUTINES
=cut
=head2 new
lib/MOBY/RDF/Ontologies/Cache/CacheUtils.pm view on Meta::CPAN
=cut
sub cache_exists {
my ( $self, $registry ) = @_;
my $pathToList =
File::Spec->catfile( $self->cachedir,
$self->_clean( $self->_endpoint($registry) ),
SERVICES_CACHE );
return -e $pathToList;
}
#-----------------------------------------------------------------
# cache_exists
#-----------------------------------------------------------------
=head2 cachedir
Return the cache dir
=cut
sub cachedir {
my ( $self, $dir ) = @_;
$self->{cachedir} = $dir if $dir and -d $dir;
return $self->{cachedir};
}
#-----------------------------------------------------------------
# create_cache_dirs
#-----------------------------------------------------------------
=head2 create_cache_dirs
Creates the cache directories needed for generating datatypes and services.
Throws an exception if there are problems creating the directories.
=cut
sub create_cache_dirs {
my ($self) = @_;
my @dirs = (
File::Spec->catfile(
$self->cachedir,
$self->_clean( $self->_endpoint ),
DATATYPES_CACHE
),
File::Spec->catdir(
$self->cachedir,
$self->_clean( $self->_endpoint ),
SERVICES_CACHE
),
File::Spec->catdir(
$self->cachedir,
$self->_clean( $self->_endpoint ),
NAMESPACES_CACHE
),
File::Spec->catdir(
$self->cachedir,
$self->_clean( $self->_endpoint ),
SERVICETYPES_CACHE
),
);
foreach my $file (@dirs) {
my ( $v, $d, $f ) = File::Spec->splitpath($file);
my $dir = File::Spec->catdir($v);
foreach my $part ( File::Spec->splitdir( ( $d . $f ) ) ) {
$dir = File::Spec->catdir( $dir, $part );
next if -d $dir or -e $dir;
mkdir($dir)
|| die( "Error creating caching directory '" . $dir . "':\n$!" );
}
}
}
#-----------------------------------------------------------------
# get_cache_dirs
#-----------------------------------------------------------------
=head2 get_cache_dirs
Gets the cache directories used for a specific cache as a hash.
=cut
sub get_cache_dirs {
my ($self) = @_;
return {
DATATYPES_CACHE => File::Spec->catfile(
$self->cachedir,
$self->_clean( $self->_endpoint ),
DATATYPES_CACHE
),
SERVICES_CACHE => File::Spec->catdir(
$self->cachedir,
$self->_clean( $self->_endpoint ),
SERVICES_CACHE
),
NAMESPACES_CACHE => File::Spec->catdir(
$self->cachedir,
$self->_clean( $self->_endpoint ),
NAMESPACES_CACHE
),
SERVICETYPES_CACHE => File::Spec->catdir(
$self->cachedir,
$self->_clean( $self->_endpoint ),
SERVICETYPES_CACHE
),
};
}
sub plainfiles {
my ($self, $dir )= @_;
my $dh = DirHandle->new($dir) or die "can't opendir $dir: $!";
return sort # sort pathnames
grep { -f } # choose only "plain" files
map { "$dir/$_" } # create full paths
grep { !/^\./ } # filter out dot files
$dh->read(); # read all entries
}
1;
__END__
( run in 0.628 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )