Sha256: 596246555b5d97232abc8dd2ee6421062bc27c923df0f1a8735aa5955d56790e

Contents?: true

Size: 1.43 KB

Versions: 9

Compression:

Stored size: 1.43 KB

Contents

module Ramaze
  # Persist cache contents to the filesystem.
  # By default this will create a `/cache` directory in your APPDIR
  #
  # Usage for sessions only:
  #
  #     Ramaze::Global::cache_alternative[:sessions] = Ramaze::FileCache
  #
  # Usage for everything:
  #
  #     Ramaze::Global::cache = Ramaze::FileCache

  class FileCache
    attr_accessor :root, :subdir
    attr_reader :host, :pid

    def initialize(root = Ramaze::Global.root, subdir = 'cache')
      @root, @subdir = root, subdir
      @host = Socket.gethostname
      @pid = $$

      FileUtils.mkdir_p(dir)
    end

    def dir(*further)
      File.join(root, subdir, *further)
    end

    def [](key)
      Marshal.load(File.read(dir(key.to_s, 'data')))
    rescue
      nil
    end

    def []=(key, value)
      key = key.to_s
      tmp_name = dir(key, "data.#{host}.#{pid}")
      key_name = dir(key, 'data')
      dir_name = dir(key)

      data = Marshal.dump(value)

      FileUtils.rm_rf(dir_name)
      FileUtils.mkdir_p(dir_name)

      File.open(tmp_name, 'w'){|fd| fd.write(data) }

      FileUtils.mv(tmp_name, key_name)

      return value
    end

    def values_at(*keys)
      keys.map{|key| self[key] }
    end

    def delete(*keys)
      keys.map do |key|
        FileUtils.rm_rf(dir(key.to_s))
      end
    end

    def clear
      Dir[dir('*')].each{|entry| FileUtils.rm_rf(entry) }
    end

    def to_sym
      name.split('::').last.to_sym
    end
  end
end

Version data entries

9 entries across 9 versions & 4 rubygems

Version Path
Pistos-ramaze-2008.12 lib/ramaze/cache/file.rb
Pistos-ramaze-2009.01 lib/ramaze/cache/file.rb
Pistos-ramaze-2009.02 lib/ramaze/cache/file.rb
manveru-ramaze-2008.12 lib/ramaze/cache/file.rb
manveru-ramaze-2009.01 lib/ramaze/cache/file.rb
ptomato-ramaze-2009.02.1 lib/ramaze/cache/file.rb
ptomato-ramaze-2009.02 lib/ramaze/cache/file.rb
ramaze-2009.01 lib/ramaze/cache/file.rb
ramaze-2009.02 lib/ramaze/cache/file.rb