Sha256: 86fed6073b292f2dadf77a2b65860023b71beed3dbde001612d5477659a3b3e7

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require 'fileutils'

require 'mega/synchash'

require 'glue/attribute'

module Nitro

# Adds support for caching.

module Caching

  # Cached fragments are stored in memory.

  class MemoryStore < SyncHash

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

    alias_method :old_delete, :delete
    
    def delete(name, options = {}) 
      old_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

  #--
  # TODO: implement me
  #++
  
  class DrbStrore
  end

  #--
  # TODO: implement me
  #++
  
  class MemcacheStore
  end

end

end

# * George Moschovitis  <gm@navel.gr>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.26.0 lib/nitro/caching/stores.rb
nitro-0.27.0 lib/nitro/caching/stores.rb