PERF: speed up css precompilation by caching

This commit is contained in:
Sam 2014-03-04 16:54:58 +11:00
parent 28f84ef0e1
commit f602c568e2
1 changed files with 32 additions and 12 deletions

View File

@ -18,20 +18,40 @@ task 'assets:precompile:before' do
require 'digest/sha1'
module ::Sprockets
class UglifierCompressor
def evaluate(context, locals, &block)
digest = Digest::SHA1.hexdigest(data)
key = "SPROCKETS_#{digest}"
if compiled = $redis.get(key)
$redis.expire(key, 1.week)
else
compiled = Uglifier.new(:comments => :none).compile(data)
$redis.setex(key, 1.week, compiled)
end
compiled
def self.cache_compiled(type, data)
digest = Digest::SHA1.hexdigest(data)
key = "SPROCKETS_#{type}_#{digest}"
if compiled = $redis.get(key)
$redis.expire(key, 1.week)
else
compiled = yield
$redis.setex(key, 1.week, compiled)
end
compiled
end
class SassCompressor
def evaluate(context, locals, &block)
::Sprockets.cache_compiled("sass", data) do
::Sass::Engine.new(data, {
:syntax => :scss,
:cache => false,
:read_cache => false,
:style => :compressed
}).render
end
end
end
class UglifierCompressor
def evaluate(context, locals, &block)
::Sprockets.cache_compiled("uglifier", data) do
Uglifier.new(:comments => :none).compile(data)
end
end
end
end