| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::CAS::Store::LocalFile; | ||
| 5 | use Any::Moose; | ||
| 6 | extends 'Jifty::CAS::Store'; | ||
| 7 | has 'path' => ( is => 'rw'); | ||
| 8 | |||
| 9 | use Storable qw(lock_store lock_retrieve); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Jifty::CAS::Store::LocalFile - A local file backend for Jifty's CAS | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | This is a local file backend for L<Jifty::CAS>, which provides a | ||
| 18 | B<durable> backend, unlike L<Jifty::CAS::Store::Memory> and | ||
| 19 | L<Jifty::CAS::Store::Memcached>. For more information about Jifty's | ||
| 20 | CAS, see L<Jifty::CAS/DESCRIPTION>. | ||
| 21 | |||
| 22 | Configuration requires providing a directory which is writable by the | ||
| 23 | web user: | ||
| 24 | |||
| 25 | framework: | ||
| 26 | CAS: | ||
| 27 | Default: | ||
| 28 | Class: 'Jifty::CAS::Store::LocalFile' | ||
| 29 | Path: %var/cas% | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =head2 _store DOMAIN NAME BLOB | ||
| 36 | |||
| 37 | Stores the BLOB (a L<Jifty::CAS::Blob>) on disk. Returns the key on | ||
| 38 | success or undef on failure. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub _store { | ||
| 43 | my ($self, $domain, $name, $blob) = @_; | ||
| 44 | mkdir($self->path) unless -d $self->path; | ||
| 45 | my $dir = $self->path . "/" . $domain; | ||
| 46 | mkdir($dir) unless -d $dir; | ||
| 47 | |||
| 48 | my $path = $dir . "/key-" . $blob->key; | ||
| 49 | unless (-e $path) { | ||
| 50 | lock_store($blob, $path) | ||
| 51 | or warn("Write of blob failed: $!") and return; | ||
| 52 | } | ||
| 53 | |||
| 54 | # Update the symlink | ||
| 55 | my $link = $dir . "/name-" . $name; | ||
| 56 | my $tmp = $dir . "/tmp-" . $name; | ||
| 57 | symlink( "key-".$blob->key, $tmp ) | ||
| 58 | or warn("Symlink failed: $!") and return; | ||
| 59 | rename( $tmp, $link ) | ||
| 60 | or warn("Rename of symlink failed: $!") and return; | ||
| 61 | |||
| 62 | return $blob->key; | ||
| 63 | } | ||
| 64 | |||
| 65 | =head2 key DOMAIN NAME | ||
| 66 | |||
| 67 | Returns the most recent key for the given pair of C<DOMAIN> and | ||
| 68 | C<NAME>, or undef if none such exists. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub key { | ||
| 73 | my ($self, $domain, $name) = @_; | ||
| 74 | my $link = $self->path . "/" . $domain . "/name-" . $name; | ||
| 75 | return unless -l $link; | ||
| 76 | $link = readlink($link); | ||
| 77 | $link =~ s/^key-//; | ||
| 78 | return $link; | ||
| 79 | } | ||
| 80 | |||
| 81 | =head2 retrieve DOMAIN KEY | ||
| 82 | |||
| 83 | Returns a L<Jifty::CAS::Blob> for the given pair of C<DOMAIN> and | ||
| 84 | C<KEY>, or undef if none such exists. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub retrieve { | ||
| 89 | my ($self, $domain, $key) = @_; | ||
| 90 | my $data = $self->path . "/" . $domain . "/key-" . $key; | ||
| 91 | return unless -r $data; | ||
| 92 | |||
| 93 | return lock_retrieve($data); | ||
| 94 | } | ||
| 95 | |||
| 96 | =head2 durable | ||
| 97 | |||
| 98 | Since presumably the files on disk will not simply vanish, the local | ||
| 99 | file store is durable. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub durable { 1 } | ||
| 104 | |||
| 105 | no Any::Moose; | ||
| 106 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); | ||
| 107 | 1; |