Sha256: 8d4ba2bc829ef126792b9adcb7e2b782888b7274189e3d54405211b7d7457c84

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

require 'fileutils'

require 'mega/synchash'

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

    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

# * George Moschovitis  <gm@navel.gr>

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nitro-0.23.0 lib/nitro/caching/stores.rb
nitro-0.24.0 lib/nitro/caching/stores.rb
nitro-0.25.0 lib/nitro/caching/stores.rb