Class: Ramaze::YAMLStoreCache

Inherits:
Object
  • Object
show all
Defined in:
lib/ramaze/cache/yaml_store.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = 'cache.yaml') ⇒ YAMLStoreCache

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



17
18
19
20
# File 'lib/ramaze/cache/yaml_store.rb', line 17

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

catch everything else and use a transaction to send it.



62
63
64
65
66
# File 'lib/ramaze/cache/yaml_store.rb', line 62

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

Instance Attribute Details

#fileObject

Returns the value of attribute file.



12
13
14
# File 'lib/ramaze/cache/yaml_store.rb', line 12

def file
  @file
end

Instance Method Details

#clearObject

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



38
39
40
41
42
# File 'lib/ramaze/cache/yaml_store.rb', line 38

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

#delete(key) ⇒ Object

Deletes the key from YAML::Store based cache.



46
47
48
49
50
# File 'lib/ramaze/cache/yaml_store.rb', line 46

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

#transaction(&block) ⇒ Object

just a helper to use transactions.



54
55
56
57
58
# File 'lib/ramaze/cache/yaml_store.rb', line 54

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

#underlying_yamlObject

Loads @file into memory via YAML::load_file



32
33
34
# File 'lib/ramaze/cache/yaml_store.rb', line 32

def underlying_yaml
  YAML.load_file(@file)
end

#values_at(*keys) ⇒ Object

return the values for given keys.



24
25
26
27
28
# File 'lib/ramaze/cache/yaml_store.rb', line 24

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