lib/picky/backends/redis.rb in picky-4.0.0pre2 vs lib/picky/backends/redis.rb in picky-4.0.0pre3

- old
+ new

@@ -5,21 +5,19 @@ # # class Redis < Backend attr_reader :client, - :immediate + :realtime def initialize options = {} - super options - maybe_load_hiredis check_hiredis_gem check_redis_gem @client = options[:client] || ::Redis.new(:db => (options[:db] || 15)) - @immediate = options[:immediate] + @realtime = options[:realtime] end def maybe_load_hiredis require 'hiredis' rescue LoadError # It's ok. @@ -37,40 +35,35 @@ # Returns an object that on #initial, #load returns an object that responds to: # [:token] # => [id, id, id, id, id] (an array of ids) # def create_inverted bundle - extract_lambda_or(inverted, bundle, client) || - List.new(client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:inverted", immediate: immediate) + List.new client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:inverted", realtime: realtime end # Returns an object that on #initial, #load returns an object that responds to: # [:token] # => 1.23 (a weight) # def create_weights bundle - extract_lambda_or(weights, bundle, client) || - Float.new(client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:weights", immediate: immediate) + Float.new client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:weights", realtime: realtime end # Returns an object that on #initial, #load returns an object that responds to: # [:encoded] # => [:original, :original] (an array of original symbols this similarity encoded thing maps to) # def create_similarity bundle - extract_lambda_or(similarity, bundle, client) || - List.new(client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:similarity", immediate: immediate) + List.new client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:similarity", realtime: realtime end # Returns an object that on #initial, #load returns an object that responds to: # [:key] # => value (a value for this config key) # def create_configuration bundle - extract_lambda_or(configuration, bundle, client) || - String.new(client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:configuration", immediate: immediate) + String.new client, "#{PICKY_ENVIRONMENT}:#{bundle.identifier}:configuration", realtime: realtime end # Returns an object that on #initial, #load returns an object that responds to: # [id] # => [:sym1, :sym2] # def create_realtime bundle - extract_lambda_or(similarity, bundle) || - List.new(client, "#{bundle.identifier}:realtime", immediate: immediate) + List.new client, "#{bundle.identifier}:realtime", realtime: realtime end # Does the Redis version already include # scripting support? # @@ -160,16 +153,16 @@ # backend implementations. # # Note: We use the amount and offset hints to speed Redis up. # def ids combinations, amount, offset - # TODO FIXME This is actually not correct: - # A dumped/loaded Redis backend should use - # the Redis backend calculation method. - # So loaded? would be more appropriate. + # TODO This is actually not correct: + # A dumped/loaded Redis backend should use + # the Redis backend calculation method. + # So loaded? would be more appropriate. # - if immediate + if realtime # Just checked once on the first call. # if redis_with_scripting? @@ids_script = "local intersected = redis.call('zinterstore', ARGV[1], #(KEYS), unpack(KEYS)); if intersected == 0 then redis.call('del', ARGV[1]); return {}; end local results = redis.call('zrange', ARGV[1], tonumber(ARGV[2]), tonumber(ARGV[3])); redis.call('del', ARGV[1]); return results;" @@ -185,16 +178,22 @@ end # Assume it's using EVALSHA. # begin - client.evalsha @@ids_sent_once, - identifiers.size, - *identifiers, - generate_intermediate_result_id, - offset, - (offset + amount) + if identifiers.size > 1 + client.evalsha @@ids_sent_once, + identifiers.size, + *identifiers, + generate_intermediate_result_id, + offset, + (offset + amount) + else + client.zrange identifiers.first, + offset, + (offset + amount) + end rescue RuntimeError => e # Make the server have a SHA-1 for the script. # @@ids_sent_once = Digest::SHA1.hexdigest @@ids_script client.eval @@ids_script, @@ -216,11 +215,9 @@ end result_id = generate_intermediate_result_id # Little optimization. - # - # TODO Include in the scripting version as well. # if identifiers.size > 1 # Intersect and store. # intersected = client.zinterstore result_id, identifiers \ No newline at end of file