Sha256: f67d60b1cd61aee1723b9aae433b79d4b27e0e7c31c9f281a10537db76f13cb1

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

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

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

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

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

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

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

      redis.eval(source, Array(keys), Array(argv))
    end

    def redis
      return @redis if instance_variable_defined?(:@redis)
      return parent.redis unless parent.nil?

      Redis.current
    end

    private

      def redis_without_namespace
        defined?(Redis::Namespace) && redis.is_a?(Redis::Namespace) ? redis.redis : redis
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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