Sha256: 9f6700fddd248cb2c19d929d30bca1ea45772b4919ad0e35a07f04b81c0b0e81

Contents?: true

Size: 945 Bytes

Versions: 1

Compression:

Stored size: 945 Bytes

Contents

module RedisEval
  class Script
    attr_accessor :parent
    attr_reader :source, :sha

    def self.build_from_parent(src, parent, with_load: true)
      script        = new(src, with_load: with_load)
      script.parent = parent
      script
    end

    def initialize(src, with_load: true)
      @source = src
      @sha    = Digest::SHA1.hexdigest(@source)
      @redis  = nil
      self.load if with_load
    end

    def load
      redis.script(:load, source)
    end

    def exist?
      redis.script(:exists, sha)
    end

    def execute(keys = [], argv = [])
      redis.evalsha(sha, Array(keys), Array(argv))
    rescue Redis::CommandError => e
      if e.message =~ /NOSCRIPT/
        redis.eval(source, Array(keys), Array(argv))
      else
        raise
      end
    end

    def redis
      return @redis if @redis
      parent ? parent.redis : Redis.current
    end

    def redis=(conn)
      @redis = conn
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_eval-0.2.0 lib/redis_eval/script.rb