Adapter defaults

jnunemaker edited this page Nov 16, 2012 · 4 revisions

By default a few methods are included with each adapter. They can be overridden on a per adapter basis.

fetch

fetch reads the key. If it does not exist, you can provide a default or a block to be returned instead. You can write the value in the block.

adapter = Adapter[:memory].new({})
adapter.fetch('foo', 'yay') # 'yay'

adapter.fetch('foo') {
  'something'
} # 'something'

# for convenience, the key is yielded to the block
adapter.fetch('foo') { |key|
  value = 'some default'
  adapter.write(key, value)
  value} # 'some default' and sets key

key?(key)

Returns true if the key exists and is not nil or false if it is nil. Performs read(key) to check if the key is nil. If client has way of optimizing this you can override.

read_multiple(*keys)

Returns Hash of each key pointed at read values.