Sha256: 361027c5e4a7d1fb9db0e194eb89441b26092ed3fe5f608172c040d39375604c

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

#          Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.
require 'yaml/store'

module Ramaze

  # Cache based on _whys YAML::Store, which uses PStore to serialize objects
  # as YAML in a thread-safe manner.

  class YAMLStoreCache

    attr_accessor :file

    # create a new YAML::Store with the given file (which will be created if it
    # is not already there).

    def initialize(file = 'cache.yaml')
      @file = file
      @cache = YAML::Store.new(file)
    end

    # return the values for given keys.

    def values_at(*keys)
      transaction do |y|
        keys.map{|k| y[k]}
      end
    end

    # Loads @file into memory via YAML::load_file

    def underlying_yaml
      YAML.load_file(@file)
    end

    # clears the YAML::Store based cache, by emptying the YAML file.

    def clear
      transaction do |y|
        File.open(@file, 'w+'){|f| f.puts({}.to_yaml)}
      end
    end

    # Deletes the key from YAML::Store based cache.

    def delete(key)
      transaction do |y|
        y.delete(key)
      end
    end

    # just a helper to use transactions.

    def transaction(&block)
      @cache.transaction do
        yield(@cache)
      end
    end

    # catch everything else and use a transaction to send it.

    def method_missing(*args, &block)
      transaction do |y|
        y.send(*args, &block)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ramaze-0.1.3 lib/ramaze/cache/yaml_store.rb
ramaze-0.1.4 lib/ramaze/cache/yaml_store.rb
ramaze-0.2.1 lib/ramaze/cache/yaml_store.rb
ramaze-0.2.0 lib/ramaze/cache/yaml_store.rb