Class: Moneta::Adapters::Riak

Inherits:
Moneta::Adapter show all
Defined in:
lib/moneta/adapters/riak.rb

Overview

Riak backend

Author:

  • Potapov Sergey (aka Blake)

Instance Attribute Summary

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

Methods included from Defaults

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

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

  • :backend (::Riak::Client)

    Use existing backend instance

  • All (Object)

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



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

def initialize(options = {})
  super
  @bucket = backend.bucket(config.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: {})


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

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

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



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

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

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/riak.rb', line 25

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

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



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

def load(key, options = {})
  @bucket.get(key, options.dup).raw_data
rescue ::Riak::FailedRequest
  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



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

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