Class: Moneta::Adapters::Couch

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/couch.rb

Overview

CouchDB backend

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #decrement, #fetch, #increment

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Couch

Returns a new instance of Couch.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (String) — default: 'http://127.0.0.1:5984'

    Couch host

  • :db (String) — default: 'moneta'

    Couch database



13
14
15
16
17
# File 'lib/moneta/adapters/couch.rb', line 13

def initialize(options = {})
  options[:db] ||= 'moneta'
  options[:host] ||= '127.0.0.1:5984'
  @db = CouchRest.new(options[:host]).database!(options[:db])
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


56
57
58
59
# File 'lib/moneta/adapters/couch.rb', line 56

def clear(options = {})
  @db.recreate!
  self
end

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

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



47
48
49
50
51
52
53
# File 'lib/moneta/adapters/couch.rb', line 47

def delete(key, options = {})
  value = @db.get(key)
  @db.delete_doc('_id' => value['_id'], '_rev' => value['_rev'])
  value['value']
rescue ::RestClient::ResourceNotFound
  nil
end

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

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See ‘Moneta::Expires`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/moneta/adapters/couch.rb', line 20

def key?(key, options = {})
  @db.get(key) != nil
rescue ::RestClient::ResourceNotFound
  false
end

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

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See ‘Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



27
28
29
30
31
# File 'lib/moneta/adapters/couch.rb', line 27

def load(key, options = {})
  @db.get(key)['value']
rescue ::RestClient::ResourceNotFound
  nil
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See ‘Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/moneta/adapters/couch.rb', line 34

def store(key, value, options = {})
  doc = {'_id' => key, 'value' => value}
  begin
    doc['_rev'] = @db.get(key)['_rev']
  rescue ::RestClient::ResourceNotFound
  end
  @db.save_doc(doc)
  value
rescue ::RestClient::RequestFailed
  value
end