| 1 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package Jifty::CAS; | ||||||||||||||||||||
| 5 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Plack::Response; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Jifty::CAS - Jifty's Content-Addressable Storage facility | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | my $key = Jifty::CAS->publish('js' => 'all', $content, | ||||||||||||||||||||
| 15 | { hash_with => $content, # default behaviour | ||||||||||||||||||||
| 16 | content_type => 'application/x-javascript', | ||||||||||||||||||||
| 17 | }); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | $ie_key = Jifty::CAS->publish('js' => 'ie-only', $ie_content, | ||||||||||||||||||||
| 20 | { hash_with => $ie_content, | ||||||||||||||||||||
| 21 | content_type => 'application/x-javascript', | ||||||||||||||||||||
| 22 | }); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | $key = Jifty::CAS->key('js', 'ie-only'); | ||||||||||||||||||||
| 25 | my $blob = Jifty::CAS->retrieve('js', $key); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | Provides an in-memory C<md5>-addressed content store. Content is | ||||||||||||||||||||
| 30 | stored under a "domain", and can be addressed using wither the "key", | ||||||||||||||||||||
| 31 | which is an C<md5> sum, or the "name", which simply stores the most | ||||||||||||||||||||
| 32 | recent key provided with that name. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 BACKENDS | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | The default data store is a per-process, in-memory store via | ||||||||||||||||||||
| 37 | L<Jifty::CAS::Store::Memory>. L<Jifty::CAS::Store::Memcached> is also | ||||||||||||||||||||
| 38 | available, and has the benefits of sharing the cache across all | ||||||||||||||||||||
| 39 | instances of a Jifty app using L<Jifty::CAS>. The memcached store is | ||||||||||||||||||||
| 40 | limited to objects less than 1MB in size, however. | ||||||||||||||||||||
| 41 | L<Jifty::CAS::Store::LocalFile> provides a durable store, which is | ||||||||||||||||||||
| 42 | well-suited for sharing the cache across instances and restarts. | ||||||||||||||||||||
| 43 | Because of its speed, however, L<Jifty::CAS::Store::Nested> is suggested | ||||||||||||||||||||
| 44 | to provide a layered cache, most probably with fast memory-based cache | ||||||||||||||||||||
| 45 | in front of a durable, file-based cache. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =head1 METHODS | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =head2 publish DOMAIN NAME CONTENT METADATA | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | Publishes the given C<CONTENT> at the address C<DOMAIN> and C<NAME>. | ||||||||||||||||||||
| 52 | C<METADATA> is an arbitrary hash; see L<Jifty::CAS::Blob> for more. | ||||||||||||||||||||
| 53 | Returns the key on success, or undef on failure. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head2 key DOMAIN NAME | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Returns the most recent key for the given pair of C<DOMAIN> and | ||||||||||||||||||||
| 58 | C<NAME>, or undef if none such exists. | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head2 retrieve DOMAIN KEY | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | Returns a L<Jifty::CAS::Blob> for the given pair of C<DOMAIN> and | ||||||||||||||||||||
| 63 | C<KEY>, or undef if none such exists. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head2 uri DOMAIN NAME | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Returns a URL where the given C<DOMAIN> and C<NAME> can be accessed. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 serve DOMAIN ARGUMENT ENV | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Serves a plack request in C<ENV>, given a C<DOMAIN> and an C<ARGUMENT>, | ||||||||||||||||||||
| 72 | which may wither be a key or a name. This method is usually only called | ||||||||||||||||||||
| 73 | by L</wrap>, which calls it as appropriate for all requests under | ||||||||||||||||||||
| 74 | C</__jifty/cas/>. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =head2 backend [DOMAIN] | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Returns the L<Jifty::CAS::Store> which backs the given C<DOMAIN>. If | ||||||||||||||||||||
| 79 | C<DOMAIN> is not specified, returns the default backing store. | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head2 config | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | Returns the CAS configuration, as specified in the framework's | ||||||||||||||||||||
| 84 | configuration. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head2 setup | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | Configures the CAS for use. | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head2 wrap APP | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | Given a PSGI application, wraps it to handle C</__jifty/cas/> requests | ||||||||||||||||||||
| 93 | by calling the appropriate L</serve> method. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | sub config { | ||||||||||||||||||
|
Subroutine
config: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 98 | 0 | my $class = shift; | |||||||||||||||||||
| 99 | 0 | my $config = Jifty->config->framework('CAS'); | |||||||||||||||||||
| 100 | 0 | ▶ | if (Jifty->config->framework('ConfigFileVersion') < 6) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | $config = { | ||||||||||||||||||||
| 102 | Default => { | ||||||||||||||||||||
| 103 | Class => $config->{'BaseClass'}, | ||||||||||||||||||||
| 104 | 0 | ▶ | %{ $config->{'Memcached'} || {} }, | ||||||||||||||||||
|
Condition0%0/2
$config->{'Memcached'} || {}
MC/DC0%0/1
$config->{'Memcached'} || {}
$config->{'Memcached'}
Truth table0%0/2
A
A= $config->{'Memcached'}
| |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | 0 | ▶ | $config->{Default}{Class} ||= "Jifty::CAS::Store::Memory"; | ||||||||||||||||||
|
Condition0%0/2
$config->{"Default"}{"Class"} ||= "Jifty::CAS::Store::Memory"
MC/DC0%0/1
$config->{"Default"}{"Class"} ||= "Jifty::CAS::Store::Memory"
$config->{"Default"}{"Class"}
Truth table0%0/2
A
A= $config->{"Default"}{"Class"}
| |||||||||||||||||||||
| 109 | 0 | ▶ | $config->{Domains} ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$config->{"Domains"} ||= {}
MC/DC0%0/1
$config->{"Domains"} ||= {}
$config->{"Domains"}
Truth table0%0/2
A
A= $config->{"Domains"}
| |||||||||||||||||||||
| 110 | 0 | return $config; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 1 | my %BACKENDS; | |||||||||||||||||||
| 114 | my $DEFAULT_BACKEND; | ||||||||||||||||||||
| 115 | 0 | ▶ | sub setup { | ||||||||||||||||||
|
Subroutine
setup: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 116 | 0 | my $class = shift; | |||||||||||||||||||
| 117 | 0 | my $config = $class->config; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | my %default = %{$config->{Default}}; | |||||||||||||||||||
| 120 | 0 | my $defaultclass = delete $default{Class}; | |||||||||||||||||||
| 121 | 0 | Jifty::Util->require( $defaultclass ); | |||||||||||||||||||
| 122 | $DEFAULT_BACKEND = $defaultclass->new( | ||||||||||||||||||||
| 123 | 0 | map {lc $_ => $config->{Default}{$_}} keys %default | |||||||||||||||||||
| 124 | ); | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | for my $domain (keys %{$config->{Domains}}) { | |||||||||||||||||||
| 127 | 0 | my %domain = %{ $config->{Domains}{$domain} }; | |||||||||||||||||||
| 128 | 0 | my $storeclass = delete $domain{Class}; | |||||||||||||||||||
| 129 | 0 | Jifty::Util->require( $storeclass ); | |||||||||||||||||||
| 130 | $BACKENDS{$domain} = $storeclass->new( | ||||||||||||||||||||
| 131 | 0 | map {lc $_ => $config->{Domains}{$domain}{$_}} keys %domain | |||||||||||||||||||
| 132 | ); | ||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | ▶ | sub wrap { | ||||||||||||||||||
|
Subroutine
wrap: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 137 | 0 | my ($class, $app) = @_; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 140 | 0 | my $env = shift; | |||||||||||||||||||
| 141 | 0 | ▶ | if (my ($domain, $arg) = $env->{PATH_INFO} =~ m{/__jifty/cas/(.*?)/(.*?)(?:\.|$)}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 0 | return $class->serve($domain,$arg,$env); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||
| 145 | 0 | return $app->($env); | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | 0 | }; | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | ▶ | sub backend { | ||||||||||||||||||
|
Subroutine
backend: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 151 | 0 | my $class = shift; | |||||||||||||||||||
| 152 | 0 | my ($domain) = @_; | |||||||||||||||||||
| 153 | 0 | ▶ | return $DEFAULT_BACKEND unless @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | ▶ | return $BACKENDS{$domain} || $DEFAULT_BACKEND; | ||||||||||||||||||
|
Condition0%0/3
$BACKENDS{$domain} || $DEFAULT_BACKEND
MC/DC0%0/2
$BACKENDS{$domain} || $DEFAULT_BACKEND
$BACKENDS{$domain}
$DEFAULT_BACKEND
Truth table0%0/3
A || B
A= $BACKENDS{$domain}B= $DEFAULT_BACKEND
| |||||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | sub publish { | ||||||||||||||||||
|
Subroutine
publish: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 158 | 0 | my $class = shift; | |||||||||||||||||||
| 159 | 0 | my ($domain) = @_; | |||||||||||||||||||
| 160 | 0 | ▶ | ($BACKENDS{$domain} || $DEFAULT_BACKEND)->publish(@_); | ||||||||||||||||||
|
Condition0%0/3
$BACKENDS{$domain} || $DEFAULT_BACKEND
MC/DC0%0/2
$BACKENDS{$domain} || $DEFAULT_BACKEND
$BACKENDS{$domain}
$DEFAULT_BACKEND
Truth table0%0/3
A || B
A= $BACKENDS{$domain}B= $DEFAULT_BACKEND
| |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 0 | ▶ | sub key { | ||||||||||||||||||
|
Subroutine
key: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 164 | 0 | my $class = shift; | |||||||||||||||||||
| 165 | 0 | my ($domain) = @_; | |||||||||||||||||||
| 166 | 0 | ▶ | ($BACKENDS{$domain} || $DEFAULT_BACKEND)->key(@_); | ||||||||||||||||||
|
Condition0%0/3
$BACKENDS{$domain} || $DEFAULT_BACKEND
MC/DC0%0/2
$BACKENDS{$domain} || $DEFAULT_BACKEND
$BACKENDS{$domain}
$DEFAULT_BACKEND
Truth table0%0/3
A || B
A= $BACKENDS{$domain}B= $DEFAULT_BACKEND
| |||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | ▶ | sub retrieve { | ||||||||||||||||||
|
Subroutine
retrieve: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 170 | 0 | my $class = shift; | |||||||||||||||||||
| 171 | 0 | my ($domain) = @_; | |||||||||||||||||||
| 172 | 0 | ▶ | ($BACKENDS{$domain} || $DEFAULT_BACKEND)->retrieve(@_); | ||||||||||||||||||
|
Condition0%0/3
$BACKENDS{$domain} || $DEFAULT_BACKEND
MC/DC0%0/2
$BACKENDS{$domain} || $DEFAULT_BACKEND
$BACKENDS{$domain}
$DEFAULT_BACKEND
Truth table0%0/3
A || B
A= $BACKENDS{$domain}B= $DEFAULT_BACKEND
| |||||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 0 | ▶ | sub uri { | ||||||||||||||||||
|
Subroutine
uri: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 176 | 0 | my $class = shift; | |||||||||||||||||||
| 177 | 0 | my ($domain) = @_; | |||||||||||||||||||
| 178 | 0 | ▶ | ($BACKENDS{$domain} || $DEFAULT_BACKEND)->uri(@_); | ||||||||||||||||||
|
Condition0%0/3
$BACKENDS{$domain} || $DEFAULT_BACKEND
MC/DC0%0/2
$BACKENDS{$domain} || $DEFAULT_BACKEND
$BACKENDS{$domain}
$DEFAULT_BACKEND
Truth table0%0/3
A || B
A= $BACKENDS{$domain}B= $DEFAULT_BACKEND
| |||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | ▶ | sub serve { | ||||||||||||||||||
|
Subroutine
serve: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 182 | 0 | my $class = shift; | |||||||||||||||||||
| 183 | 0 | my ($domain) = @_; | |||||||||||||||||||
| 184 | 0 | ▶ | ($BACKENDS{$domain} || $DEFAULT_BACKEND)->serve(@_); | ||||||||||||||||||
|
Condition0%0/3
$BACKENDS{$domain} || $DEFAULT_BACKEND
MC/DC0%0/2
$BACKENDS{$domain} || $DEFAULT_BACKEND
$BACKENDS{$domain}
$DEFAULT_BACKEND
Truth table0%0/3
A || B
A= $BACKENDS{$domain}B= $DEFAULT_BACKEND
| |||||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 1 | 1; | |||||||||||||||||||