Class: Mtodos::Cache
- Inherits:
-
Object
- Object
- Mtodos::Cache
- Defined in:
- lib/mtodos.rb
Overview
Local cache
Defined Under Namespace
Classes: NotFound
Instance Method Summary collapse
- #get(key) ⇒ Object
- #having?(key) ⇒ Boolean
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #set(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/mtodos.rb', line 9 def initialize @cache_filename = 'mtodos.cache' @cache_file = File.join(Dir.pwd, @cache_filename) if File.file?(@cache_file) @hash = Marshal.load(File.read(@cache_file)) else @hash = {} File.write(@cache_file, Marshal.dump(@hash)) end end |
Instance Method Details
#get(key) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/mtodos.rb', line 27 def get(key) begin @hash = Marshal.load(File.read(@cache_file)) rescue Errno::ENOENT @hash = {} end having?(key) end |
#having?(key) ⇒ Boolean
36 37 38 39 40 41 42 |
# File 'lib/mtodos.rb', line 36 def having?(key) if @hash[key] true else fail NotFound, 'NotFound key' end end |
#set(key, value) ⇒ Object
22 23 24 25 |
# File 'lib/mtodos.rb', line 22 def set(key, value) @hash[key] = value File.write(@cache_file, Marshal.dump(@hash)) end |