Compare commits

...

3 Commits

Author SHA1 Message Date
ccocchi
4dae04896d First implementation of fragment cache 2012-05-28 10:56:53 +02:00
ccocchi
4cf10fb8f6 Merge branch 'master' into fragment-cache 2012-05-04 16:05:11 +02:00
ccocchi
79d6bb9c81 Basic fragment class and renderer code. Need to be tested 2012-04-16 00:38:02 +02:00
6 changed files with 77 additions and 2 deletions

View File

@ -6,6 +6,7 @@ require 'active_support/core_ext/class/attribute_accessors'
require 'rabl-rails/version' require 'rabl-rails/version'
require 'rabl-rails/template' require 'rabl-rails/template'
require 'rabl-rails/fragment'
require 'rabl-rails/compiler' require 'rabl-rails/compiler'
require 'rabl-rails/renderer' require 'rabl-rails/renderer'

View File

@ -6,6 +6,7 @@ module RablRails
class Compiler class Compiler
def initialize def initialize
@glue_count = 0 @glue_count = 0
@cache_count = 0
end end
# #
@ -124,6 +125,17 @@ module RablRails
t = Library.instance.get(path) t = Library.instance.get(path)
@template.merge!(t.source) @template.merge!(t.source)
end end
#
# cache key: ->(resource) { |resource| resource.cache_key }, expires_in: 1800
#
def cache(options = {}, &block)
return unless block_given?
key = options.delete(:key)
source = sub_compile { yield }
@template[:"_cache#{@cache_count}"] = Fragment.new(key, source, options)
@cache_count += 1
end
protected protected
@ -148,11 +160,11 @@ module RablRails
end end
end end
def sub_compile(data) def sub_compile(data = nil)
return {} unless block_given? return {} unless block_given?
old_template, @template = @template, {} old_template, @template = @template, {}
yield yield
@template.merge!(:_data => data) data ? @template.merge!(:_data => data) : @template
ensure ensure
@template = old_template @template = old_template
end end

View File

@ -0,0 +1,42 @@
require 'active_support/cache'
module RablRails
class Fragment
attr_reader :compiled_source, :options
def initialize(key, source, options = {})
@key = key
@compiled_source = source
@options = options
end
def expand_cache_key(data)
if @key
@key.respond_to?(:call) ? @key.call(data) : @key
else
ActiveSupport::Cache.expand_cache_key(data, 'rabl')
end
end
end
end
#
# cache key: ->(resource) { |resource| resource.cache_key }, expires_in: 1800
#
# def cache(options = {}, &block)
# return unless block_given?
# key = options.delete(:key)
# source = sub_compiler { compile_block(&block) }
# @template[:"_cache#{cache_count}"] = Fragment.new(key, source, options)
# end
#
# when Fragment
# render_fragment(value)
#
# def render_fragment(data, fragment)
# Rails.cache.fetch(fragment.expand_cache_key(data), fragment.options) do
# render_resource(data, fragment.compiled_source)
# end
# end

View File

@ -62,6 +62,12 @@ module RablRails
else # child else # child
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value) object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
end end
when Fragment
cache_output = Rails.cache.fetch(value.expand_cache_key(data), value.options) do
data.respond_to?(:each) ? render_collection(data, value.compiled_source) : render_resource(data, value.compiled_source)
end
output.merge!(cache_output)
next output
end end
output[key] = out output[key] = out
output output

View File

@ -149,4 +149,14 @@ class CompilerTest < ActiveSupport::TestCase
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source) assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
assert_equal false, t.data assert_equal false, t.data
end end
test "cache are compiled to fragment" do
t = @compiler.compile_source(%{
cache do
attributes :name
end
})
assert_instance_of RablRails::Fragment, t.source[:_cache0]
assert_equal({ :name => :name }, t.source[:_cache0].compiled_source)
end
end end

View File

@ -0,0 +1,4 @@
require 'test_helper'
class FragmentCacheTest < ActiveSupport::TestCase
end