Sha256: 6c10679a5d7fe548d93dc2aee4d917210e8a55cbc95877dac965bac42ae3485e

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: fragments.rb 1 2005-04-11 11:04:30Z gmosx $

require 'fileutils'

require 'glue/attribute'
require 'nitro/caching/stores'

module Nitro

# Adds support for caching.

module Caching

	# Action caching.

	module Fragments

		@@store = FileStore.new # MemoryStore.new

		def self.store 
			@@store 
		end
		
		def self.store=(store) 
			@@store = store
		end
		
		def self.get(name, options = {})
			return @@store.read(name, options)
		end
		
		def self.put(name, content = nil, options = {})
			@@store.write(name, content, options)
			return content
		end
		
		def self.append_features(base) # :nodoc: 
			super
		end

		private

		def cache(name = nil, options = {}, &block)
			name = @action_name unless name	
			cache_fragment(block, "#{name}#{options}", options)
		end

		def cache_fragment(block, name, options = {})
			unless caching_enabled
				block.call
				return
			end

			if fragment = Fragments.get(name, options)
				@out << fragment
			else
				pos = @out.length				
				block.call
				Fragments.put(name, @out[pos..-1], options)
			end
		end

		def expire_fragment(name, options = {})
			Fragments.store.delete(name, options)
		end
	end
	
end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nitro-0.16.0 lib/nitro/caching/fragments.rb
nitro-0.17.0 lib/nitro/caching/fragments.rb
nitro-0.18.0 lib/nitro/caching/fragments.rb
nitro-0.18.1 lib/nitro/caching/fragments.rb