Class: RedisPrescription

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_prescription.rb,
lib/redis_prescription/errors.rb,
lib/redis_prescription/version.rb,
lib/redis_prescription/adapters.rb,
lib/redis_prescription/adapters/redis.rb,
lib/redis_prescription/adapters/redis_client.rb,
lib/redis_prescription/adapters/redis_namespace.rb

Overview

Lua script executor for redis.

Instead of executing script with ‘EVAL` everytime - loads script once and then runs it with `EVALSHA`.

Examples:

Usage


redis  = Redis.new
script = RedisPrescription.new("return ARGV[1] + ARGV[2]")
script.call(redis, argv: [2, 2]) # => 4

Direct Known Subclasses

Redis::Prescription

Defined Under Namespace

Modules: Adapters Classes: CommandError, Error, ScriptError

Constant Summary collapse

VERSION =

Gem version.

"2.6.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ RedisPrescription

Returns a new instance of RedisPrescription.

Parameters:

  • source (#to_s)

    Lua script



36
37
38
39
# File 'lib/redis_prescription.rb', line 36

def initialize(source)
  @source = -source.to_s
  @digest = Digest::SHA1.hexdigest(@source).freeze
end

Instance Attribute Details

#digestString (readonly)

Lua script SHA1 digest.

Returns:

  • (String)


33
34
35
# File 'lib/redis_prescription.rb', line 33

def digest
  @digest
end

#sourceString (readonly)

Lua script source.

Returns:

  • (String)


29
30
31
# File 'lib/redis_prescription.rb', line 29

def source
  @source
end

Instance Method Details

#call(redis, keys: EMPTY_LIST, argv: EMPTY_LIST) ⇒ Object

Executes script and return result of execution.

Parameters:

  • redis (Redis, RedisClient)
  • keys (Array) (defaults to: EMPTY_LIST)

    keys to pass to the script

  • argv (Array) (defaults to: EMPTY_LIST)

    arguments to pass to the script

Returns:

  • depends on the script

Raises:

  • (TypeError)

    if given redis client is not supported

  • (ScriptError)

    if script execution failed



48
49
50
51
52
# File 'lib/redis_prescription.rb', line 48

def call(redis, keys: EMPTY_LIST, argv: EMPTY_LIST)
  evalsha_with_fallback(Adapters[redis], redis, keys, argv)
rescue CommandError => e
  raise ScriptError.new(e.message, @source)
end