lib/splash/backends/redis.rb in prometheus-splash-0.0.3 vs lib/splash/backends/redis.rb in prometheus-splash-0.1.0

- old
+ new

@@ -1,40 +1,62 @@ require "redis" +require "socket" module Splash module Backends class Redis include Splash::Config def initialize(store) + @hostname = Socket.gethostname @config = get_config[:backends][:stores][store] @store = ::Redis.new :host => @config[:host], :port => @config[:port], :db => @config[:base].to_i @redis_cli_cmd = `which redis-cli` @store.auth(@config[:auth]) if @config[:auth] end - def list(pattern='*') - return @store.keys pattern + def list(pattern='*', hostname = @hostname) + return @store.keys("#{hostname}##{pattern}").map{|item| item = remove_hostname(item)} end + def listall(pattern='*') + return @store.keys(pattern) + end + def get(options) - return @store.get(options[:key]) + hostname = (options[:hostname])? options[:hostname] : @hostname + return @store.get(prefix_hostname(options[:key],hostname)) end def put(options) - @store.set options[:key], options[:value] + hostname = (options[:hostname])? options[:hostname] : @hostname + @store.set prefix_hostname(options[:key],hostname), options[:value] end def del(options) - @store.del options[:key] + hostname = (options[:hostname])? options[:hostname] : @hostname + @store.del prefix_hostname(options[:key],hostname) end def flush `#{@redis_cli_cmd} -n 3 flushdb` # @@store.flushdb end def exist?(options) - return ( not @store.get(options[:key]).nil?) + hostname = (options[:hostname])? options[:hostname] : @hostname + return ( not @store.get(prefix_hostname(options[:key],hostname)).nil?) + end + + private + def prefix_hostname(key,hostname) + return "#{hostname}##{key}" + end + + + def remove_hostname(astring) + result = astring.split("#") + result.shift + return result.join("#") end end end