class MuxTf::YamlCache
Public Class Methods
new(path, default_ttl:)
click to toggle source
# File lib/mux_tf/yaml_cache.rb, line 5 def initialize(path, default_ttl:) @default_ttl = default_ttl @store = YAML::Store.new path end
Public Instance Methods
fetch(key, ttl: @default_ttl) { || ... }
click to toggle source
# File lib/mux_tf/yaml_cache.rb, line 19 def fetch(key, ttl: @default_ttl) info = nil @store.transaction(true) do info = @store[key] end if info.nil? || info[:expires_at] < Time.now if block_given? value = yield set(key, value, ttl: ttl) return value else raise KeyError, info.nil? ? "no value at key: #{key}" : "value expired at key: #{key}" end end info[:value] end
set(key, value, ttl: @default_ttl)
click to toggle source
# File lib/mux_tf/yaml_cache.rb, line 10 def set(key, value, ttl: @default_ttl) @store.transaction do @store[key] = { expires_at: Time.now + ttl, value: value } end end