Class: Moneta::Adapters::KyotoCabinet

Inherits:
Memory
  • Object
show all
Defined in:
lib/moneta/adapters/kyotocabinet.rb

Overview

KyotoCabinet backend

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Instance Method Summary collapse

Methods included from CreateSupport

included

Methods included from IncrementSupport

included, #increment

Methods included from HashAdapter

#clear, #load, #store

Methods included from Defaults

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

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ KyotoCabinet

Returns a new instance of KyotoCabinet

Options Hash (options):

  • :file (String)

    Database file

  • :backend (::KyotoCabinet::DB)

    Use existing backend instance


11
12
13
14
15
16
17
18
19
20
# File 'lib/moneta/adapters/kyotocabinet.rb', line 11

def initialize(options = {})
  if options[:backend]
    @backend = options[:backend]
  else
    raise ArgumentError, 'Option :file is required' unless options[:file]
    @backend = ::KyotoCabinet::DB.new
    raise @backend.error.to_s unless @backend.open(options[:file],
                                                   ::KyotoCabinet::DB::OWRITER | ::KyotoCabinet::DB::OCREATE)
  end
end

Instance Method Details

#closeObject

Explicitly close the store


38
39
40
41
# File 'lib/moneta/adapters/kyotocabinet.rb', line 38

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)


33
34
35
# File 'lib/moneta/adapters/kyotocabinet.rb', line 33

def create(key, value, options = {})
  @backend.add(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


28
29
30
# File 'lib/moneta/adapters/kyotocabinet.rb', line 28

def delete(key, options = {})
  @backend.seize(key)
end

#each_keyEnumerator

Note:

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

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.


44
45
46
47
48
# File 'lib/moneta/adapters/kyotocabinet.rb', line 44

def each_key
  return enum_for(:each_key) unless block_given?
  @backend.each_key{ |arr| yield arr.first }
  self
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware


23
24
25
# File 'lib/moneta/adapters/kyotocabinet.rb', line 23

def key?(key, options = {})
  @backend.check(key) >= 0
end