Sha256: 57e9e0388b95621dd1cf16896b94638296c72204110342e2708d1033f1bc96b7

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: stores.rb 9 2005-04-13 00:08:20Z nasis $

require 'fileutils'

require 'glue/hash'

module Nitro

# Adds support for caching.

module Caching

	# Cached fragments are stored in memory.

	class MemoryStore < Glue::SafeHash

		def read(name, options = {})
			self[name]
		end
		
		def write(name, content = '', options = {})
			self[name] = content
		end

		def delete(name, options = {}) 
			self.delete(name)
		end

	end

	# Cached fragments are stored as html files
	# on the filesystem.

	class FileStore
		cattr_accessor :cache_root, 'cache'
		
		def initialize(cache_root = FileStore.cache_root)
			@cache_root = cache_root
		end
		
		def read(name, options = {})
			begin
				IO.read(path_for_name(name))
			rescue
				nil
			end
		end

		def write(name, content = '', options = {})
			begin
				path = path_for_name(name)
				dir = File.dirname(path)
				FileUtils.makedirs(dir) unless File.exists?(dir)
				
				File.open(path, 'w+') { |f| f.write(content) }
			rescue
				Logger.error "Could not save cached file '#{path}'"
			end
		end

		def delete(name, options = {})
			path = path_for_name(name)
			File.delete(path) if File.exist?(path)
		end
		
		private

		def path_for_name(name)
			"#@cache_root/#{name}"
		end

	end

	class DrbStrore
	end
	
	class MemcacheStore
	end

end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nitro-0.16.0 lib/nitro/caching/stores.rb
nitro-0.17.0 lib/nitro/caching/stores.rb
nitro-0.18.0 lib/nitro/caching/stores.rb
nitro-0.18.1 lib/nitro/caching/stores.rb
nitro-0.19.0 lib/nitro/caching/stores.rb
nitro-0.20.0 lib/nitro/caching/stores.rb