Class: Moneta::Adapters::Riak

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

Overview

Riak backend

Author:

  • Potapov Sergey (aka Blake)

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

  • :backend (::Riak::Client)

    Use existing backend instance



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

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

Instance Attribute Details

#backendObject (readonly)



11
12
13
# File 'lib/moneta/adapters/riak.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: {})


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

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



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

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)


26
27
28
# File 'lib/moneta/adapters/riak.rb', line 26

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



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

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

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



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

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