/
riak.rb
55 lines (47 loc) · 1.44 KB
/
riak.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright: 2011 TMX Credit
# Author: Potapov Sergey (aka Blake)
require 'riak'
module Moneta
module Adapters
# Riak backend
# @api public
class Riak
include Defaults
# @param [Hash] options
# @option options [String] :bucket ('moneta') Bucket name
# @option options [String] :content_type ('application/octet-stream') Default content type
# @option options All other options passed to `Riak::Client#new`
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
def key?(key, options = {})
@bucket.exists?(key, options.dup)
end
def load(key, options = {})
@bucket.get(key, options.dup).raw_data
rescue ::Riak::FailedRequest => ex
nil
end
def delete(key, options = {})
value = load(key, options)
@bucket.delete(key, options.dup)
value
end
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
def clear(options = {})
@bucket.keys do |keys|
keys.each{ |key| @bucket.delete(key) }
end
self
end
end
end
end