lib/riak/cache_store.rb in riak-client-0.7.1 vs lib/riak/cache_store.rb in riak-client-0.8.0.beta
- old
+ new
@@ -17,65 +17,63 @@
attr_accessor :client
def initialize(options = {})
@bucket_name = options.delete(:bucket) || '_cache'
@n_value = options.delete(:n_value) || 2
- @r = [options.delete(:r) || 1, @n_value].min
- @w = [options.delete(:w) || 1, @n_value].min
- @dw = [options.delete(:dw) || 0, @n_value].min
- @rw = [options.delete(:rw) || 1, @n_value].min
+ @r = options.delete(:r) || 1
+ @w = options.delete(:w) || 1
+ @dw = options.delete(:dw) || 0
+ @rw = options.delete(:rw) || "quorum"
@client = Riak::Client.new(options)
+ set_bucket_defaults
end
def bucket
- @bucket ||= @client.bucket(@bucket_name, :keys => false).tap do |b|
- begin
- b.n_value = @n_value unless b.n_value == @n_value
- rescue
- end
- end
+ @bucket ||= @client.bucket(@bucket_name, :keys => false)
end
-
- def write(key, value, options={})
- super do
- object = bucket.get_or_new(key, :r => @r)
- object.content_type = 'application/yaml'
- object.data = value
- object.store(:r => @r, :w => @w, :dw => @dw)
- end
- end
- def read(key, options={})
- super do
- begin
- bucket.get(key, :r => @r).data
- rescue Riak::FailedRequest => fr
- raise fr unless fr.code == 404
- nil
+ def delete_matched(matcher, options={})
+ instrument(:delete_matched, matcher) do
+ bucket.keys do |keys|
+ keys.grep(matcher).each do |k|
+ bucket.delete(k)
+ end
end
end
end
- def exist?(key)
- super do
- bucket.exists?(key, :r => @r)
+ protected
+ def set_bucket_defaults
+ begin
+ new_values = {}
+ new_values['n_val'] = @n_value unless bucket.n_value == @n_value
+ new_values['r'] = @r unless bucket.r == @r
+ new_values['w'] = @w unless bucket.w == @w
+ new_values['dw'] = @dw unless bucket.dw == @dw
+ new_values['rw'] = @rw unless bucket.rw == @rw
+ bucket.props = new_values unless new_values.empty?
+ rescue
end
end
- def delete_matched(matcher, options={})
- super do
- bucket.keys do |keys|
- keys.grep(matcher).each do |k|
- bucket.delete(k, :rw => @rw)
- end
- end
- end
+ def write_entry(key, value, options={})
+ object = bucket.get_or_new(key)
+ object.content_type = 'application/yaml'
+ object.data = value
+ object.store
end
- def delete(key, options={})
- super do
- bucket.delete(key, :rw => @rw)
+ def read_entry(key, options={})
+ begin
+ bucket.get(key).data
+ rescue Riak::FailedRequest => fr
+ raise fr unless fr.code == 404
+ nil
end
+ end
+
+ def delete_entry(key, options={})
+ bucket.delete(key)
end
end
end
ActiveSupport::Cache::RiakStore = Riak::CacheStore unless defined?(ActiveSupport::Cache::RiakStore)