Module: Picky::Backends::Redis::Scripting

Defined in:
lib/picky/backends/redis.rb

Overview

Uses Lua scripting on Redis 2.6.

Instance Method Summary collapse

Instance Method Details

#ids(combinations, amount, offset) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/picky/backends/redis.rb', line 224

def ids combinations, amount, offset
  identifiers = identifiers_for combinations

  # Assume it's using EVALSHA.
  #
  begin
    if identifiers.size > 1
      # Reuse script already installed in Redis.
      #
      # Note: This raises an error in Redis,
      # when the script is not installed.
      #
      client.evalsha @ids_script_hash,
                     identifiers,
                     [
                       generate_intermediate_result_id,
                       offset,
                       (offset + amount)
                     ]
    else
      # No complex calculation necessary.
      #
      client.zrange identifiers.first,
                    offset,
                    (offset + amount)
    end
  rescue ::Redis::CommandError => e
    # Install script in Redis.
    #
    @ids_script_hash = client.script 'load', @ids_script
    retry
  end
end