Parent

Class/Module Index [+]

Quicksearch

Volatile::DB

The main DB class. This is the public API for the library.

Public Class Methods

new(path) click to toggle source

Initializes storage to the given path.

# File lib/volatiledb.rb, line 17
def initialize(path)
  @raw ||= Volatile::Raw.new(path)
  @db  ||= {}
end

Public Instance Methods

fetch(key) click to toggle source

Works the same as get, but will fire the action defined for the given key after the timeout defined for that key has elapsed.

Returns nil for a key which has not had any action defined for it yet.

# File lib/volatiledb.rb, line 55
def fetch(key)
  checking key do |k|
    item = @db[k]
    delta = now - item[:last_access]
    delta > item[:timeout] ? sync(k) : get(k)
  end
end
get(key) click to toggle source

Read the data for the given key from storage. If the underlying storage has disappeared, it will be re-initialized by calling the action defined for that key again. After the storage has been re-initialized, its value will be read and returned.

Returns nil for a key which has not had any action defined for it yet.

# File lib/volatiledb.rb, line 40
def get(key)
  data = raw_get key
  if data.nil?
    sync key
  else
    data
  end
end
pull(key) click to toggle source

Works the same as fetch, but ignores timeout and calls the defined action every time. Should be used sparingly as it re-initializes storage on every call.

Returns nil for a key which has not had any action defined for it yet.

# File lib/volatiledb.rb, line 70
def pull(key)
  sync key
end
put(key, timeout=3, &action) click to toggle source

Define an action for a given key. The action should return a String. A timeout in seconds may optionally be defined. Default is 3 seconds.

# File lib/volatiledb.rb, line 26
def put(key, timeout=3, &action)
  populate_db key, timeout, action
  raw_put key
  key
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.