Class: Moneta::Adapters::Riak

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

Instance Method Summary collapse

Methods included from Defaults

#[]=, #fetch

Constructor Details

#initialize(options = {}) ⇒ Riak

Returns a new instance of Riak.



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

def initialize(options = {})
  bucket_name = options.delete(:bucket) || 'moneta'
  client = ::Riak::Client.new(options)
  @bucket = client.bucket(bucket_name)
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  serialized_key = key_for(key)
  deserialize(@bucket[serialized_key].data)
rescue ::Riak::HTTPFailedRequest => err
  nil
end

#clearObject



48
49
50
# File 'lib/moneta/adapters/riak.rb', line 48

def clear(*)
  @bucket.keys.each{|key| @bucket.delete(key)}
end

#delete(key) ⇒ Object



33
34
35
36
37
38
# File 'lib/moneta/adapters/riak.rb', line 33

def delete(key, *)
  value = self[key]
  serialized_key = key_for(key)
  @bucket.delete(serialized_key)
  value
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/moneta/adapters/riak.rb', line 22

def key?(key, *)
  !!self[key]
end

#store(key, value) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/moneta/adapters/riak.rb', line 40

def store(key, value, *)
  serialized_key = key_for(key)
  obj = ::Riak::RObject.new(@bucket, serialized_key)
  obj.content_type = "text/plain"
  obj.data = serialize(value)
  obj.store
end