[Jifty-commit] jifty branch, master, updated. 7c76e2bdde55cae69693933e243eced1b81e9b57

Jifty commits jifty-commit at lists.jifty.org
Wed Jan 27 21:00:55 EST 2010


The branch, master has been updated
       via  7c76e2bdde55cae69693933e243eced1b81e9b57 (commit)
       via  7d67747a1aef89854e50acf734d40a5b6c41eb3a (commit)
      from  9196d3d0661490d6064ec8044f1e06ad8a72b950 (commit)

Summary of changes:
 lib/Jifty/CAS.pm                                  |   61 +++++++++++++++++++++
 lib/Jifty/Plugin/CompressedCSSandJS.pm            |   29 ----------
 lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm |    9 ++--
 t/cas-memcached.t                                 |   60 ++++++++++++++++++++
 t/cas-memcached.t-config.yml                      |    7 +++
 5 files changed, 132 insertions(+), 34 deletions(-)
 create mode 100644 t/cas-memcached.t
 create mode 100644 t/cas-memcached.t-config.yml

- Log -----------------------------------------------------------------
commit 7d67747a1aef89854e50acf734d40a5b6c41eb3a
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Jan 27 20:24:40 2010 -0500

    Cleanup serving of CAS objects into Jifty::CAS

diff --git a/lib/Jifty/CAS.pm b/lib/Jifty/CAS.pm
index 3f7e54f..b6aecde 100644
--- a/lib/Jifty/CAS.pm
+++ b/lib/Jifty/CAS.pm
@@ -57,6 +57,67 @@ C<NAME>, or undef if none such exists.
 Returns a L<Jifty::CAS::Blob> for the given pair of C<DOMAIN> and
 C<KEY>, or undef if none such exists.
 
+=head2 serve_by_name DOMAIN NAME REQUESTED_KEY
+
+Intelligently serves up the content of the object at NAME (B<not>
+REQUESTED_KEY) in DOMAIN.  REQUESTED_KEY is currently used only to check if the
+content at NAME equals the content requested.  If so, this method responds with
+an HTTP 304 status, indicating the content hasn't changed.  This use case
+assumes that content is served to clients from the CAS with the CAS key (an MD5
+sum) as the filename or part of it.
+
+The C<content_type> key in the requested object's metadata is expected to be
+set and is used for the HTTP response.  If a true value is set for C<deflate>
+in the object's metadata, then this method will serve gzipped content if the
+client supports it.
+
+This method is usually called from a dispatcher rule.  Returns the HTTP status
+code set by this method (possibly for your use in the dispatcher).
+
 =cut
 
+sub serve_by_name {
+    my ($class, $domain, $name, $incoming_key) = @_;
+    my $key = Jifty::CAS->key($domain, $name);
+
+    return $class->_serve_404( $domain, $name, "Unable to lookup key." )
+        if not defined $key;
+
+    if ( Jifty->handler->cgi->http('If-Modified-Since') and $incoming_key eq $key ) {
+        Jifty->log->debug("Returning 304 for CAS cached $domain:$name ($key)");
+        Jifty->handler->apache->header_out( Status => 304 );
+        return 304;
+    }
+
+    my $obj = Jifty::CAS->retrieve($domain, $key);
+
+    return $class->_serve_404( $domain, $name, "Unable to retrieve blob." )
+        if not defined $obj;
+
+    my $compression = '';
+    $compression = 'gzip' if $obj->metadata->{deflate}
+      && Jifty::View::Static::Handler->client_accepts_gzipped_content;
+
+    Jifty->handler->apache->content_type($obj->metadata->{content_type});
+    Jifty::View::Static::Handler->send_http_header($compression, length($obj->content));
+
+    if ( $compression ) {
+        Jifty->log->debug("Sending gzipped squished $domain:$name ($key) from CAS");
+        binmode STDOUT;
+        print $obj->content_deflated;
+    } else {
+        Jifty->log->debug("Sending squished $domain:$name ($key) from CAS");
+        print $obj->content;
+    }
+    return 200;
+}
+
+sub _serve_404 {
+    my ($class, $domain, $name, $msg) = @_;
+    $msg ||= '';
+    Jifty->log->error("Returning 404 for CAS cached $domain:$name.  $msg");
+    Jifty->handler->apache->header_out( Status => 404 );
+    return 404;
+}
+
 1;
diff --git a/lib/Jifty/Plugin/CompressedCSSandJS.pm b/lib/Jifty/Plugin/CompressedCSSandJS.pm
index 1a8d190..d360471 100644
--- a/lib/Jifty/Plugin/CompressedCSSandJS.pm
+++ b/lib/Jifty/Plugin/CompressedCSSandJS.pm
@@ -254,35 +254,6 @@ sub minify_js {
     $$input = $output;
 }
 
-# this should be cleaned up and moved to Jifty::CAS
-sub _serve_cas_object {
-    my ($self, $name, $incoming_key) = @_;
-    my $key = Jifty::CAS->key('ccjs', $name);
-
-    if ( Jifty->handler->cgi->http('If-Modified-Since') and $incoming_key eq $key ) {
-        $self->log->debug("Returning 304 for cached $name");
-        Jifty->handler->apache->header_out( Status => 304 );
-        return;
-    }
-
-    my $obj = Jifty::CAS->retrieve('ccjs', $key);
-    my $compression = '';
-    $compression = 'gzip' if $obj->metadata->{deflate}
-      && Jifty::View::Static::Handler->client_accepts_gzipped_content;
-
-    Jifty->handler->apache->content_type($obj->metadata->{content_type});
-    Jifty::View::Static::Handler->send_http_header($compression, length($obj->content));
-
-    if ( $compression ) {
-        $self->log->debug("Sending gzipped squished $name");
-        binmode STDOUT;
-        print $obj->content_deflated;
-    } else {
-        $self->log->debug("Sending squished $name");
-        print $obj->content;
-    }
-}
-
 sub _js_is_skipped {
     my $self       = shift;
     my $file       = shift;
diff --git a/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm b/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
index 6900120..0cbfeaa 100644
--- a/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
+++ b/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
@@ -18,7 +18,6 @@ use HTTP::Date ();
 
 use Jifty::Dispatcher -base;
 
-
 on '/__jifty/js/*' => run {
     my $arg = $1;
     if ( $arg !~ /^[0-9a-f]{32}\.js$/ ) {
@@ -34,8 +33,8 @@ on '/__jifty/js/*' => run {
     $ccjs->_generate_javascript;
 
     $arg =~ s/\.js$//;
-    $ccjs->_serve_cas_object( 'js-all', $arg );
-    abort;
+    my $status = Jifty::CAS->serve_by_name( 'ccjs', 'js-all', $arg );
+    abort $status;
 };
 
 on '/__jifty/css/*' => run {
@@ -51,8 +50,8 @@ on '/__jifty/css/*' => run {
     $ccjs->generate_css;
 
     $arg =~ s/\.css$//;
-    $ccjs->_serve_cas_object( 'css-all', $arg );
-    abort;
+    my $status = Jifty::CAS->serve_by_name( 'ccjs', 'css-all', $arg );
+    abort $status;
 };
 
 1;

commit 7c76e2bdde55cae69693933e243eced1b81e9b57
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Jan 27 21:00:30 2010 -0500

    Basic tests for the CAS memcached store

diff --git a/t/cas-memcached.t b/t/cas-memcached.t
new file mode 100644
index 0000000..65fbeff
--- /dev/null
+++ b/t/cas-memcached.t
@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+
+use Jifty::Test;
+
+eval "use Cache::Memcached";
+plan skip_all => "Cache::Memcached required for testing CAS memcache store" if $@;
+
+require IO::Socket::INET;
+# check if there's a running memcached on the default port, skip otherwise
+plan skip_all => "Testing CAS memcached store requires a memcached running on the default port"
+    unless IO::Socket::INET->new('127.0.0.1:11211');
+
+plan tests => 23;
+
+my $data    = "a" x (1024*10);
+my $databig = "a" x (1024*1024*2);
+
+{
+    ok((grep { $_ eq 'Jifty::CAS::Store::Memcached' } @Jifty::CAS::ISA), 'Using memcached backed store');
+    my $key = Jifty::CAS->publish("test$$", 'one', $data, { deflate => 1, content_type => 'text/plain' });
+    ok $key, "Published";
+    is length $key, 32, "Key is 32 chars long - an MD5 sum";
+    is(Jifty::CAS->key("test$$", "one"), $key, "Matches what we get back from ->key");
+    
+    my $blob = Jifty::CAS->retrieve("test$$", $key);
+    ok $blob, "retrieved value";
+    isa_ok $blob, 'Jifty::CAS::Blob', 'got a blob';
+    is $blob->content, $data, "content is the same";
+    is_deeply $blob->metadata, { deflate => 1, content_type => 'text/plain' }, "metadata is still good";
+    is $blob->{content_deflated}, undef, "no deflated content until we request it";
+    ok $blob->content_deflated, "got deflated content";
+    ok $blob->{content_deflated}, "now deflated content exists";
+}
+
+{
+    my $key = Jifty::CAS->publish("test$$", "two", $databig, { deflate => 1, content_type => 'text/plain' });
+    is $key, undef, "Not published, there was an error";
+    is(Jifty::CAS->key("test$$", "two"), undef, "Can't lookup a key because it isn't there");
+}
+
+{
+    Jifty->config->framework('CAS')->{'MemcachedFallback'} = 1;
+    my $key = Jifty::CAS->publish("test$$", "three", $databig, { deflate => 1, content_type => 'text/plain' });
+    ok $key, "Published";
+    is length $key, 32, "Key is 32 chars long - an MD5 sum";
+    is(Jifty::CAS->key("test$$", "three"), $key, "Matches what we get back from ->key");
+    
+    my $blob = Jifty::CAS->retrieve("test$$", $key);
+    ok $blob, "retrieved value";
+    isa_ok $blob, 'Jifty::CAS::Blob', 'got a blob';
+    is $blob->content, $databig, "content is the same";
+    is_deeply $blob->metadata, { deflate => 1, content_type => 'text/plain' }, "metadata is still good";
+    is $blob->{content_deflated}, undef, "no deflated content until we request it";
+    ok $blob->content_deflated, "got deflated content";
+    ok $blob->{content_deflated}, "now deflated content exists";
+}
+
+# XXX TODO test serving up of CAS content
+
diff --git a/t/cas-memcached.t-config.yml b/t/cas-memcached.t-config.yml
new file mode 100644
index 0000000..d70a6be
--- /dev/null
+++ b/t/cas-memcached.t-config.yml
@@ -0,0 +1,7 @@
+---
+framework:
+  CAS:
+    BaseClass: 'Jifty::CAS::Store::Memcached'
+    Memcached:
+      compress_threshold: 100000000 # effectively disables compression
+    MemcachedFallback: 0

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list