First implementation of fragment cache
This commit is contained in:
parent
4cf10fb8f6
commit
4dae04896d
|
@ -6,6 +6,7 @@ require 'active_support/core_ext/class/attribute_accessors'
|
|||
|
||||
require 'rabl-rails/version'
|
||||
require 'rabl-rails/template'
|
||||
require 'rabl-rails/fragment'
|
||||
require 'rabl-rails/compiler'
|
||||
|
||||
require 'rabl-rails/renderer'
|
||||
|
|
|
@ -6,6 +6,7 @@ module RablRails
|
|||
class Compiler
|
||||
def initialize
|
||||
@glue_count = 0
|
||||
@cache_count = 0
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -125,6 +126,17 @@ module RablRails
|
|||
@template.merge!(t.source)
|
||||
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
|
||||
|
||||
#
|
||||
|
@ -148,11 +160,11 @@ module RablRails
|
|||
end
|
||||
end
|
||||
|
||||
def sub_compile(data)
|
||||
def sub_compile(data = nil)
|
||||
return {} unless block_given?
|
||||
old_template, @template = @template, {}
|
||||
yield
|
||||
@template.merge!(:_data => data)
|
||||
data ? @template.merge!(:_data => data) : @template
|
||||
ensure
|
||||
@template = old_template
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
require 'active_support/cache'
|
||||
|
||||
module RablFastJson
|
||||
module RablRails
|
||||
class Fragment
|
||||
|
||||
attr_reader :compiled_source, :options
|
||||
|
||||
def initialize(key, source, options = {})
|
||||
|
@ -12,16 +11,18 @@ module RablFastJson
|
|||
end
|
||||
|
||||
def expand_cache_key(data)
|
||||
@key.call(data)
|
||||
end
|
||||
|
||||
def fetch_and_render
|
||||
Rails.cache.fetch(key)
|
||||
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)
|
|
@ -62,6 +62,12 @@ module RablRails
|
|||
else # child
|
||||
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
|
||||
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
|
||||
output[key] = out
|
||||
output
|
||||
|
|
|
@ -149,4 +149,14 @@ class CompilerTest < ActiveSupport::TestCase
|
|||
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
|
||||
assert_equal false, t.data
|
||||
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
|
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class FragmentCacheTest < ActiveSupport::TestCase
|
||||
end
|
Loading…
Reference in New Issue