Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixup - force a cache obj to be required and under dev no caching
  • Loading branch information
ranguard committed Jan 18, 2014
1 parent 73ab00d commit 470e8e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
16 changes: 8 additions & 8 deletions app.psgi
Expand Up @@ -87,16 +87,16 @@ my $cache = CHI->new(
cache_size => '100k'
);

# Wrap up static to serve lessc parsed files
# Wrap up to serve lessc parsed files
$app = Plack::Middleware::MCLess->wrap($app,
cache => $cache,
cache_ttl => '1 second',
root => "root/static",
files => [
map {"root/static/less/$_.less"}
qw(
cache => $cache,
cache_ttl => '30 minutes',
root => "root/static",
files => [
map {"root/static/less/$_.less"}
qw(
style
)
)
],
);

Expand Down
54 changes: 18 additions & 36 deletions lib/Plack/Middleware/MCLess.pm
Expand Up @@ -13,10 +13,10 @@ our $VERSION = '0.02';
use parent qw(Plack::Middleware);
use Digest::MD5 qw(md5_hex);
use Plack::Util;
use Plack::Util::Accessor qw(root files key mtime expires _data cache cache_ttl);
use Plack::Util::Accessor qw(root files key expires cache cache_ttl);
use Capture::Tiny ':all';
use HTTP::Date ();
use Try::Tiny;

use CSS::Minifier::XS qw(minify);

use Carp;
Expand All @@ -28,7 +28,13 @@ sub new {
my $less = `lessc -v`;
croak("Can't find lessc command") unless $less;

$self->cache_ttl('10 minutes') unless $self->cache_ttl;
if( $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development' ) {
# No caching, always build fresh
$self->cache_ttl('0'); # Auto change if running tests or something?
$self->expires('0');
} else {
$self->cache_ttl('30 minutes') unless $self->cache_ttl;
}

$self->_build_content;

Expand All @@ -40,13 +46,6 @@ sub call {
my $self = shift;
my $env = shift;

if ( $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development' ) {
# Try and reload if needed
my @mtime = map { ( stat($_) )[9] } @{ $self->files };
$self->_build_content
if ( $self->mtime < ( reverse( sort(@mtime) ) )[0] );
}

$env->{'psgix.assets_less'} ||= [];
my $url = '/_asset_less/' . $self->key . '.css';
push( @{ $env->{'psgix.assets_less'} }, $url );
Expand All @@ -61,8 +60,8 @@ sub serve {
200,
[ 'Content-Type' => 'text/css',
'Content-Length' => length( $self->_get_content ),
# 'Expires' =>
# HTTP::Date::time2str( time + ( $self->expires || 2592000 ) ),
'Expires' =>
HTTP::Date::time2str( time + ( $self->expires || 2592000 ) ),
],
[ $self->_get_content ]
];
Expand All @@ -72,47 +71,30 @@ sub _build_content {
my $self = shift;

my $content = join( "\n",
map { $self->_run_less($_) } @{ $self->files }
map { $self->_run_less($_) } @{ $self->files }
);

$self->_set_content($content);

return $content;
}

sub _set_content {
my $self = shift;
my $content = shift || croak "No content";

$self->key( md5_hex($content) );
$self->cache->set( $self->key, $content, $self->cache_ttl );

if(my $cache = $self->cache) {
$cache->set( $self->key, $content, $self->cache_ttl );
} else {
$self->_data($content);
}
return $content;
}

sub _get_content {
my $self = shift;

if(my $cache = $self->cache) {
my $content = $cache->get($self->key);
return $content if $content;
return $self->_build_content;
} else {
return $self->_data();
}
my $content = $self->cache->get($self->key);
return $content if $content;
return $self->_build_content;
}


sub _run_less {
my ($self, $file) = @_;

my ($stdout, $stderr, $exit) = capture {
system( 'lessc', $file );
};
croak $stderr if $stderr;
die $stderr if $stderr;
return minify($stdout);
}

Expand Down
17 changes: 15 additions & 2 deletions t/plack/mcless.t
@@ -1,16 +1,30 @@
use strict;

use Plack::App::File;
use Plack::Middleware::MCLess;

use Test::More;
use Plack::Test;
use Plack::Builder;
use HTTP::Request::Common;


BEGIN {
$ENV{PLACK_ENV} = 'development';
}

use Plack::Middleware::MCLess;

use CHI;
my $cache = CHI->new(
driver => 'FastMmap',
root_dir => '/tmp/',
cache_size => '100k'
);

my $app = builder {

enable "Plack::Middleware::MCLess",
cache => $cache,
root => "t/plack/css",
files => ['t/plack/css/style.less'];

Expand Down Expand Up @@ -45,7 +59,6 @@ test_psgi $app, sub {
is $res->content, "#header{color:#4d926f}h2{color:#4d926f}", "Content matches";
}


};

eval {
Expand Down

0 comments on commit 470e8e4

Please sign in to comment.