[Jifty-commit] r5116 - in jifty/trunk: lib/Jifty lib/Jifty/Plugin lib/Jifty/Plugin/CompressedCSSandJS

Jifty commits jifty-commit at lists.jifty.org
Fri Feb 15 17:02:22 EST 2008


Author: clkao
Date: Fri Feb 15 17:02:21 2008
New Revision: 5116

Added:
   jifty/trunk/lib/Jifty/CAS.pm
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Everything.pm
   jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm
   jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm

Log:
Content-addressable storage (in memory) facility for jifty.

Port CCJS plugin to use CAS.


Added: jifty/trunk/lib/Jifty/CAS.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/CAS.pm	Fri Feb 15 17:02:21 2008
@@ -0,0 +1,71 @@
+package Jifty::CAS;
+use strict;
+
+=head1 NAME
+
+Jifty::CAS - Jifty's Content-addressable storage facility
+
+=head1 SYNOPSIS
+
+  my $key = Jifty::CAS->publish('js' => 'all', $content,
+                      { hash_with => $content, # default behaviour
+                        content_type => 'application/x-javascript',
+                        deflate => 1
+                      });
+
+  $ie_key = Jifty::CAS->publish('js' => 'ie-only', $ie_content,
+                      { hash_with => $ie_content,
+                        content_type => 'application/x-javascript',
+                      });
+
+  $key = Jifty::CAS->key('js', 'ie-only');
+  my $blob = Jifty::CAS->retrieve('js', $key);
+
+=head1 DESCRIPTION
+
+
+
+=cut
+
+my %CONTAINER;
+
+use Digest::MD5 'md5_hex';
+use Compress::Zlib ();
+
+sub publish {
+    my ($class, $domain, $name, $content, $opt) = @_;
+    my $db = $CONTAINER{$domain} ||= {};
+
+    my $key = md5_hex( delete $opt->{hash_with} || $content );
+    $db->{DB}{$key} = Jifty::CAS::Blob->new
+        ( { content => $content,
+            metadata => $opt } );
+    $db->{KEYS}{$name} = $key;
+
+    return $key;
+}
+
+sub key {
+    my ($class, $domain, $name) = @_;
+    return $CONTAINER{$domain}{KEYS}{$name};
+}
+
+sub retrieve {
+    my ($class, $domain, $key) = @_;
+    return $CONTAINER{$domain}{DB}{$key};
+}
+
+package Jifty::CAS::Blob;
+
+use base 'Class::Accessor::Fast';
+
+__PACKAGE__->mk_accessors(qw(content content_deflated metadata));
+
+sub new {
+    my $class = shift;
+    my $self = $class->SUPER::new(@_);
+    $self->content_deflated(Compress::Zlib::memGzip($self->content)) if $self->metadata->{deflate};
+    return $self;
+}
+
+1;

Modified: jifty/trunk/lib/Jifty/Everything.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Everything.pm	(original)
+++ jifty/trunk/lib/Jifty/Everything.pm	Fri Feb 15 17:02:21 2008
@@ -85,6 +85,8 @@
 use Jifty::Subs ();
 use Jifty::Subs::Render ();
 
+use Jifty::CAS ();
+
 use Jifty::Module::Pluggable;
 #Jifty::Module::Pluggable->import(search_path => ['Jifty::Web::Form::Field'], require     => 1, except      => qr/\.#/);
 #__PACKAGE__->plugins;

Modified: jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm	Fri Feb 15 17:02:21 2008
@@ -4,9 +4,7 @@
 package Jifty::Plugin::CompressedCSSandJS;
 use base 'Jifty::Plugin';
 
-use Digest::MD5 'md5_hex';
 use IPC::Run3 'run3';
-use Compress::Zlib ();
 use IO::Handle ();
 
 =head1 NAME
@@ -45,18 +43,7 @@
 
 =cut
 
-__PACKAGE__->mk_accessors(qw(css js jsmin cdn gzip_enabled
-
-    cached_javascript 
-    cached_javascript_gzip
-    cached_javascript_digest 
-    cached_javascript_time 
-
-    cached_css
-    cached_css_gzip
-    cached_css_time
-    cached_css_digest
-));
+__PACKAGE__->mk_accessors(qw(css js jsmin cdn gzip_enabled));
 
 =head2 init
 
@@ -123,7 +110,7 @@
 
     $self->_generate_javascript;
     Jifty->web->out( qq[<script type="text/javascript" src="@{[ $self->cdn ]}/__jifty/js/]
-            . $self->cached_javascript_digest
+            . Jifty::CAS->key('ccjs', 'js-all')
             . qq[.js"></script>] );
     return 0;
 }
@@ -133,7 +120,7 @@
     $self->generate_css;
     Jifty->web->out(
     qq{<link rel="stylesheet" type="text/css" href="@{[ $self->cdn ]}/__jifty/css/}
-    . $self->cached_css_digest . '.css" />');
+    . Jifty::CAS->key('ccjs', 'css-all') . '.css" />');
     return 0;
 }
 
@@ -147,24 +134,20 @@
 
 sub generate_css {
     my $self = shift;
-            
-    if (not defined $self->cached_css_digest or Jifty->config->framework('DevelMode')) {
-        Jifty->log->debug("Generating CSS...");
-
-        my @roots = map { Jifty::Util->absolute_path( $_ ) }
-            Jifty->handler->view('Jifty::View::Static::Handler')->roots;
-    
-        my $css = CSS::Squish->new( roots => \@roots )->concatenate(
-            map { File::Spec->catfile('css', $_ ) }
-                @{ Jifty->web->css_files }
-        );
-
-        $self->cached_css( $css );
-        $self->cached_css_digest( md5_hex( $css ) );
-        $self->cached_css_time( time );
-        $self->cached_css_gzip(Compress::Zlib::memGzip( $css));
 
-    }
+    return if Jifty::CAS->key('ccjs', 'css-all') && !Jifty->config->framework('DevelMode');
+
+    Jifty->log->debug("Generating CSS...");
+
+    my @roots = map { Jifty::Util->absolute_path( $_ ) }
+        Jifty->handler->view('Jifty::View::Static::Handler')->roots;
+
+    my $css = CSS::Squish->new( roots => \@roots )->concatenate(
+        map { File::Spec->catfile('css', $_ ) } @{ Jifty->web->css_files }
+    );
+
+    Jifty::CAS->publish( 'ccjs', 'css-all', $css,
+        { content_type => 'text/css', deflate => $self->gzip_enabled, time => time() } );
 }
 
 
@@ -179,11 +162,10 @@
 sub _generate_javascript {
     my $self = shift;
 
-    if ( not defined $self->cached_javascript_digest
-        or Jifty->config->framework('DevelMode') ) {
-        Jifty->log->debug("Generating JS...");
+    return if Jifty::CAS->key('ccjs', 'js-all') && !Jifty->config->framework('DevelMode');
+    Jifty->log->debug("Generating JS...");
 
-        # for the file cascading logic
+    # for the file cascading logic
         my $static_handler = Jifty->handler->view('Jifty::View::Static::Handler');
         my $js = "";
 
@@ -208,11 +190,9 @@
             eval { $self->minify_js(\$js) };
             Jifty->log->error("Unable to run jsmin: $@") if $@;
         }
-        $self->cached_javascript($js);
-        $self->cached_javascript_digest( md5_hex($js) );
-        $self->cached_javascript_time(time);
-        $self->cached_javascript_gzip(Compress::Zlib::memGzip( $js));
-    }
+
+    Jifty::CAS->publish( 'ccjs', 'js-all', $js,
+        { content_type => 'application/x-javascript', deflate => $self->gzip_enabled, time => time() } );
 }
 
 =head2 minify_js \$js
@@ -245,4 +225,33 @@
     $$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 ) {
+        Jifty->log->debug("Returning 304 for cached $name");
+        Jifty->handler->apache->header_out( Status => 304 );
+        return;
+    }
+
+    my $obj = Jifty::CAS->retrieve('ccjs', $key);
+    Jifty->handler->apache->content_type($obj->metadata->{content_type});
+    Jifty->handler->apache->header_out( 'Expires' => HTTP::Date::time2str( time + 31536000 ) );
+
+    if ($obj->metadata->{deflate} && Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
+        Jifty->log->debug("Sending gzipped squished $name");
+        Jifty->handler->apache->header_out( "Content-Encoding" => "gzip" );
+        Jifty->handler->apache->send_http_header();
+        binmode STDOUT;
+        print $obj->content_deflated;
+    } else {
+        Jifty->log->debug("Sending squished $name");
+        Jifty->handler->apache->send_http_header();
+        print $obj->content;
+    }
+}
+
+
 1;

Modified: jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm	Fri Feb 15 17:02:21 2008
@@ -32,33 +32,8 @@
 
     $ccjs->_generate_javascript;
 
-    if ( Jifty->handler->cgi->http('If-Modified-Since')
-        and $arg eq $ccjs->cached_javascript_digest . '.js' )
-    {
-        Jifty->log->debug("Returning 304 for cached javascript");
-        Jifty->handler->apache->header_out( Status => 304 );
-        Jifty->handler->apache->send_http_header();
-        return;
-    }
-
-    Jifty->handler->apache->content_type("application/x-javascript");
-    Jifty->handler->apache->header_out( 'Expires' => HTTP::Date::time2str( time + 31536000 ) );
-
-    # XXX TODO: If we start caching the squished JS in a file somewhere, we
-    # can have the static handler serve it, which would take care of gzipping
-    # for us.
-    if ( $ccjs->gzip_enabled && Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
-        Jifty->log->debug("Sending gzipped squished JS");
-        Jifty->handler->apache->header_out( "Content-Encoding" => "gzip" );
-        Jifty->handler->apache->send_http_header();
-        binmode STDOUT;
-        print $ccjs->cached_javascript_gzip;
-
-    } else {
-        Jifty->log->debug("Sending squished JS");
-        Jifty->handler->apache->send_http_header();
-        print $ccjs->cached_javascript;
-    }
+    $arg =~ s/\.js$//;
+    $ccjs->_serve_cas_object( 'js-all', $arg );
     abort;
 };
 
@@ -74,31 +49,8 @@
 
     $ccjs->generate_css;
 
-    if ( Jifty->handler->cgi->http('If-Modified-Since')
-        and $arg eq $ccjs->cached_css_digest . '.css' )
-    {
-        Jifty->log->debug("Returning 304 for cached css");
-        Jifty->handler->apache->header_out( Status => 304 );
-        return;
-    }
-
-    Jifty->handler->apache->content_type("text/css");
-    Jifty->handler->apache->header_out( 'Expires' => HTTP::Date::time2str( time + 31536000 ) );
-
-    # XXX TODO: If we start caching the squished CSS in a file somewhere, we
-    # can have the static handler serve it, which would take care of gzipping
-    # for us.
-    if ($ccjs->gzip_enabled && Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
-        Jifty->log->debug("Sending gzipped squished CSS");
-        Jifty->handler->apache->header_out( "Content-Encoding" => "gzip" );
-        Jifty->handler->apache->send_http_header();
-        binmode STDOUT;
-        print $ccjs->cached_css_gzip;
-    } else {
-        Jifty->log->debug("Sending squished CSS");
-        Jifty->handler->apache->send_http_header();
-        print $ccjs->cached_css;
-    }
+    $arg =~ s/\.css$//;
+    $ccjs->_serve_cas_object( 'css-all', $arg );
     abort;
 };
 


More information about the Jifty-commit mailing list