Class: Rotten::SimpleCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rotten/simple_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SimpleCache

Returns a new instance of SimpleCache.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rotten/simple_cache.rb', line 4

def initialize options={}
  require "yaml"
  if options[:cache_path] && File.exists?(options[:cache_path])
    @cache_file = options[:cache_path]
    @data = YAML.load_file @cache_file || {}
  else
    require "tempfile"
    @cache_file = Tempfile.new "__rotten"
    @data = YAML.load_file(@cache_file) || {}
  end
end

Instance Attribute Details

#cache_fileObject (readonly)

Returns the value of attribute cache_file.



3
4
5
# File 'lib/rotten/simple_cache.rb', line 3

def cache_file
  @cache_file
end

Instance Method Details

#read(key) ⇒ Object



16
17
18
# File 'lib/rotten/simple_cache.rb', line 16

def read key
  @data[key.to_s]
end

#write(key, value) ⇒ Object



20
21
22
23
# File 'lib/rotten/simple_cache.rb', line 20

def write key, value
  @data[key.to_s] = value
  save_to_disk
end