Class: Regentanz::Cache::File
Overview
Implements file-based caching. Cache files are stored in Regentanz.configuration.cache_dir
and are prefixed with Regentanz.configuration.cache_prefix
.
Instance Method Summary collapse
-
#available?(key) ⇒ Boolean
Cache is available (n.b. not necessarily #valid?) if a file exists for
key
. -
#expire!(key) ⇒ Object
Unlinks the cache file for
key
. - #filename(key) ⇒ Object
-
#get(key) ⇒ Object
Retrieves content of #filename for
key
. -
#set(key, value) ⇒ Object
Stores
value
in #filename forkey
. -
#set_retry_state! ⇒ Object
Persists the timeout state by writing a retry_marker file.
-
#unset_retry_state! ⇒ Object
Checks if we’ve waited long enough.
- #valid?(key) ⇒ Boolean
-
#waiting_for_retry? ⇒ Boolean
Returns whether or not weather retrieval from the API is currently waiting for a timeout to expire; here: existence of a retry marker file.
Methods inherited from Base
Instance Method Details
#available?(key) ⇒ Boolean
Cache is available (n.b. not necessarily #valid?) if a file exists for key
12 13 14 |
# File 'lib/regentanz/cache/file.rb', line 12 def available?(key) ::File.exists?(filename(key)) rescue nil end |
#expire!(key) ⇒ Object
Unlinks the cache file for key
17 18 19 20 21 22 23 24 |
# File 'lib/regentanz/cache/file.rb', line 17 def expire!(key) return false unless available?(key) begin ::File.delete(filename(key)) true rescue end end |
#filename(key) ⇒ Object
26 27 28 |
# File 'lib/regentanz/cache/file.rb', line 26 def filename(key) ::File.join(Regentanz.configuration.cache_dir, "#{Regentanz.configuration.cache_prefix}_#{key}.xml") end |
#get(key) ⇒ Object
Retrieves content of #filename for key
31 32 33 34 35 |
# File 'lib/regentanz/cache/file.rb', line 31 def get(key) if available?(key) ::File.open(filename(key), "r") { |file| file.read } rescue nil end end |
#set(key, value) ⇒ Object
Stores value
in #filename for key
38 39 40 41 42 43 44 |
# File 'lib/regentanz/cache/file.rb', line 38 def set(key, value) begin ::File.open(filename(key), "w") { |file| file.puts value } filename(key) rescue end end |
#set_retry_state! ⇒ Object
Persists the timeout state by writing a retry_marker file
77 78 79 80 |
# File 'lib/regentanz/cache/file.rb', line 77 def set_retry_state! ::File.open(Regentanz.configuration.retry_marker, "w+").close waiting_for_retry? end |
#unset_retry_state! ⇒ Object
Checks if we’ve waited long enough. Deletes a possible retry marker file (and returns true) if so or returns false if not
68 69 70 71 72 73 74 |
# File 'lib/regentanz/cache/file.rb', line 68 def unset_retry_state! marker = Regentanz.configuration.retry_marker if waiting_for_retry? and ::File.new(marker).mtime < Regentanz.configuration.retry_ttl.seconds.ago ::File.delete(marker) if ::File.exists?(marker) true end end |
#valid?(key) ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/regentanz/cache/file.rb', line 46 def valid?(key) return false unless available?(key) begin # TODO delegate XML parsing and verification doc = REXML::Document.new(get(key)) node = doc.elements["xml_api_reply/weather/forecast_information/current_date_time"] time = node.attribute("data").to_s.to_time if node time > Regentanz.configuration.cache_ttl.seconds.ago rescue # TODO pass exception upstream until properly delegated in the first place? end end |
#waiting_for_retry? ⇒ Boolean
Returns whether or not weather retrieval from the API is currently waiting for a timeout to expire; here: existence of a retry marker file
62 63 64 |
# File 'lib/regentanz/cache/file.rb', line 62 def waiting_for_retry? ::File.exists?(Regentanz.configuration.retry_marker) end |