Sha256: 69d25efbde43139ce19d65f6240b7d3be7d198c56295a59993e0e1b44da120c6

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: stores.rb 335 2005-03-31 14:02:02Z gmosx $

require 'fileutils'

require 'glue/hash'

module N

# Adds support for caching.

module Caching

	# Cached fragments are stored in memory.

	class MemoryStore < 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

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.15.0 lib/nitro/caching/stores.rb