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/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'
|
||||||
|
@ -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
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
require 'active_support/cache'
|
require 'active_support/cache'
|
||||||
|
|
||||||
module RablFastJson
|
module RablRails
|
||||||
class Fragment
|
class Fragment
|
||||||
|
|
||||||
attr_reader :compiled_source, :options
|
attr_reader :compiled_source, :options
|
||||||
|
|
||||||
def initialize(key, source, options = {})
|
def initialize(key, source, options = {})
|
||||||
@ -12,16 +11,18 @@ module RablFastJson
|
|||||||
end
|
end
|
||||||
|
|
||||||
def expand_cache_key(data)
|
def expand_cache_key(data)
|
||||||
@key.call(data)
|
if @key
|
||||||
end
|
@key.respond_to?(:call) ? @key.call(data) : @key
|
||||||
|
else
|
||||||
def fetch_and_render
|
ActiveSupport::Cache.expand_cache_key(data, 'rabl')
|
||||||
Rails.cache.fetch(key)
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# cache key: ->(resource) { |resource| resource.cache_key }, expires_in: 1800
|
||||||
|
#
|
||||||
# def cache(options = {}, &block)
|
# def cache(options = {}, &block)
|
||||||
# return unless block_given?
|
# return unless block_given?
|
||||||
# key = options.delete(:key)
|
# key = options.delete(:key)
|
@ -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
|
||||||
|
@ -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
|
4
test/fragment_cache_test.rb
Normal file
4
test/fragment_cache_test.rb
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class FragmentCacheTest < ActiveSupport::TestCase
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user