Class: Moneta::Adapters::Riak

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

Overview

Riak 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 = {}) ⇒ Riak

Returns a new instance of Riak.

Parameters:

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

Options Hash (options):

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

    Bucket name

  • :content_type (String) — default: 'application/octet-stream'

    Default content type

  • All (Object)

    other options passed to ‘Riak::Client#new`



17
18
19
20
21
# File 'lib/moneta/adapters/riak.rb', line 17

def initialize(options = {})
  bucket = options.delete(:bucket) || 'moneta'
  @content_type = options.delete(:content_type) || 'application/octet-stream'
  @bucket = ::Riak::Client.new(options).bucket(bucket)
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


52
53
54
55
56
57
# File 'lib/moneta/adapters/riak.rb', line 52

def clear(options = {})
  @bucket.keys do |keys|
    keys.each{ |key| @bucket.delete(key) }
  end
  self
end

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

Delete the key from the store and return the current value

Parameters:

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

Returns:

  • (Object)

    current value



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

def delete(key, options = {})
  value = load(key, options)
  @bucket.delete(key, options.dup)
  value
end

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

Exists the value with key

Parameters:

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

Returns:

  • (Boolean)


24
25
26
# File 'lib/moneta/adapters/riak.rb', line 24

def key?(key, options = {})
  @bucket.exists?(key, options.dup)
end

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

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

Parameters:

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

Returns:

  • (Object)

    value



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

def load(key, options = {})
  @bucket.get(key, options.dup).raw_data
rescue ::Riak::FailedRequest => ex
  nil
end

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

Store value with key

Parameters:

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

Returns:

  • value



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

def store(key, value, options = {})
  obj = ::Riak::RObject.new(@bucket, key)
  obj.content_type = options[:content_type] || @content_type
  obj.raw_data = value
  obj.store(options.dup)
  value
end