Class: Rotten::SimpleCache
- Inherits:
-
Object
- Object
- Rotten::SimpleCache
- Defined in:
- lib/rotten/simple_cache.rb
Instance Attribute Summary collapse
-
#cache_file ⇒ Object
readonly
Returns the value of attribute cache_file.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ SimpleCache
constructor
A new instance of SimpleCache.
- #read(key) ⇒ Object
- #write(key, value) ⇒ Object
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 ={} require "yaml" if [:cache_path] && File.exists?([:cache_path]) @cache_file = [: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_file ⇒ Object (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 |