class Librarian::Environment::RuntimeCache

Attributes

data[RW]

Public Class Methods

new() click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 46
def initialize
  self.data = {}
end

Public Instance Methods

[](keyspace, key) click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 75
def [](keyspace, key)
  get(keyspace, key)
end
[]=(keyspace, key, value) click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 79
def []=(keyspace, key, value)
  put(keyspace, key, value)
end
delete(keyspace, key) click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 62
def delete(keyspace, key)
  data.delete(combined_key(keyspace, key))
end
get(keyspace, key) click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 54
def get(keyspace, key)
  data[combined_key(keyspace, key)]
end
include?(keyspace, key) click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 50
def include?(keyspace, key)
  data.include?(combined_key(keyspace, key))
end
keyspace(keyspace) click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 83
def keyspace(keyspace)
  KeyspaceCache.new(self, keyspace)
end
memo(keyspace, key) { || ... } click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 66
def memo(keyspace, key)
  put(keyspace, key, yield) unless include?(keyspace, key)
  get(keyspace, key)
end
once(keyspace, key) { || ... } click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 71
def once(keyspace, key)
  memo(keyspace, key) { yield ; nil }
end
put(keyspace, key, value = nil) { |: value| ... } click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 58
def put(keyspace, key, value = nil)
  data[combined_key(keyspace, key)] = block_given? ? yield : value
end

Private Instance Methods

combined_key(keyspace, key) click to toggle source
# File lib/librarian/environment/runtime_cache.rb, line 91
def combined_key(keyspace, key)
  keyspace.kind_of?(String) or raise Error, "keyspace must be a string"
  keyspace.size > 0 or raise Error, "keyspace must not be empty"
  keyspace.size < 2**16 or raise Error, "keyspace must not be too large"
  key.kind_of?(String) or raise Error, "key must be a string"
  [keyspace.size.to_s(16).rjust(4, "0"), keyspace, key].join
end