class Awssh::Cache
Attributes
data[R]
Public Class Methods
instance()
click to toggle source
# File lib/awssh/cache.rb, line 10 def instance raise 'cache not loaded?' unless @instance.data @instance end
load(file)
click to toggle source
# File lib/awssh/cache.rb, line 6 def load(file) @instance = new(file) end
new(file, expires)
click to toggle source
# File lib/awssh/cache.rb, line 18 def initialize(file, expires) if file @file = File.expand_path(file) else @disabled = true @file = nil end @expires = expires @data = load end
Public Instance Methods
fetch(key, force) { || ... }
click to toggle source
# File lib/awssh/cache.rb, line 40 def fetch(key, force) if force || @disabled diff = Time.now.to_i else time = @data[key] ? @data[key][:time] : 0 diff = Time.now.to_i - time end if diff > @expires value = yield write(key, value) return value else read(key) end end
read(key)
click to toggle source
# File lib/awssh/cache.rb, line 36 def read(key) @data[key][:value] end
write(key, value)
click to toggle source
# File lib/awssh/cache.rb, line 29 def write(key, value) time = Time.now.to_i data = {time: time, value: value} @data[key] = data save end
Private Instance Methods
load()
click to toggle source
# File lib/awssh/cache.rb, line 58 def load return {} if @disabled unless File.exists?(@file) @data = {} save end YAML.load_file(@file) end
save()
click to toggle source
# File lib/awssh/cache.rb, line 67 def save return if @disabled File.open(@file, "w+") {|f| f.write(@data.to_yaml)} end