Sha256: 972b2402d8c14c1a5fe15edac08b9edf0dbd7fec7c9e14ea8502873a99da3662

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

# coding: utf-8
module Splash
  module Backends
    class Redis
      include Splash::Config
      def initialize(store)
        @hostname = Socket.gethostname
        @config = get_config[:backends][:stores][store]
        conf = { :host => @config[:host], :port => @config[:port], :db => @config[:base].to_i}
        conf[:password] = @config[:auth] if @config[:auth]
        @store = ::Redis.new conf
        @redis_cli_cmd = `which redis-cli`
        @store.auth(@config[:auth]) if @config[:auth]
      end

      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)
        hostname = (options[:hostname])? options[:hostname] : @hostname
        return @store.get(prefix_hostname(options[:key],hostname))
      end

      def put(options)
        hostname = (options[:hostname])? options[:hostname] : @hostname
        @store.set prefix_hostname(options[:key],hostname), options[:value]
      end

      def del(options)
        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)
        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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prometheus-splash-0.5.0 lib/splash/backends/redis.rb
prometheus-splash-0.4.5 lib/splash/backends/redis.rb
prometheus-splash-0.4.4 lib/splash/backends/redis.rb