Class: Moneta::Adapters::Couch

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

Overview

CouchDB backend

You can store hashes directly using this adapter.

Examples:

Store hashes

db = Moneta::Adapters::Mongo.new
db['key'] = {a: 1, b: 2}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #decrement, #features, #fetch, included, #increment, #supports?

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: '127.0.0.1'

    Couch host

  • :port (String) — default: 5984

    Couch port

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

    Couch database

  • :value_field (String) — default: 'value'

    Document field to store value

  • :type_field (String) — default: 'type'

    Document field to store value type

  • :backend (Faraday connection)

    Use existing backend instance


29
30
31
32
33
34
35
# File 'lib/moneta/adapters/couch.rb', line 29

def initialize(options = {})
  @value_field = options[:value_field] || 'value'
  @type_field = options[:type_field] || 'type'
  url = "http://#{options[:host] || '127.0.0.1'}:#{options[:port] || 5984}/#{options[:db] || 'moneta'}"
  @backend = options[:backend] || ::Faraday.new(url: url)
  create_db
end

Instance Attribute Details

#backendObject (readonly)


18
19
20
# File 'lib/moneta/adapters/couch.rb', line 18

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

75
76
77
78
79
# File 'lib/moneta/adapters/couch.rb', line 75

def clear(options = {})
  @backend.delete ''
  create_db
  self
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.

Parameters:

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

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)

Returns:

  • (Boolean)

    key was set


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/moneta/adapters/couch.rb', line 82

def create(key, value, options = {})
  body = value_to_body(value, nil)
  response = @backend.put(key, body, 'Content-Type' => 'application/json')
  case response.status
  when 201
    true
  when 409
    false
  else
    raise "HTTP error #{response.status}"
  end
rescue
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
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 Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/moneta/adapters/couch.rb', line 61

def delete(key, options = {})
  response = @backend.get(key)
  if response.status == 200
    value = body_to_value(response.body)
    response = @backend.delete("#{key}?rev=#{response['etag'][1..-2]}")
    raise "HTTP error #{response.status}" unless response.status == 200
    value
  end
rescue
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
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 Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

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

def key?(key, options = {})
  @backend.head(key).status == 200
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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


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

def load(key, options = {})
  response = @backend.get(key)
  response.status == 200 ? body_to_value(response.body) : 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 Expires)

  • :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

Returns:

  • value


49
50
51
52
53
54
55
56
57
58
# File 'lib/moneta/adapters/couch.rb', line 49

def store(key, value, options = {})
  response = @backend.head(key)
  body = value_to_body(value, response.status == 200 && response['etag'][1..-2])
  response = @backend.put(key, body, 'Content-Type' => 'application/json')
  raise "HTTP error #{response.status}" unless response.status == 201
  value
rescue
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
end