lib/nitro/caching/fragments.rb in nitro-0.27.0 vs lib/nitro/caching/fragments.rb in nitro-0.28.0

- old
+ new

@@ -1,51 +1,65 @@ require 'fileutils' +require 'glue/configuration' require 'glue/attribute' -require 'nitro/caching/stores' +require 'glue/cache/memory' module Nitro # Adds support for caching. module Caching # Action caching. Caches a fragment, ie a page part. - # Use output cachingfor full page caching. + # Use output caching for full page caching. module Fragments - @@store = FileStore.new # MemoryStore.new - - def self.store - @@store - end + # The cache used to store the fragments. - def self.store=(store) - @@store = store - end + setting :cache, :default => nil, :doc => 'The cache used to store the fragments' + #-- + # gmosx, FIXME: this is a hack, improve setting + # implementation. + #++ + + @@cache = Glue::MemoryCache.new + def self.get(name, options = {}) - return @@store.read(name, options) + return @@cache.get(name, options) end def self.put(name, content = nil, options = {}) - @@store.write(name, content, options) + @@cache.put(name, content, options) return content end - def self.append_features(base) # :nodoc: - super - end - private + # Helper method to cache a fragment. + # + # === Example + # + # ... + # <h1>Article list</h1> + # + # <?r cache(:my_cache_key) do ?> + # <ul> + # <li each="a in Article.all">#{a.title}</li> + # </ul> + # <?r end ?> + # ... + def cache(name = nil, options = {}, &block) name = @action_name unless name cache_fragment(block, "#{name}#{options}", options) end + # Internal method, prefer to use cache instead. + def cache_fragment(block, name, options = {}) unless caching_enabled? block.call return end @@ -57,16 +71,17 @@ block.call Fragments.put(name, @out[pos..-1], options) end end + # Expire a cached fragment. #-- # gmosx: If you modify the code here, don't forget to modify # the same method on the sweeper. #++ def expire_fragment(name, options = {}) - Fragments.store.delete(name, options) + Fragments.cache.delete(name, options) end end end