Module: Keystok::Cache
- Included in:
- Client
- Defined in:
- lib/keystok/cache.rb
Overview
Module for handling cache write and read operations
Instance Method Summary collapse
- #cache_file_exist? ⇒ Boolean
- #cache_file_path(tmp_dir = nil) ⇒ Object
- #cache_stream(method = :read) ⇒ Object
- #load_cache(iostream = nil) ⇒ Object
- #write_cache(cache_data, iostream = nil) ⇒ Object
Instance Method Details
#cache_file_exist? ⇒ Boolean
14 15 16 |
# File 'lib/keystok/cache.rb', line 14 def cache_file_exist? File.exist?(cache_file_path) end |
#cache_file_path(tmp_dir = nil) ⇒ Object
9 10 11 12 |
# File 'lib/keystok/cache.rb', line 9 def cache_file_path(tmp_dir = nil) tmp_dir ||= (@config && @config[:tmp_dir]) ? @config[:tmp_dir] : '.' @cache_file_path ||= File.join(tmp_dir, 'keystok_cache.data') end |
#cache_stream(method = :read) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/keystok/cache.rb', line 18 def cache_stream(method = :read) case method when :write mode = 'wb' else mode = 'r' end File.open(cache_file_path, mode) end |
#load_cache(iostream = nil) ⇒ Object
28 29 30 31 32 |
# File 'lib/keystok/cache.rb', line 28 def load_cache(iostream = nil) Keystok.logger.warn('Loading data from cache') iostream ||= cache_stream iostream.read end |
#write_cache(cache_data, iostream = nil) ⇒ Object
34 35 36 37 38 |
# File 'lib/keystok/cache.rb', line 34 def write_cache(cache_data, iostream = nil) iostream ||= cache_stream(:write) iostream.write(cache_data) iostream.flush end |