Class: Moneta::Adapters::Couch

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

Overview

CouchDB backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #create, #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

  • :backend (Faraday connection)

    Use existing backend instance



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

def initialize(options = {})
  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)



11
12
13
# File 'lib/moneta/adapters/couch.rb', line 11

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: {})


63
64
65
66
67
# File 'lib/moneta/adapters/couch.rb', line 63

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

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



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

def delete(key, options = {})
  response = @backend.get(key)
  if response.status == 200
    value = MultiJson.load(response.body)['value']
    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)


25
26
27
# File 'lib/moneta/adapters/couch.rb', line 25

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)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



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

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



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

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