Source

AURLite / lib / PBCache.pm

Full commit
package PBCache;

use warnings;
use strict;

use File::stat;

our $Path = 'cache';

sub _path
{
    my ($pkgname) = @_;

    my $first = substr $pkgname, 0, 1;
    $first = q{0} if $first =~ /[0-9]/;

    my $dir   = "$Path/$first";
    mkdir $dir or die "mkdir $dir: $!" unless -d $dir;

    return "$dir/$pkgname";
}

sub mtime
{
    my ($pkgname) = @_;

    my $path = _path( $pkgname );
    return undef unless -f $path;
    return stat( $path )->mtime;
}

sub retrieve
{
    my ($pkgname) = @_;

    my $path = _path( $pkgname );
    return undef unless -f $path;

    open my $fh, '<:encoding(utf8)', $path or die "open: $!";
    local $/;
    return <$fh>;
}

sub store
{
    my ($pkgname, $pbtext, $mtime) = @_;

    my $path = _path( $pkgname );
    open my $fh, '>:encoding(utf8)', $path or die "open: $!";
    print $fh $pbtext or die "print: $!";
    close $fh;

    utime $mtime, $mtime, $path or die "utime: $!";
    return;
}

1;