Class: Moneta::Adapters::TokyoCabinet

Inherits:
Object
  • Object
show all
Includes:
CreateSupport, Defaults, EachKeySupport, HashAdapter, IncrementSupport
Defined in:
lib/moneta/adapters/tokyocabinet.rb

Overview

TokyoCabinet backend

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Instance Method Summary collapse

Methods included from EachKeySupport

#each_key, included

Methods included from CreateSupport

included

Methods included from IncrementSupport

included, #increment

Methods included from HashAdapter

#clear, #fetch_values, #key?, #load, #merge!, #slice, #store, #values_at

Methods included from Defaults

#[], #[]=, #decrement, #each_key, #features, #fetch, #fetch_values, included, #increment, #key?, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ TokyoCabinet

Returns a new instance of TokyoCabinet

Options Hash (options):

  • :file (String)

    Database file

  • :type (Symbol) — default: :hdb

    Database type (:bdb and :hdb possible)

  • :backend (::TokyoCabinet::*DB)

    Use existing backend instance



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/moneta/adapters/tokyocabinet.rb', line 18

def initialize(options = {})
  if options[:backend]
    @backend = options[:backend]
  else
    raise ArgumentError, 'Option :file is required' unless options[:file]
    if options[:type] == :bdb
      @backend = ::TokyoCabinet::BDB.new
      @backend.open(options[:file], ::TokyoCabinet::BDB::OWRITER | ::TokyoCabinet::BDB::OCREAT)
    else
      @backend = ::TokyoCabinet::HDB.new
      @backend.open(options[:file], ::TokyoCabinet::HDB::OWRITER | ::TokyoCabinet::HDB::OCREAT)
    end or raise @backend.errmsg(@backend.ecode)
  end
end

Instance Method Details

#closeObject

Explicitly close the store



48
49
50
51
# File 'lib/moneta/adapters/tokyocabinet.rb', line 48

def close
  @backend.close
  nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically sets a key to value if it's not set.

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)



43
44
45
# File 'lib/moneta/adapters/tokyocabinet.rb', line 43

def create(key, value, options = {})
  @backend.putkeep(key, value)
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware



34
35
36
37
38
39
40
# File 'lib/moneta/adapters/tokyocabinet.rb', line 34

def delete(key, options = {})
  value = load(key, options)
  if value
    @backend.delete(key)
    value
  end
end