Sha256: 1675fbefdb3267fda188db86c8514ffc300359e2440821f767e11311f86d47fb

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

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='*', 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

1 entries across 1 versions & 1 rubygems

Version Path
prometheus-splash-0.1.0 lib/splash/backends/redis.rb